Fix enum private names (__x) incorrectly marked as final#20832
Open
Fridayai700 wants to merge 2 commits intopython:masterfrom
Open
Fix enum private names (__x) incorrectly marked as final#20832Fridayai700 wants to merge 2 commits intopython:masterfrom
Fridayai700 wants to merge 2 commits intopython:masterfrom
Conversation
Since Python 3.11, private names (names starting with double underscore
but not ending with it, like `__config`) are not enum members and
should not be treated as final.
The semantic analyzer was using `is_dunder()` to skip only dunder names
(`__x__`), missing private names (`__x`). This caused false positives
like "Cannot assign to final attribute" when reassigning private
attributes on an enum.
The fix uses `name.startswith("__")` which matches the logic already
used by `TypeInfo.enum_members` in nodes.py, ensuring consistency
between member detection and finality marking.
Fixes python#20789
for more information, see https://pre-commit.ci
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Since Python 3.11, private names (names starting with
__but not ending with__, like__config) are not enum members and should not be treated as final. Currently, mypy incorrectly marks them as final, causing false positives.Before:
After: No error — private names are not enum members and can be reassigned.
Root cause
In
semanal.py, the check for non-final enum names usedis_dunder()which only matches__x__(dunder names). Private names like__configwere missed.Meanwhile,
TypeInfo.enum_membersinnodes.pyalready correctly usedname.startswith("__")to exclude both dunders AND private names from the member list. This created an inconsistency:__configwas not listed as a member but was still treated as final.Fix
Changed the finality check to use
name.startswith("__"), matching the existingenum_memberslogic. This ensures consistency between member detection and finality marking.Test plan
testEnumPrivateNameNotFinaltest casepython -m pytest mypy/test/testcheck.py -k Enum -o "addopts="— all passFixes #20789