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
Fix enum attributes are not members #17207
base: master
Are you sure you want to change the base?
Fix enum attributes are not members #17207
Conversation
This adds on to the change in python#17182 and fixes enum attributes being used as members.
f319685
to
ba435ac
Compare
This comment has been minimized.
This comment has been minimized.
isinstance(sym.node, mypy.nodes.Var) | ||
and name not in ENUM_REMOVED_PROPS | ||
and not name.startswith("__") | ||
and sym.node.has_explicit_value |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This last condition changes the behavior of mypy which currently considers unassigned but annotated attributes as members of the enum.
I would say the new behavior is better but it must be noted in the PR title or description that this is a breaking change that must be considered on its own.
Looking at the mypy primer output, it looks like members of enums constructed using the enum call syntax do not have the has_explicit_value
set which breaks type narrowing. I suggest changing the variable here to also set this flag perhaps with a comment explaining the reason -- something like "E = Enum("E", "A")
is equivalent to class E(Enum): A = auto()
"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for noticing that, I've not really used them in that form, but hopefully the comment is not too wordy. I only glanced at the code that you pointed to, but I was cross referencing https://github.com/python/cpython/blob/7528b84e947f727734bd802356e380673553387d/Lib/enum.py#L828-L839 and how much of this is implemented by mypy? 🤔
Diff from mypy_primer, showing the effect of this PR on open source code: Tanjun (https://github.com/FasterSpeeding/Tanjun)
+ tanjun/_internal/__init__.py:189: error: Statement is unreachable [unreachable]
+ tanjun/annotations.py:2646: error: Statement is unreachable [unreachable]
|
It looks like this is actually related to the ongoing discussion python/typing-council#11 |
This adds on to the change in #17182 and fixes enum attributes being used as members.
@hamdanal / @hauntsaninja I noticed there was the function
get_enum_values
in mypy types and instead of adding more to the case intry_expanding_sum_type_to_union
it seemed like it might make sense to move the changes from #17182 there. I don't think the other code touched in that PR can useget_enum_values
.