Skip to content

Commit 3e29e05

Browse files
authored
Experiment: remove IntFlag from enum.auto (#12760)
comments in #10384 suggest that type checkers should special case enum.auto rather than relying on the IntFlag hack. It's been a while since then, do we still need it?
1 parent 4b11151 commit 3e29e05

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

stdlib/@tests/stubtest_allowlists/common.txt

+5
Original file line numberDiff line numberDiff line change
@@ -670,3 +670,8 @@ typing(_extensions)?\.IO\.__iter__ # See https://github.com/python/typeshed/com
670670
(xml.etree.cElementTree.XMLPullParser.flush)?
671671
(xml.parsers.expat.XMLParserType.GetReparseDeferralEnabled)?
672672
(xml.parsers.expat.XMLParserType.SetReparseDeferralEnabled)?
673+
674+
# enum.auto is magic, see comments
675+
enum.auto.__or__
676+
enum.auto.__and__
677+
enum.auto.__xor__

stdlib/enum.pyi

+13-2
Original file line numberDiff line numberDiff line change
@@ -316,13 +316,24 @@ else:
316316
__rand__ = __and__
317317
__rxor__ = __xor__
318318

319-
# subclassing IntFlag so it picks up all implemented base functions, best modeling behavior of enum.auto()
320-
class auto(IntFlag):
319+
class auto:
321320
_value_: Any
322321
@_magic_enum_attr
323322
def value(self) -> Any: ...
324323
def __new__(cls) -> Self: ...
325324

325+
# These don't exist, but auto is basically immediately replaced with
326+
# either an int or a str depending on the type of the enum. StrEnum's auto
327+
# shouldn't have these, but they're needed for int versions of auto (mostly the __or__).
328+
# Ideally type checkers would special case auto enough to handle this,
329+
# but until then this is a slightly inaccurate helping hand.
330+
def __or__(self, other: int | Self) -> Self: ...
331+
def __and__(self, other: int | Self) -> Self: ...
332+
def __xor__(self, other: int | Self) -> Self: ...
333+
__ror__ = __or__
334+
__rand__ = __and__
335+
__rxor__ = __xor__
336+
326337
if sys.version_info >= (3, 11):
327338
def pickle_by_global_name(self: Enum, proto: int) -> str: ...
328339
def pickle_by_enum_name(self: _EnumMemberT, proto: int) -> tuple[Callable[..., Any], tuple[type[_EnumMemberT], str]]: ...

0 commit comments

Comments
 (0)