From 06a70c4295d7e00bcbb7995feb65ccfa57d7f203 Mon Sep 17 00:00:00 2001 From: Ethan Furman Date: Mon, 27 Mar 2023 16:26:16 -0700 Subject: [PATCH] gh-102558: [Enum] better handling of non-Enum EnumType classes (GH-103060) (cherry picked from commit f4ed2c6ae5915329e49b9f94033ef182400e29fa) Co-authored-by: Ethan Furman --- Lib/enum.py | 12 +++++------- Lib/test/test_enum.py | 1 - 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/Lib/enum.py b/Lib/enum.py index ea55f7bbef8637..76575500e42403 100644 --- a/Lib/enum.py +++ b/Lib/enum.py @@ -936,7 +936,7 @@ def _convert_(cls, name, module, filter, source=None, *, boundary=None, as_globa def _check_for_existing_members_(mcls, class_name, bases): for chain in bases: for base in chain.__mro__: - if issubclass(base, Enum) and base._member_names_: + if isinstance(base, EnumType) and base._member_names_: raise TypeError( " cannot extend %r" % (class_name, base) @@ -958,7 +958,7 @@ def _get_mixins_(mcls, class_name, bases): # ensure final parent class is an Enum derivative, find any concrete # data type, and check that Enum has no members first_enum = bases[-1] - if not issubclass(first_enum, Enum): + if not isinstance(first_enum, EnumType): raise TypeError("new enumerations should be created as " "`EnumName([mixin_type, ...] [data_type,] enum_type)`") member_type = mcls._find_data_type_(class_name, bases) or object @@ -970,7 +970,7 @@ def _find_data_repr_(mcls, class_name, bases): for base in chain.__mro__: if base is object: continue - elif issubclass(base, Enum): + elif isinstance(base, EnumType): # if we hit an Enum, use it's _value_repr_ return base._value_repr_ elif '__repr__' in base.__dict__: @@ -988,12 +988,12 @@ def _find_data_type_(mcls, class_name, bases): base_chain.add(base) if base is object: continue - elif issubclass(base, Enum): + elif isinstance(base, EnumType): if base._member_type_ is not object: data_types.add(base._member_type_) break elif '__new__' in base.__dict__ or '__init__' in base.__dict__: - if issubclass(base, Enum): + if isinstance(base, EnumType): continue data_types.add(candidate or base) break @@ -1187,8 +1187,6 @@ def _missing_(cls, value): return None def __repr__(self): - if not isinstance(self, Enum): - return repr(self) v_repr = self.__class__._value_repr_ or repr return "<%s.%s: %s>" % (self.__class__.__name__, self._name_, v_repr(self._value_)) diff --git a/Lib/test/test_enum.py b/Lib/test/test_enum.py index 2a305e415cd9e2..e9dae29d26872d 100644 --- a/Lib/test/test_enum.py +++ b/Lib/test/test_enum.py @@ -1321,7 +1321,6 @@ def repr(self): class Huh(MyStr, MyInt, Enum): One = 1 - def test_pickle_enum(self): if isinstance(Stooges, Exception): raise Stooges