@@ -990,9 +990,8 @@ reveal_type(Color.RED._name_) # revealed: Literal["RED"]
990990def _ (red_or_blue : Literal[Color.RED , Color.BLUE ]):
991991 reveal_type(red_or_blue.name) # revealed: Literal["RED", "BLUE"]
992992
993- def _ (any_color : Color):
994- # TODO : Literal["RED", "GREEN", "BLUE"]
995- reveal_type(any_color.name) # revealed: Any
993+ def _ (color : Color):
994+ reveal_type(color.name) # revealed: Literal["RED", "GREEN", "BLUE"]
996995```
997996
998997### ` value ` and ` _value_ `
@@ -1017,6 +1016,9 @@ reveal_type(Color.RED._value_) # revealed: Literal[1]
10171016reveal_type(Color.GREEN .value) # revealed: Literal[2]
10181017reveal_type(Color.GREEN ._value_) # revealed: Literal[2]
10191018
1019+ def _ (color : Color):
1020+ reveal_type(color.value) # revealed: Literal[1, 2, 3]
1021+
10201022class Answer (StrEnum ):
10211023 YES = " yes"
10221024 NO = " no"
@@ -1026,6 +1028,9 @@ reveal_type(Answer.YES._value_) # revealed: Literal["yes"]
10261028
10271029reveal_type(Answer.NO .value) # revealed: Literal["no"]
10281030reveal_type(Answer.NO ._value_) # revealed: Literal["no"]
1031+
1032+ def _ (answer : Answer):
1033+ reveal_type(answer.value) # revealed: Literal["yes", "no"]
10291034```
10301035
10311036## Properties of enum types
@@ -1140,6 +1145,9 @@ python-version = "3.9"
11401145from enum import Enum, EnumMeta
11411146
11421147class EnumWithEnumMetaMetaclass (metaclass = EnumMeta ):
1148+ # Using `EnumMeta` as a metaclass without inheriting `Enum` requires an `__init__`
1149+ # method that will accept member values (TODO we could catch the lack of this):
1150+ def __init__ (self , val ): ...
11431151 NO = 0
11441152 YES = 1
11451153
@@ -1148,24 +1156,37 @@ reveal_type(EnumWithEnumMetaMetaclass.NO) # revealed: Literal[EnumWithEnumMetaM
11481156class SubclassOfEnumMeta (EnumMeta ): ...
11491157
11501158class EnumWithSubclassOfEnumMetaMetaclass (metaclass = SubclassOfEnumMeta ):
1159+ def __init__ (self , val ): ...
11511160 NO = 0
11521161 YES = 1
11531162
11541163reveal_type(EnumWithSubclassOfEnumMetaMetaclass.NO ) # revealed: Literal[EnumWithSubclassOfEnumMetaMetaclass.NO]
11551164
1156- # Attributes like `.value` can *not* be accessed on members of these enums:
1165+ # Attributes `.value` and `.name` can *not* be accessed on members of these enums:
1166+
11571167# error: [unresolved-attribute]
11581168EnumWithSubclassOfEnumMetaMetaclass.NO .value
11591169# error: [unresolved-attribute]
1160- EnumWithSubclassOfEnumMetaMetaclass.NO ._value_
1161- # error: [unresolved-attribute]
11621170EnumWithSubclassOfEnumMetaMetaclass.NO .name
1163- # error: [unresolved-attribute]
1164- EnumWithSubclassOfEnumMetaMetaclass.NO ._name_
1171+
1172+ # But the internal underscore attributes are available:
1173+
1174+ reveal_type(EnumWithSubclassOfEnumMetaMetaclass.NO ._value_) # revealed: Any
1175+ reveal_type(EnumWithSubclassOfEnumMetaMetaclass.NO ._name_) # revealed: Literal["NO"]
1176+
1177+ def _ (x : EnumWithSubclassOfEnumMetaMetaclass):
1178+ # error: [unresolved-attribute]
1179+ x.value
1180+ # error: [unresolved-attribute]
1181+ x.name
1182+ reveal_type(x._value_) # revealed: Any
1183+ reveal_type(x._name_) # revealed: Literal["NO", "YES"]
11651184```
11661185
11671186### Enums with (subclasses of) ` EnumType ` as metaclass
11681187
1188+ In Python 3.11, the meta-type was renamed to ` EnumType ` .
1189+
11691190``` toml
11701191[environment ]
11711192python-version = " 3.11"
@@ -1175,6 +1196,7 @@ python-version = "3.11"
11751196from enum import Enum, EnumType
11761197
11771198class EnumWithEnumMetaMetaclass (metaclass = EnumType ):
1199+ def __init__ (self , val ): ...
11781200 NO = 0
11791201 YES = 1
11801202
@@ -1183,13 +1205,31 @@ reveal_type(EnumWithEnumMetaMetaclass.NO) # revealed: Literal[EnumWithEnumMetaM
11831205class SubclassOfEnumMeta (EnumType ): ...
11841206
11851207class EnumWithSubclassOfEnumMetaMetaclass (metaclass = SubclassOfEnumMeta ):
1208+ def __init__ (self , val ): ...
11861209 NO = 0
11871210 YES = 1
11881211
11891212reveal_type(EnumWithSubclassOfEnumMetaMetaclass.NO ) # revealed: Literal[EnumWithSubclassOfEnumMetaMetaclass.NO]
11901213
1214+ # Attributes `.value` and `.name` can *not* be accessed on members of these enums:
1215+
11911216# error: [unresolved-attribute]
11921217EnumWithSubclassOfEnumMetaMetaclass.NO .value
1218+ # error: [unresolved-attribute]
1219+ EnumWithSubclassOfEnumMetaMetaclass.NO .name
1220+
1221+ # But the internal underscore attributes are available:
1222+
1223+ reveal_type(EnumWithSubclassOfEnumMetaMetaclass.NO ._value_) # revealed: Any
1224+ reveal_type(EnumWithSubclassOfEnumMetaMetaclass.NO ._name_) # revealed: Literal["NO"]
1225+
1226+ def _ (x : EnumWithSubclassOfEnumMetaMetaclass):
1227+ # error: [unresolved-attribute]
1228+ x.value
1229+ # error: [unresolved-attribute]
1230+ x.name
1231+ reveal_type(x._value_) # revealed: Any
1232+ reveal_type(x._name_) # revealed: Literal["NO", "YES"]
11931233```
11941234
11951235## Function syntax
0 commit comments