From 3e29e05e4ae28c9c17e2ada8f6f088de40e6d692 Mon Sep 17 00:00:00 2001 From: Stephen Morton Date: Tue, 8 Oct 2024 21:24:31 -0700 Subject: [PATCH] Experiment: remove IntFlag from enum.auto (#12760) comments in https://github.com/python/typeshed/issues/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? --- stdlib/@tests/stubtest_allowlists/common.txt | 5 +++++ stdlib/enum.pyi | 15 +++++++++++++-- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/stdlib/@tests/stubtest_allowlists/common.txt b/stdlib/@tests/stubtest_allowlists/common.txt index 55dbdbae4..e42ffd34b 100644 --- a/stdlib/@tests/stubtest_allowlists/common.txt +++ b/stdlib/@tests/stubtest_allowlists/common.txt @@ -670,3 +670,8 @@ typing(_extensions)?\.IO\.__iter__ # See https://github.com/python/typeshed/com (xml.etree.cElementTree.XMLPullParser.flush)? (xml.parsers.expat.XMLParserType.GetReparseDeferralEnabled)? (xml.parsers.expat.XMLParserType.SetReparseDeferralEnabled)? + +# enum.auto is magic, see comments +enum.auto.__or__ +enum.auto.__and__ +enum.auto.__xor__ diff --git a/stdlib/enum.pyi b/stdlib/enum.pyi index 5c82b07c4..3b6c32552 100644 --- a/stdlib/enum.pyi +++ b/stdlib/enum.pyi @@ -316,13 +316,24 @@ else: __rand__ = __and__ __rxor__ = __xor__ -# subclassing IntFlag so it picks up all implemented base functions, best modeling behavior of enum.auto() -class auto(IntFlag): +class auto: _value_: Any @_magic_enum_attr def value(self) -> Any: ... def __new__(cls) -> Self: ... + # These don't exist, but auto is basically immediately replaced with + # either an int or a str depending on the type of the enum. StrEnum's auto + # shouldn't have these, but they're needed for int versions of auto (mostly the __or__). + # Ideally type checkers would special case auto enough to handle this, + # but until then this is a slightly inaccurate helping hand. + def __or__(self, other: int | Self) -> Self: ... + def __and__(self, other: int | Self) -> Self: ... + def __xor__(self, other: int | Self) -> Self: ... + __ror__ = __or__ + __rand__ = __and__ + __rxor__ = __xor__ + if sys.version_info >= (3, 11): def pickle_by_global_name(self: Enum, proto: int) -> str: ... def pickle_by_enum_name(self: _EnumMemberT, proto: int) -> tuple[Callable[..., Any], tuple[type[_EnumMemberT], str]]: ...