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
But it's not so simple, since Enum is a class appearing in the context of an expression, and therefore "coerce" to a callable somewhere in the code. Thus the above code still results in error: Iterable expected. At the point where the error is emitted, the information that it's enum does not seem to exist anymore - it is a simple callable.
Did you check the is_type_obj() method? It should still know...
Also, I doubt that defining a classmethod is the same as having it on the
metaclass. IIRC that doesn't even work in CPython.
As you said, it's not how mypy work. I think it should, but I guess it will require a big change.
(I wonder whether mypy should mimic Smalltalk's object model. It's very elegant, and perhaps we can think of Python as "hiding the theory" (e.g. that type(type) != type) for the convenience of the user. Similarly to the -type-in-type flag in COQ, which collapses the universe of types into a single one.
Yes, I know, I get carried away sometimes. Sorry)
This code runs in practice, but i get the following type checking error: error: Argument 1 to "iter" has incompatible type "Type[Color]"; expected "SupportsIter[<nothing>]"
Reacted by Fernando Sávio, Colin Atkinson and Rob Percival
Activity
elazarg commentedon Oct 26, 2016
At first sight, we only need #2193
And somehow making Enum itself iterable?
But it's not so simple, since
Enumis a class appearing in the context of an expression, and therefore "coerce" to a callable somewhere in the code. Thus the above code still results inerror: Iterable expected. At the point where the error is emitted, the information that it's enum does not seem to exist anymore - it is a simple callable.dokai commentedon Oct 26, 2016
In terms of making a class itself iterable it seems you have to use a metaclass instead of using just a
@classmethodin the class definition.gvanrossum commentedon Oct 26, 2016
gvanrossum commentedon Oct 27, 2016
elazarg commentedon Oct 27, 2016
I didn't check yet.
As you said, it's not how mypy work. I think it should, but I guess it will require a big change.
(I wonder whether mypy should mimic Smalltalk's object model. It's very elegant, and perhaps we can think of Python as "hiding the theory" (e.g. that type(type) != type) for the convenience of the user. Similarly to the
-type-in-typeflag in COQ, which collapses the universe of types into a single one.Yes, I know, I get carried away sometimes. Sorry)
Support enum iteration.
Support enum iteration.
Support enum iteration.
Support enum iteration (#1136)
gvanrossum commentedon Apr 21, 2017
Fixed by #1136.
11 remaining items
srittau commentedon Jan 31, 2018
For reference, using mypy 0.560, the following:
will print
I'd expect this to be
builtins.list[Foo]andbuiltins.set[Foo].elazarg commentedon Jan 31, 2018
This example seems to work on master
srittau commentedon Jan 31, 2018
I will report back when 0.570 is released.
Add enum iteration test cases
Add enum iteration test cases
Add enum iteration test cases
Add enum iteration test cases
Add enum iteration test cases (#4400)
ilevkivskyi commentedon Feb 13, 2018
All examples I have found in this issue now work on mypy master.
otchita commentedon Oct 8, 2020
I'm seeing this issue when using a subclass of
MultiValueEnumonmypy==0.782test.py:
> mypy test.pyJelleZijlstra commentedon Oct 8, 2020
@otchita that might be an issue with the stubs for aenum.
harahu commentedon Aug 23, 2022
@ilevkivskyi I'm seeing this issue when trying to express a function that should work for enums in general. Simplified example:
This code runs in practice, but i get the following type checking error:
error: Argument 1 to "iter" has incompatible type "Type[Color]"; expected "SupportsIter[<nothing>]"colatkinson commentedon Jan 19, 2023
Confirming that I'm running into the same behavior as @harahu on 0.991. Possibly related: #12553.
Interestingly, if that example is modified slightly, it does work:
So it seems that as far as mypy is concerned
Enums support iteration, but notiter().This also makes the error go away:
Not sure if this is exactly the same problem as the original -- perhaps a maintainer could advise as to whether a new issue is warranted.
ilevkivskyi commentedon Jan 29, 2023
Yes, this is most likely #12553 (which seems easy to fix, so I may submit PR there soon).