Skip to content

Commit 568a638

Browse files
authored
Allow enum members to have type objects as values (#19160)
Type objects as enum values are supported at runtime. Fixes #19151.
1 parent 537fc55 commit 568a638

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

mypy/nodes.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3301,8 +3301,8 @@ def enum_members(self) -> list[str]:
33013301
continue # unannotated value not a member
33023302

33033303
typ = mypy.types.get_proper_type(sym.node.type)
3304-
if isinstance(
3305-
typ, mypy.types.FunctionLike
3304+
if (
3305+
isinstance(typ, mypy.types.FunctionLike) and not typ.is_type_obj()
33063306
) or ( # explicit `@member` is required
33073307
isinstance(typ, mypy.types.Instance)
33083308
and typ.type.fullname == "enum.nonmember"

test-data/unit/check-python310.test

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2638,3 +2638,24 @@ def f2() -> None:
26382638
return
26392639
reveal_type(y) # N: Revealed type is "builtins.str"
26402640
[builtins fixtures/list.pyi]
2641+
2642+
[case testEnumTypeObjectMember]
2643+
import enum
2644+
from typing import NoReturn
2645+
2646+
def assert_never(x: NoReturn) -> None: ...
2647+
2648+
class ValueType(enum.Enum):
2649+
INT = int
2650+
STR = str
2651+
2652+
value_type: ValueType = ValueType.INT
2653+
2654+
match value_type:
2655+
case ValueType.INT:
2656+
pass
2657+
case ValueType.STR:
2658+
pass
2659+
case _:
2660+
assert_never(value_type)
2661+
[builtins fixtures/tuple.pyi]

0 commit comments

Comments
 (0)