You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Guido seems to be working on this here. I have also tried implementing it myself. len is easy; the problem is with declaring EnumMeta as an iterable, since it should be iterable over the concrete subclass, which is not declared yet. Selftype solves this nicely for the method definition, but not for the subclassing declaration. The closest I've got is
This gives us list(Letters) as List[Enum], which is not very helpful.
But in a way, metaclasses are generic in the concrete base class, so we should be writing something like
T=TypeVar('T', metaclass='EnumMeta')
classEnumMeta(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: ...
classEnum(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 Any - but it would be nice to have the "right" solution somehow.
To summarize:
len and __getitem__ seems to be easy
We might need "inference" for protocols like Iterable[Subclass] since we can't express this today
__getitem__ already works (#2812). Making the iteration item type Any would be a reasonable first step to unblock the use case. It looks like inferring the item type would require some special casing in mypy.
Activity
ilevkivskyi commentedon Feb 22, 2017
This is probably related to #2305
Now that we have some support for metaclasses, both these should be not difficult to fix.
@elazarg what do you think?
elazarg commentedon Feb 22, 2017
Guido seems to be working on this here. I have also tried implementing it myself.
lenis easy; the problem is with declaringEnumMetaas an iterable, since it should be iterable over the concrete subclass, which is not declared yet. Selftype solves this nicely for the method definition, but not for the subclassing declaration. The closest I've got isThis gives us
list(Letters)asList[Enum], which is not very helpful.But in a way, metaclasses are generic in the concrete base class, so we should be writing something like
We can probably give some ad-hoc fix for protocols such as Iterable - possibly by inferring the generic parameter instead of defaulting to
Any- but it would be nice to have the "right" solution somehow.To summarize:
lenand__getitem__seems to be easyIterable[Subclass]since we can't express this todayJukkaL commentedon Feb 24, 2017
__getitem__already works (#2812). Making the iteration item typeAnywould be a reasonable first step to unblock the use case. It looks like inferring the item type would require some special casing in mypy.MeGotsThis commentedon Mar 1, 2017
Also here are some other lines I just ran into:
produces in mypy
MeGotsThis commentedon Mar 2, 2017
Found another case
produces in mypy 0.470
gvanrossum commentedon Apr 21, 2017
Fixed by #1136.
ilevkivskyi commentedon Apr 21, 2017
There is a small remainder:
But it looks like a purely typeshed issue (
EnumMetashould inherit fromReversible).