Skip to content

Commit b6da4fc

Browse files
committed
Allow enum members to have type objects as values (#19160)
Type objects as enum values are supported at runtime. Fixes #19151.
1 parent 334469f commit b6da4fc

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
@@ -3313,8 +3313,8 @@ def enum_members(self) -> list[str]:
33133313
continue # unannotated value not a member
33143314

33153315
typ = mypy.types.get_proper_type(sym.node.type)
3316-
if isinstance(
3317-
typ, mypy.types.FunctionLike
3316+
if (
3317+
isinstance(typ, mypy.types.FunctionLike) and not typ.is_type_obj()
33183318
) or ( # explicit `@member` is required
33193319
isinstance(typ, mypy.types.Instance)
33203320
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)