Uh oh!
There was an error while loading. Please reload this page.
Makes Enum members implicitly final, refs #5599 - #10852
Conversation
This comment has been minimized.
This comment has been minimized.
sobolevn
commented
Jul 21, 2021
This one is interesting: classA(str, Enum):
value='value'Produces: |
This comment has been minimized.
This comment has been minimized.
sobolevn
commented
Jul 21, 2021
Is this fail related? It does not seem to be. |
hauntsaninja
commented
Jul 21, 2021
It's unrelated, @pranavrajpal fixed this in #10853 |
sobolevn
commented
Jul 21, 2021
Rebased, now it should be fine. |
This comment has been minimized.
This comment has been minimized.
bluetech
commented
Aug 13, 2021
I was just intending to implement this myself, but luckily I checked before and saw there's already a PR for it :) My reason for wanting this is not so much marking the values immutable, but to have classA(Enum):
some: Final='some'other='other'reveal_type(A.some.value) # Literal['some']reveal_type(A.other.value) # strbut no reason for it to not be implicit. |
JukkaL
left a comment
There was a problem hiding this comment.
Nice, enum members are clearly not mutable. This looks good to me overall, but I have question about one change.
| proper_type is not None and is_equivalent(proper_type, underlying_type) | ||
| for proper_type in proper_types) | ||
| if all_equivalent_types: | ||
| return make_simplified_union(cast(Sequence[Type], proper_types)) |
There was a problem hiding this comment.
It's unclear to me when the above new code is needed. Is there a test case that depends on this? If not, could you add a test case.
In any case, it would be good to have a comment here.
There was a problem hiding this comment.
Here are related changes:
- https://github.com/python/mypy/pull/10852/files#diff-ebc735ad8fa6eb42d5390fa4fa9ef60581764c7bbfb5eafcde16f5e40c330c84R69
- https://github.com/python/mypy/pull/10852/files#diff-ebc735ad8fa6eb42d5390fa4fa9ef60581764c7bbfb5eafcde16f5e40c330c84R143
- https://github.com/python/mypy/pull/10852/files#diff-ebc735ad8fa6eb42d5390fa4fa9ef60581764c7bbfb5eafcde16f5e40c330c84R102
There was a problem hiding this comment.
Updated code with more comments 👍
This comment has been minimized.
This comment has been minimized.
hauntsaninja
left a comment
There was a problem hiding this comment.
Thanks, this is a nice improvement. Lgtm, but wouldn't mind if Jukka took another look.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
This comment has been minimized.
This comment has been minimized.
sobolevn
commented
Nov 7, 2021
Rebased! 🚜 |
This comment has been minimized.
This comment has been minimized.
sobolevn
commented
Nov 7, 2021
Merge conflicts 😮💨 Should be fine now! |
Diff from mypy_primer, showing the effect of this PR on open source code: prefect (https://github.com/PrefectHQ/prefect.git)
+ src/prefect/core/task.py:45: error: Cannot override writable attribute "value" with a final one
alerta (https://github.com/alerta/alerta.git)
+ alerta/models/enums.py:159: error: Cannot override writable attribute "value" with a final one |
97littleleaf11
commented
Nov 10, 2021
lgtm, I really like this feature! Also thanks for your detailed comments. |
sobolevn
commented
Nov 10, 2021
Awesome, thanks, everyone! 🎉 |
joey-laminar
commented
Jan 5, 2022
It looks like this still doesn't work in latest mypy for strings (but does work for int/bool). Any idea why? |
refs python#5599 This change allows to catch this error by making all Enum members implicitly Final. Also modifies Enum plugin, since it was not ready to work with `Literal[True]` and `Literal[False]`. Co-authored-by: Shantanu <12621235+hauntsaninja@users.noreply.github.com>
Makes
Enummembers implicitlyfinalMy problem was that this code was typechecking:
While in runtime it was failing with:
This change allows to catch this error by making all
Enummembers implicitlyFinal.I also had to modify
Enumplugin, since it was not ready to work withLiteral[True]andLiteral[False].Closes#5599
Notes
I would love to hear your feedback! 👍