Skip to content

Commit fd7ff19

Browse files
authored
Use StrEnum as base class, when available (#11100)
1 parent b5511ca commit fd7ff19

File tree

7 files changed

+25
-10
lines changed

7 files changed

+25
-10
lines changed

stdlib/_typeshed/__init__.pyi

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,3 +320,12 @@ class DataclassInstance(Protocol):
320320
# Anything that can be passed to the int/float constructors
321321
ConvertibleToInt: TypeAlias = str | ReadableBuffer | SupportsInt | SupportsIndex | SupportsTrunc
322322
ConvertibleToFloat: TypeAlias = str | ReadableBuffer | SupportsFloat | SupportsIndex
323+
324+
# A few classes updated from Foo(str, Enum) to Foo(StrEnum). This is a convenience so these
325+
# can be accurate on all python versions without getting too wordy
326+
if sys.version_info >= (3, 11):
327+
from enum import StrEnum as StrEnum
328+
else:
329+
from enum import Enum
330+
331+
class StrEnum(str, Enum): ...

stdlib/pstats.pyi

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import sys
2-
from _typeshed import StrOrBytesPath
2+
from _typeshed import StrEnum, StrOrBytesPath
33
from collections.abc import Iterable
44
from cProfile import Profile as _cProfile
5-
from enum import Enum
65
from profile import Profile
76
from typing import IO, Any, overload
87
from typing_extensions import Literal, Self, TypeAlias
@@ -14,7 +13,7 @@ else:
1413

1514
_Selector: TypeAlias = str | float | int
1615

17-
class SortKey(str, Enum):
16+
class SortKey(StrEnum):
1817
CALLS: str
1918
CUMULATIVE: str
2019
FILENAME: str

stdlib/tkinter/__init__.pyi

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import _tkinter
22
import sys
3-
from _typeshed import Incomplete, StrOrBytesPath
3+
from _typeshed import Incomplete, StrEnum, StrOrBytesPath
44
from collections.abc import Callable, Mapping, Sequence
5-
from enum import Enum
65
from tkinter.constants import *
76
from tkinter.font import _FontDescription
87
from types import TracebackType
@@ -195,7 +194,7 @@ if sys.version_info >= (3, 11):
195194
releaselevel: str
196195
serial: int
197196

198-
class EventType(str, Enum):
197+
class EventType(StrEnum):
199198
Activate: str
200199
ButtonPress: str
201200
Button = ButtonPress

tests/stubtest_allowlists/py310.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,3 +172,7 @@ pkgutil.ImpImporter\..*
172172
pkgutil.ImpLoader\..*
173173

174174
types.CodeType.replace # stubtest thinks default values are None but None doesn't work at runtime
175+
176+
# These enums derive from (str, Enum)
177+
pstats.SortKey.__new__
178+
tkinter.EventType.__new__

tests/stubtest_allowlists/py38.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,3 +181,7 @@ tkinter.test
181181
tkinter\.test\..+
182182
unittest.test
183183
unittest\.test\..+
184+
185+
# These enums derive from (str, Enum)
186+
pstats.SortKey.__new__
187+
tkinter.EventType.__new__

tests/stubtest_allowlists/py39.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,3 +163,7 @@ pkgutil.ImpImporter\..*
163163
pkgutil.ImpLoader\..*
164164

165165
types.CodeType.replace # stubtest thinks default values are None but None doesn't work at runtime
166+
167+
# These enums derive from (str, Enum)
168+
pstats.SortKey.__new__
169+
tkinter.EventType.__new__

tests/stubtest_allowlists/py3_common.txt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -433,10 +433,6 @@ inspect.Signature.__init__
433433
inspect.Parameter.empty # set as private marker _empty
434434
inspect.Signature.empty # set as private marker _empty
435435

436-
# These enums derive from (int, IntEnum) or (str, Enum)
437-
pstats.SortKey.__new__
438-
tkinter.EventType.__new__
439-
440436
# These multiprocessing proxy methods have *args, **kwargs signatures at runtime,
441437
# But have more precise (accurate) signatures in the stub
442438
multiprocessing.managers.BaseListProxy.__imul__

0 commit comments

Comments
 (0)