bpo-44356: [Enum] allow multiple data-type mixins if they are all the same (GH-26649) (GH-26653)

This enables, for example, two base Enums to both inherit from `str`, and then both be mixed into the same final Enum:

    class Str1Enum(str, Enum):
        GH- some behavior here

    class Str2Enum(str, Enum):
        GH- some more behavior here

    class FinalStrEnum(Str1Enum, Str2Enum):
        GH- this now works
(cherry picked from commit 8a4f0850d7)

Co-authored-by: Ethan Furman <ethan@stoneleaf.us>

Co-authored-by: Ethan Furman <ethan@stoneleaf.us>
This commit is contained in:
Miss Islington (bot) 2021-06-10 15:01:03 -07:00 committed by GitHub
parent e3bc32fc1a
commit 01286017c3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 52 additions and 4 deletions

View file

@ -819,7 +819,7 @@ class EnumType(type):
return object, Enum
def _find_data_type(bases):
data_types = []
data_types = set()
for chain in bases:
candidate = None
for base in chain.__mro__:
@ -827,19 +827,19 @@ class EnumType(type):
continue
elif issubclass(base, Enum):
if base._member_type_ is not object:
data_types.append(base._member_type_)
data_types.add(base._member_type_)
break
elif '__new__' in base.__dict__:
if issubclass(base, Enum):
continue
data_types.append(candidate or base)
data_types.add(candidate or base)
break
else:
candidate = base
if len(data_types) > 1:
raise TypeError('%r: too many data types: %r' % (class_name, data_types))
elif data_types:
return data_types[0]
return data_types.pop()
else:
return None