Skip to content

Commit be52ca4

Browse files
bpo-41889: [Enum] fix multiple-inheritance regression (GH-22487) (GH-23673)
(cherry picked from commit c266736)
1 parent 60463e8 commit be52ca4

File tree

3 files changed

+35
-3
lines changed

3 files changed

+35
-3
lines changed

‎Lib/enum.py‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,9 @@ def __new__(metacls, cls, bases, classdict):
145145
for key in ignore:
146146
classdict.pop(key, None)
147147
member_type, first_enum = metacls._get_mixins_(cls, bases)
148-
__new__, save_new, use_args = metacls._find_new_(classdict, member_type,
149-
first_enum)
148+
__new__, save_new, use_args = metacls._find_new_(
149+
classdict, member_type, first_enum,
150+
)
150151

151152
# save enum items into separate mapping so they don't get baked into
152153
# the new class
@@ -500,12 +501,16 @@ def _find_data_type(bases):
500501
for base in chain.__mro__:
501502
if base is object:
502503
continue
504+
elif issubclass(base, Enum):
505+
if base._member_type_ is not object:
506+
data_types.append(base._member_type_)
507+
break
503508
elif '__new__' in base.__dict__:
504509
if issubclass(base, Enum):
505510
continue
506511
data_types.append(candidate or base)
507512
break
508-
elif not issubclass(base, Enum):
513+
else:
509514
candidate = base
510515
if len(data_types) > 1:
511516
raise TypeError('%r: too many data types: %r' % (class_name, data_types))

‎Lib/test/test_enum.py‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1998,6 +1998,32 @@ class Decision2(MyEnum):
19981998
REVERT_ALL = "REVERT_ALL"
19991999
RETRY = "RETRY"
20002000

2001+
def test_multiple_mixin_inherited(self):
2002+
class MyInt(int):
2003+
def __new__(cls, value):
2004+
return super().__new__(cls, value)
2005+
2006+
class HexMixin:
2007+
def __repr__(self):
2008+
return hex(self)
2009+
2010+
class MyIntEnum(HexMixin, MyInt, enum.Enum):
2011+
pass
2012+
2013+
class Foo(MyIntEnum):
2014+
TEST = 1
2015+
self.assertTrue(isinstance(Foo.TEST, MyInt))
2016+
self.assertEqual(repr(Foo.TEST), "0x1")
2017+
2018+
class Fee(MyIntEnum):
2019+
TEST = 1
2020+
def __new__(cls, value):
2021+
value += 1
2022+
member = int.__new__(cls, value)
2023+
member._value_ = value
2024+
return member
2025+
self.assertEqual(Fee.TEST, 2)
2026+
20012027
def test_empty_globals(self):
20022028
# bpo-35717: sys._getframe(2).f_globals['__name__'] fails with KeyError
20032029
# when using compile and exec because f_globals is empty
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Enum: fix regression involving inheriting a multiply-inherited enum

0 commit comments

Comments
 (0)