From a3d23626686edda0d81520023d0f51ed15288170 Mon Sep 17 00:00:00 2001 From: Stephen Morton Date: Tue, 8 Oct 2024 19:51:46 -0700 Subject: [PATCH 1/4] Experiment: remove IntFlag from enum.auto 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/enum.pyi | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/stdlib/enum.pyi b/stdlib/enum.pyi index 5c82b07c4185..d266bd464ed8 100644 --- a/stdlib/enum.pyi +++ b/stdlib/enum.pyi @@ -316,8 +316,7 @@ 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: ... From 4d7e68f9f761e1abced8b672480f9f8c61102b79 Mon Sep 17 00:00:00 2001 From: Stephen Morton Date: Tue, 8 Oct 2024 20:33:02 -0700 Subject: [PATCH 2/4] add dunder methods --- stdlib/enum.pyi | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/stdlib/enum.pyi b/stdlib/enum.pyi index d266bd464ed8..4d778cc398c9 100644 --- a/stdlib/enum.pyi +++ b/stdlib/enum.pyi @@ -321,6 +321,12 @@ class auto: @_magic_enum_attr def value(self) -> Any: ... def __new__(cls) -> Self: ... + 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: ... From f53acd009af50f595cc3532b440cae79185cb810 Mon Sep 17 00:00:00 2001 From: Stephen Morton Date: Tue, 8 Oct 2024 20:52:02 -0700 Subject: [PATCH 3/4] stubtest allowlist and add note --- stdlib/@tests/stubtest_allowlists/common.txt | 4 ++++ stdlib/enum.pyi | 12 +++++++++--- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/stdlib/@tests/stubtest_allowlists/common.txt b/stdlib/@tests/stubtest_allowlists/common.txt index 55dbdbae4b2e..e0fd4050b9b0 100644 --- a/stdlib/@tests/stubtest_allowlists/common.txt +++ b/stdlib/@tests/stubtest_allowlists/common.txt @@ -670,3 +670,7 @@ 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.__and__ +enum.auto.__xor__ diff --git a/stdlib/enum.pyi b/stdlib/enum.pyi index 4d778cc398c9..3dd5171cfd93 100644 --- a/stdlib/enum.pyi +++ b/stdlib/enum.pyi @@ -321,9 +321,15 @@ class auto: @_magic_enum_attr def value(self) -> Any: ... def __new__(cls) -> Self: ... - def __or__(self, other: int | Self) -> Self: ... - def __and__(self, other: int | Self) -> Self: ... - def __xor__(self, other: int | Self) -> 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, value: int | Self, /) -> Self: ... + def __and__(self, value: int | Self, /) -> Self: ... + def __xor__(self, value: int | Self, /) -> Self: ... __ror__ = __or__ __rand__ = __and__ __rxor__ = __xor__ From e80b5fd6823f9c43040578a06e0f1bd2705a62f2 Mon Sep 17 00:00:00 2001 From: Stephen Morton Date: Tue, 8 Oct 2024 20:56:50 -0700 Subject: [PATCH 4/4] I was misled by the presence of object.__or__ on 3.10+, fixing --- stdlib/@tests/stubtest_allowlists/common.txt | 1 + stdlib/enum.pyi | 6 +++--- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/stdlib/@tests/stubtest_allowlists/common.txt b/stdlib/@tests/stubtest_allowlists/common.txt index e0fd4050b9b0..e42ffd34b07b 100644 --- a/stdlib/@tests/stubtest_allowlists/common.txt +++ b/stdlib/@tests/stubtest_allowlists/common.txt @@ -672,5 +672,6 @@ typing(_extensions)?\.IO\.__iter__ # See https://github.com/python/typeshed/com (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 3dd5171cfd93..3b6c325522d7 100644 --- a/stdlib/enum.pyi +++ b/stdlib/enum.pyi @@ -327,9 +327,9 @@ class 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, value: int | Self, /) -> Self: ... - def __and__(self, value: int | Self, /) -> Self: ... - def __xor__(self, value: int | Self, /) -> Self: ... + 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__