-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Closed
Labels
Description
Steps to reproduce
Run mypy against the following code:
from enum import Enum
class OuterEnum(Enum):
foo = "bar"
class Foo:
def __init__(self,) -> None:
self.OuterEnum = OuterEnum
list(self.OuterEnum)
self.InnerEnum = Enum("InnerEnum", [("foo", "bar")])
list(self.InnerEnum) # Error!
[i for i in self.InnerEnum] # Error!
self.DictEnum = Enum("DictEnum", {"foo": "bar"})
list(self.DictEnum) # Error!
OtherEnum = Enum("OtherEnum", [("foo", "bar")])
list(OtherEnum)
Observed behavior
When trying to iterate over an enumeration defined as an instance attribute using the functional API, mypy raises an error when attempting to iterate over said attribute.
The above snippet raises three mypy errors where marked:
$ poetry run mypy foo.py
foo.py:13: error: No overload variant of "list" matches argument type "Enum"
foo.py:13: note: Possible overload variant:
foo.py:13: note: def [_T] __init__(self, iterable: Iterable[_T]) -> List[_T]
foo.py:13: note: <1 more non-matching overload not shown>
foo.py:14: error: "Enum" has no attribute "__iter__"; maybe "__str__" or "__dir__"? (not iterable)
foo.py:16: error: No overload variant of "list" matches argument type "Enum"
foo.py:16: note: Possible overload variant:
foo.py:16: note: def [_T] __init__(self, iterable: Iterable[_T]) -> List[_T]
foo.py:16: note: <1 more non-matching overload not shown>
Found 3 errors in 1 file (checked 1 source file)
Expected behavior
When testing the above code, iteration over the instance attribute Enum works fine, and mypy does not raise errors when iterating over similar Enums if they're defined using the class syntax or aren't instance attributes. I would not expect mypy to raise an error here.
Environment
$ poetry run mypy --version
mypy 0.740
$ poetry run python --version
Python 3.7.4
Also tested against master with the same behavior
$ poetry run mypy --version
mypy 0.750+dev.2af0ac021335ec4a3e84a0ba739b4aea7e659c43
Thank you for your work on this tool!