Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 2607c06

Browse files
sobolevnpre-commit-ci[bot]
authored andcommittedFeb 24, 2025
Properly account for member and nonmember in TypeInfo.enum_members (python#18559)
Closes python#18557 Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 88fb767 commit 2607c06

File tree

3 files changed

+55
-5
lines changed

3 files changed

+55
-5
lines changed
 

‎mypy/nodes.py

+20-4
Original file line numberDiff line numberDiff line change
@@ -3247,10 +3247,26 @@ def enum_members(self) -> list[str]:
32473247
name
32483248
for name, sym in self.names.items()
32493249
if (
3250-
isinstance(sym.node, Var)
3251-
and name not in EXCLUDED_ENUM_ATTRIBUTES
3252-
and not name.startswith("__")
3253-
and sym.node.has_explicit_value
3250+
(
3251+
isinstance(sym.node, Var)
3252+
and name not in EXCLUDED_ENUM_ATTRIBUTES
3253+
and not name.startswith("__")
3254+
and sym.node.has_explicit_value
3255+
and not (
3256+
isinstance(
3257+
typ := mypy.types.get_proper_type(sym.node.type), mypy.types.Instance
3258+
)
3259+
and typ.type.fullname == "enum.nonmember"
3260+
)
3261+
)
3262+
or (
3263+
isinstance(sym.node, Decorator)
3264+
and any(
3265+
dec.fullname == "enum.member"
3266+
for dec in sym.node.decorators
3267+
if isinstance(dec, RefExpr)
3268+
)
3269+
)
32543270
)
32553271
]
32563272

‎mypy/typeops.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -955,7 +955,7 @@ class Status(Enum):
955955
FAILURE = 2
956956
UNKNOWN = 3
957957
958-
...and if we call `try_expanding_enum_to_union(Union[Color, Status], 'module.Color')`,
958+
...and if we call `try_expanding_sum_type_to_union(Union[Color, Status], 'module.Color')`,
959959
this function will return Literal[Color.RED, Color.BLUE, Color.YELLOW, Status].
960960
"""
961961
typ = get_proper_type(typ)

‎test-data/unit/check-enum.test

+34
Original file line numberDiff line numberDiff line change
@@ -1933,6 +1933,18 @@ class D(C): # E: Cannot extend enum with existing members: "C" \
19331933
x: int # E: Cannot assign to final name "x"
19341934
[builtins fixtures/bool.pyi]
19351935

1936+
[case testEnumNotFinalWithMethodsAndUninitializedValuesStubMember]
1937+
# flags: --python-version 3.11
1938+
# This was added in 3.11
1939+
import lib
1940+
1941+
[file lib.pyi]
1942+
from enum import Enum, member
1943+
class A(Enum):
1944+
@member
1945+
def x(self) -> None: ...
1946+
[builtins fixtures/bool.pyi]
1947+
19361948
[case testEnumLiteralValues]
19371949
from enum import Enum
19381950

@@ -2325,6 +2337,28 @@ def some_a(a: A):
23252337
[builtins fixtures/dict.pyi]
23262338

23272339

2340+
[case testEnumMemberAndNonMemberSupport]
2341+
# flags: --python-version 3.11 --warn-unreachable
2342+
# This was added in 3.11
2343+
from enum import Enum, member, nonmember
2344+
2345+
class A(Enum):
2346+
x = 1
2347+
y = member(2)
2348+
z = nonmember(3)
2349+
2350+
def some_a(a: A):
2351+
if a is not A.x and a is not A.z:
2352+
reveal_type(a) # N: Revealed type is "Literal[__main__.A.y]"
2353+
if a is not A.y and a is not A.z:
2354+
reveal_type(a) # N: Revealed type is "Literal[__main__.A.x]"
2355+
if a is not A.x:
2356+
reveal_type(a) # N: Revealed type is "Literal[__main__.A.y]"
2357+
if a is not A.y:
2358+
reveal_type(a) # N: Revealed type is "Literal[__main__.A.x]"
2359+
[builtins fixtures/dict.pyi]
2360+
2361+
23282362
[case testErrorOnAnnotatedMember]
23292363
from enum import Enum
23302364

0 commit comments

Comments
 (0)
Please sign in to comment.