Skip to content

Commit 66d4ca2

Browse files
committed
more
1 parent ae54bab commit 66d4ca2

File tree

4 files changed

+31
-7
lines changed

4 files changed

+31
-7
lines changed

CHANGELOG.md

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,36 @@ enum members must be left unannotated.
1111
class Pet(Enum):
1212
CAT = 1 # Member attribute
1313
DOG = 2 # Member attribute
14-
# WOLF = ... # The "..." placeholder can be used in type stubs
14+
WOLF: int = 3 # New error: Enum members must be left unannotated
1515

1616
species: str # Considered a non-member attribute
1717
```
1818

19-
Contributed by Terence Honles in PR [17207](https://github.com/python/mypy/pull/17207).
19+
In particular, the specification change can result in issues in type stubs (`.pyi` files), since
20+
historically it was common to leave the value absent:
21+
22+
```python
23+
# In a type stub (.pyi file)
24+
25+
class Pet(Enum):
26+
# Change in semantics: previously considered members, now non-member attributes
27+
CAT: int
28+
DOG: int
29+
30+
# Mypy will now issue a warning if it detects this situation in type stubs:
31+
# Detected an enum in a type stub with zero members.
32+
# There is a chance this is due to a recent change in the semantics of enum membership.
33+
# If so, use `member = value` to mark an enum member, instead of `member: type
34+
35+
class Pet(Enum):
36+
# As per the specification, you should now do one of the following:
37+
DOG = 1 # Member attribute with value 1 and known type
38+
WOLF = cast(int, ...) # Member attribute with unknown value but known type
39+
LION = ... # Member attribute with unknown value and unknown type
40+
```
41+
42+
Contributed by Terence Honles in PR [17207](https://github.com/python/mypy/pull/17207) and
43+
Shantanu Jain in PR [18068](https://github.com/python/mypy/pull/18068).
2044

2145
## Mypy 1.13
2246

mypy/checker.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2603,7 +2603,7 @@ def check_enum(self, defn: ClassDef) -> None:
26032603
self.fail(
26042604
"Detected an enum in a type stub with zero members. "
26052605
"There is a chance this is due to a recent change in the semantics of "
2606-
"enum membership. If so, use `member = ...` to mark an enum member, "
2606+
"enum membership. If so, use `member = value` to mark an enum member, "
26072607
"instead of `member: type`",
26082608
defn,
26092609
)

mypy/test/teststubtest.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1318,7 +1318,7 @@ def baz(x: Flags3 | None = ...) -> None: ...
13181318
""",
13191319
runtime="""
13201320
class Flags3(enum.Flag):
1321-
a = ...
1321+
a = 1
13221322
b = 2
13231323
def baz(x=Flags3(0)): pass
13241324
""",
@@ -1897,7 +1897,7 @@ def test_good_literal(self) -> Iterator[Case]:
18971897
18981898
import enum
18991899
class Color(enum.Enum):
1900-
RED: int
1900+
RED = ...
19011901
19021902
NUM: Literal[1]
19031903
CHAR: Literal['a']

test-data/unit/check-enum.test

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1788,7 +1788,7 @@ import lib
17881788

17891789
[file lib.pyi]
17901790
from enum import Enum
1791-
class A(Enum): # E: Detected an enum in a type stub with zero members. There is a chance this is due to a recent change in the semantics of enum membership. If so, use `member = ...` to mark an enum member, instead of `member: type` \
1791+
class A(Enum): # E: Detected an enum in a type stub with zero members. There is a chance this is due to a recent change in the semantics of enum membership. If so, use `member = value` to mark an enum member, instead of `member: type` \
17921792
# N: See https://typing.readthedocs.io/en/latest/spec/enums.html#defining-members
17931793
x: int
17941794
class B(A): # E: Cannot extend enum with existing members: "A"
@@ -1797,7 +1797,7 @@ class B(A): # E: Cannot extend enum with existing members: "A"
17971797
class C(Enum):
17981798
x = 1
17991799
class D(C): # E: Cannot extend enum with existing members: "C" \
1800-
# E: Detected an enum in a type stub with zero members. There is a chance this is due to a recent change in the semantics of enum membership. If so, use `member = ...` to mark an enum member, instead of `member: type` \
1800+
# E: Detected an enum in a type stub with zero members. There is a chance this is due to a recent change in the semantics of enum membership. If so, use `member = value` to mark an enum member, instead of `member: type` \
18011801
# N: See https://typing.readthedocs.io/en/latest/spec/enums.html#defining-members
18021802
x: int # E: Cannot assign to final name "x"
18031803
[builtins fixtures/bool.pyi]

0 commit comments

Comments
 (0)