-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
len(Enum) error #2891
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Guido seems to be working on this here. I have also tried implementing it myself. T = TypeVar('T', bound='Enum')
class EnumMeta(abc.ABCMeta, Iterable['Enum'], Sized): # ABCMeta fixes an unrelated issue
def __iter__(self: Type[T]) -> Iterator[T]: ...
def __getitem__(self: Type[T], item: str) -> T: ...
def __len__(self) -> int: ... This gives us But in a way, metaclasses are generic in the concrete base class, so we should be writing something like T = TypeVar('T', metaclass='EnumMeta')
class EnumMeta(Type[T], Iterable[T], Sized): # Type[T] is a declaration like Generic[T]
def __iter__(self: EnumMeta[T]) -> Iterator[T]: ...
def __getitem__(self: EnumMeta[T], item: str) -> T: ...
def __len__(self) -> int: ...
class Enum(metaclass=EnumMeta):
... We can probably give some ad-hoc fix for protocols such as Iterable - possibly by inferring the generic parameter instead of defaulting to To summarize:
|
|
Also here are some other lines I just ran into: print(Letters.A in Letters) produces in mypy
|
Found another case print(list(Letters))
print(list(reversed(Letters))) produces in mypy 0.470
|
Fixed by #1136. |
There is a small remainder:
But it looks like a purely typeshed issue ( |
Not sure if this is a mypy or typeshed problem.
Running mypy gives this error
Running python on it gives this
The text was updated successfully, but these errors were encountered: