Skip to content
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

Merged
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
link to spec
hauntsaninja committed Oct 26, 2024

Unverified

This commit is not signed, but one or more authors requires that any commit attributed to them is signed.
commit 295343e21d10d6398c87a7a18a9bfb64706fdabd
4 changes: 4 additions & 0 deletions mypy/semanal.py
Original file line number Diff line number Diff line change
@@ -4237,6 +4237,10 @@ def analyze_name_lvalue(

if explicit_type and has_explicit_value:
self.fail("Enum members must be left unannotated", lvalue)
self.note(
"See https://typing.readthedocs.io/en/latest/spec/enums.html#defining-members",
lvalue,
)

if (not existing or isinstance(existing.node, PlaceholderNode)) and not outer:
# Define new variable.
12 changes: 7 additions & 5 deletions test-data/unit/check-enum.test
Original file line number Diff line number Diff line change
@@ -1764,7 +1764,8 @@ class B(A):
x = 1 # E: Cannot override writable attribute "x" with a final one

class A1(Enum):
x: int = 1 # E: Enum members must be left unannotated
x: int = 1 # E: Enum members must be left unannotated \
# N: See https://typing.readthedocs.io/en/latest/spec/enums.html#defining-members
class B1(A1): # E: Cannot extend enum with existing members: "A1"
pass

@@ -1779,6 +1780,7 @@ class A3(Enum):
x: Final[int] # type: ignore
class B3(A3):
x = 1 # E: Cannot override final attribute "x" (previously declared in base class "A3")

[builtins fixtures/bool.pyi]

[case testEnumNotFinalWithMethodsAndUninitializedValuesStub]
@@ -2191,18 +2193,18 @@ def some_a(a: A):
from enum import Enum

class Medal(Enum):
gold: int = 1 # E: Enum members must be left unannotated
gold: int = 1 # E: Enum members must be left unannotated \
# N: See https://typing.readthedocs.io/en/latest/spec/enums.html#defining-members
silver: str = 2 # E: Enum members must be left unannotated \
# N: See https://typing.readthedocs.io/en/latest/spec/enums.html#defining-members \
# E: Incompatible types in assignment (expression has type "int", variable has type "str")
bronze = 3


[case testEnumMemberWithPlaceholder]
from enum import Enum

class Pet(Enum):
CAT = ...
DOG: str = ... # E: Enum members must be left unannotated \
# N: See https://typing.readthedocs.io/en/latest/spec/enums.html#defining-members \
# E: Incompatible types in assignment (expression has type "ellipsis", variable has type "str")

[file test.pyi]