8000 Add `StrEnum` support for Python 3.11 (#12035) · python/mypy@37d58c3 · GitHub
[go: up one dir, main page]

Skip to content

Commit 37d58c3

Browse files
authored
Add StrEnum support for Python 3.11 (#12035)
1 parent 0647570 commit 37d58c3

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

mypy/semanal_enum.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
# Note: 'enum.EnumMeta' is deliberately excluded from this list. Classes that directly use
1919
# enum.EnumMeta do not necessarily automatically have the 'name' and 'value' attributes.
2020
ENUM_BASES: Final = frozenset((
21-
'enum.Enum', 'enum.IntEnum', 'enum.Flag', 'enum.IntFlag',
21+
'enum.Enum', 'enum.IntEnum', 'enum.Flag', 'enum.IntFlag', 'enum.StrEnum',
2222
))
2323
ENUM_SPECIAL_PROPS: Final = frozenset((
2424
'name', 'value', '_name_', '_value_', '_order_', '__order__',

test-data/unit/check-enum.test

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,20 @@ an_enum = SomeIntEnum.x
232232
an_enum = returns_some_int_enum()
233233
[out]
234234

235+
[case testStrEnumCreation]
236+
# flags: --python-version 3.11
237+
from enum import StrEnum
238+
239+
class MyStrEnum(StrEnum):
240+
x = 'x'
241+
y = 'y'
242+
243+
reveal_type(MyStrEnum.x) # N: Revealed type is "Literal[__main__.MyStrEnum.x]?"
244+
reveal_type(MyStrEnum.x.value) # N: Revealed type is "Literal['x']?"
245+
reveal_type(MyStrEnum.y) # N: Revealed type is "Literal[__main__.MyStrEnum.y]?"
246+
reveal_type(MyStrEnum.y.value) # N: Revealed type is "Literal['y']?"
247+
[out]
248+
235249
[case testEnumMethods]
236250
from enum import Enum
237251

test-data/unit/lib-stub/enum.pyi

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,8 @@ class IntFlag(int, Flag):
4343

4444
class auto(IntFlag):
4545
value: Any
46+
47+
48+
# It is python-3.11+ only:
49+
class StrEnum(str, Enum):
50+
def __new__(cls: Type[_T], value: str | _T) -> _T: ...

0 commit comments

Comments
 (0)
0