-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
"Enum" has no attribute "__iter__" on instance attributes defined with functional API #8009
Comments
It looks like this might be more general than just iteration -- mypy raises an error on almost any normal
gives
|
Thanks for the prompt response; that makes sense. Out of curiosity, why should generating enums and namedtuples as attributes be prohibited? It would be quite convenient for me in this case, since I'd like to build up an enumeration from data I'm reading in from the file system. |
Creating a new type for every instance doesn't really make sense in a statically typed world (assuming we will never do full scale dependent types): Also this is honestly an anti-pattern. I could easily imagine this causes subtle bugs when you compare two enum values from different instances. Currently mypy allows assigning normal class objects (i.e. non-special like enums) as attributes, because this pattern is used in unit tests (and there it kind of fine because there is typically a single instance of the test case class). |
That makes sense to me. Thank you for the feedback. |
What about having an |
A similar question was asked in setuptools: how about at a module level? |
Steps to reproduce
Run mypy against the following code:
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:
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
Also tested against master with the same behavior
Thank you for your work on this tool!
The text was updated successfully, but these errors were encountered: