Skip to content

Commit 2bfa3b4

Browse files
authored
Fix #11971 (#11972)
Allows PlaceholderNode objects before raising an error for the attempted reuse of a member name inside an Enum.
1 parent 17850b3 commit 2bfa3b4

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

mypy/semanal.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2871,7 +2871,7 @@ def analyze_name_lvalue(self,
28712871
outer = self.is_global_or_nonlocal(name)
28722872
if kind == MDEF and isinstance(self.type, TypeInfo) and self.type.is_enum:
28732873
# Special case: we need to be sure that `Enum` keys are unique.
2874-
if existing:
2874+
if existing is not None and not isinstance(existing.node, PlaceholderNode):
28752875
self.fail('Attempted to reuse member name "{}" in Enum definition "{}"'.format(
28762876
name, self.type.name,
28772877
), lvalue)

test-data/unit/check-enum.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1810,4 +1810,4 @@ reveal_type(A.str.value) # N: Revealed type is "Literal['foo']?"
18101810
reveal_type(A.int.value) # N: Revealed type is "Literal[1]?"
18111811
reveal_type(A.bool.value) # N: Revealed type is "Literal[False]?"
18121812
reveal_type(A.tuple.value) # N: Revealed type is "Tuple[Literal[1]?]"
1813-
[builtins fixtures/tuple.pyi]
1813+
[builtins fixtures/tuple.pyi]

test-data/unit/pythoneval.test

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1573,3 +1573,15 @@ x: OrderedDict[str, str] = OrderedDict({})
15731573
reveal_type(x) # Revealed type is "collections.OrderedDict[builtins.str, builtins.int]"
15741574
[out]
15751575
_testTypingExtensionsOrderedDictAlias.py:3: note: Revealed type is "collections.OrderedDict[builtins.str, builtins.str]"
1576+
1577+
[case testEnumValueWithPlaceholderNodeType]
1578+
# https://github.com/python/mypy/issues/11971
1579+
from enum import Enum
1580+
from typing import Callable, Dict
1581+
class Foo(Enum):
1582+
Bar: Foo = Callable[[str], None]
1583+
Baz: Foo = Callable[[Dict[str, "Missing"]], None]
1584+
[out]
1585+
_testEnumValueWithPlaceholderNodeType.py:5: error: Incompatible types in assignment (expression has type "object", variable has type "Foo")
1586+
_testEnumValueWithPlaceholderNodeType.py:6: error: Incompatible types in assignment (expression has type "object", variable has type "Foo")
1587+
_testEnumValueWithPlaceholderNodeType.py:6: error: Name "Missing" is not defined

0 commit comments

Comments
 (0)