Skip to content

Commit 50d0967

Browse files
update: Type annotations are not allowed for enum members
Co-authored-by: Ali Hamdan <[email protected]>
1 parent dc00ef6 commit 50d0967

File tree

5 files changed

+40
-42
lines changed

5 files changed

+40
-42
lines changed

mypy/semanal.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4213,6 +4213,9 @@ def analyze_name_lvalue(
42134213
lvalue,
42144214
)
42154215

4216+
if explicit_type and has_explicit_value:
4217+
self.fail("Type annotations are not allowed for enum members", lvalue)
4218+
42164219
if (not existing or isinstance(existing.node, PlaceholderNode)) and not outer:
42174220
# Define new variable.
42184221
var = self.make_name_lvalue_var(lvalue, kind, not explicit_type, has_explicit_value)

mypyc/test-data/run-classes.test

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,8 +254,8 @@ from enum import Enum
254254

255255
class TestEnum(Enum):
256256
_order_ = "a b"
257-
a : int = 1
258-
b : int = 2
257+
a = 1
258+
b = 2
259259

260260
@classmethod
261261
def test(cls) -> int:

test-data/unit/check-enum.test

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1764,7 +1764,7 @@ class B(A):
17641764
x = 1 # E: Cannot override writable attribute "x" with a final one
17651765

17661766
class A1(Enum):
1767-
x: int = 1
1767+
x: int = 1 # E: Type annotations are not allowed for enum members
17681768
class B1(A1): # E: Cannot extend enum with existing members: "A1"
17691769
pass
17701770

@@ -2185,3 +2185,24 @@ reveal_type(A.y.value) # N: Revealed type is "Literal[2]?"
21852185
def some_a(a: A):
21862186
reveal_type(a.value) # N: Revealed type is "Union[Literal[1]?, Literal[2]?]"
21872187
[builtins fixtures/dict.pyi]
2188+
2189+
2190+
[case testErrorOnAnnotatedMember]
2191+
from enum import Enum
2192+
2193+
class Medal(Enum):
2194+
gold: int = 1 # E: Type annotations are not allowed for enum members
2195+
silver: str = 2 # E: Type annotations are not allowed for enum members \
2196+
# E: Incompatible types in assignment (expression has type "int", variable has type "str")
2197+
bronze = 3
2198+
2199+
2200+
[case testEnumMemberWithPlaceholder]
2201+
from enum import Enum
2202+
2203+
class Pet(Enum):
2204+
CAT = ...
2205+
DOG: str = ... # E: Type annotations are not allowed for enum members \
2206+
# E: Incompatible types in assignment (expression has type "ellipsis", variable has type "str")
2207+
2208+
[file test.pyi]

test-data/unit/check-python311.test

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,3 @@
1-
[case testMatchLiteralPatternEnumWithNonMember-xfail]
2-
from enum import Enum, nonmember
3-
from typing import NoReturn
4-
def assert_never(x: NoReturn) -> None: ...
5-
6-
class int:
7-
def __new__(cls, value: int): pass
8-
9-
class Medal(int, Enum):
10-
prize: str = nonmember("nothing")
11-
12-
def __new__(cls, value: int, prize: str | None = None) -> Medal:
13-
enum = int.__new__(cls, value)
14-
enum._value_ = value
15-
if prize is not None:
16-
enum.prize = prize
17-
return enum
18-
19-
gold = (1, 'cash prize')
20-
silver = (2, 'sponsorship')
21-
bronze = (3,)
22-
23-
m: Medal
24-
25-
match m:
26-
case Medal.gold:
27-
reveal_type(m) # N: Revealed type is "Literal[__main__.Medal.gold]"
28-
case Medal.silver:
29-
reveal_type(m) # N: Revealed type is "Literal[__main__.Medal.silver]"
30-
case Medal.bronze:
31-
reveal_type(m) # N: Revealed type is "Literal[__main__.Medal.bronze]"
32-
case _ as unreachable:
33-
assert_never(unreachable)
34-
35-
[builtins fixtures/tuple.pyi]
36-
371
[case testTryStarSimple]
382
try:
393
pass

test-data/unit/pythoneval.test

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1601,14 +1601,24 @@ _testSpecialTypingProtocols.py:8: error: Statement is unreachable
16011601
[case testEnumValueWithPlaceholderNodeType]
16021602
# https://github.com/python/mypy/issues/11971
16031603
from enum import Enum
1604-
from typing import Callable, Dict
1604+
from typing import Any, Callable, Dict
16051605
class Foo(Enum):
16061606
Bar: Foo = Callable[[str], None]
1607-
Baz: Foo = Callable[[Dict[str, "Missing"]], None]
1607+
Baz: Any = Callable[[Dict[str, "Missing"]], None]
1608+
1609+
reveal_type(Foo.Bar)
1610+
reveal_type(Foo.Bar.value) # this should probably not be "Foo" https://typing.readthedocs.io/en/latest/spec/enums.html#member-values
1611+
reveal_type(Foo.Baz)
1612+
reveal_type(Foo.Baz.value)
16081613
[out]
1614+
_testEnumValueWithPlaceholderNodeType.py:5: error: Type annotations are not allowed for enum members
16091615
_testEnumValueWithPlaceholderNodeType.py:5: error: Incompatible types in assignment (expression has type "<typing special form>", variable has type "Foo")
1610-
_testEnumValueWithPlaceholderNodeType.py:6: error: Incompatible types in assignment (expression has type "<typing special form>", variable has type "Foo")
1616+
_testEnumValueWithPlaceholderNodeType.py:6: error: Type annotations are not allowed for enum members
16111617
_testEnumValueWithPlaceholderNodeType.py:6: error: Name "Missing" is not defined
1618+
_testEnumValueWithPlaceholderNodeType.py:8: note: Revealed type is "Literal[_testEnumValueWithPlaceholderNodeType.Foo.Bar]?"
1619+
_testEnumValueWithPlaceholderNodeType.py:9: note: Revealed type is "_testEnumValueWithPlaceholderNodeType.Foo"
1620+
_testEnumValueWithPlaceholderNodeType.py:10: note: Revealed type is "Literal[_testEnumValueWithPlaceholderNodeType.Foo.Baz]?"
1621+
_testEnumValueWithPlaceholderNodeType.py:11: note: Revealed type is "Any"
16121622

16131623
[case testTypeshedRecursiveTypesExample]
16141624
from typing import List, Union

0 commit comments

Comments
 (0)