mirror of https://github.com/python/cpython
gh-102558: [Enum] better handling of non-Enum EnumType classes (GH-103060)
This commit is contained in:
parent
b838d80085
commit
f4ed2c6ae5
12
Lib/enum.py
12
Lib/enum.py
|
@ -928,7 +928,7 @@ class EnumType(type):
|
|||
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(
|
||||
"<enum %r> cannot extend %r"
|
||||
% (class_name, base)
|
||||
|
@ -947,7 +947,7 @@ class EnumType(type):
|
|||
# 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
|
||||
|
@ -959,7 +959,7 @@ class EnumType(type):
|
|||
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__:
|
||||
|
@ -985,12 +985,12 @@ class EnumType(type):
|
|||
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
|
||||
|
@ -1191,8 +1191,6 @@ class Enum(metaclass=EnumType):
|
|||
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_))
|
||||
|
||||
|
|
|
@ -1386,7 +1386,6 @@ class TestSpecial(unittest.TestCase):
|
|||
class Huh(MyStr, MyInt, Enum):
|
||||
One = 1
|
||||
|
||||
|
||||
def test_pickle_enum(self):
|
||||
if isinstance(Stooges, Exception):
|
||||
raise Stooges
|
||||
|
|
Loading…
Reference in New Issue