Expand file tree Collapse file tree 3 files changed +35
-3
lines changed Original file line number Diff line number Diff line change @@ -146,8 +146,9 @@ def __new__(metacls, cls, bases, classdict):
146
146
for key in ignore :
147
147
classdict .pop (key , None )
148
148
member_type , first_enum = metacls ._get_mixins_ (cls , bases )
149
- __new__ , save_new , use_args = metacls ._find_new_ (classdict , member_type ,
150
- first_enum )
149
+ __new__ , save_new , use_args = metacls ._find_new_ (
150
+ classdict , member_type , first_enum ,
151
+ )
151
152
152
153
# save enum items into separate mapping so they don't get baked into
153
154
# the new class
@@ -501,12 +502,16 @@ def _find_data_type(bases):
501
502
for base in chain .__mro__ :
502
503
if base is object :
503
504
continue
505
+ elif issubclass (base , Enum ):
506
+ if base ._member_type_ is not object :
507
+ data_types .append (base ._member_type_ )
508
+ break
504
509
elif '__new__' in base .__dict__ :
505
510
if issubclass (base , Enum ):
506
511
continue
507
512
data_types .append (candidate or base )
508
513
break
509
- elif not issubclass ( base , Enum ) :
514
+ else :
510
515
candidate = base
511
516
if len (data_types ) > 1 :
512
517
raise TypeError ('%r: too many data types: %r' % (class_name , data_types ))
Original file line number Diff line number Diff line change @@ -2021,6 +2021,32 @@ class Decision2(MyEnum):
2021
2021
REVERT_ALL = "REVERT_ALL"
2022
2022
RETRY = "RETRY"
2023
2023
2024
+ def test_multiple_mixin_inherited (self ):
2025
+ class MyInt (int ):
2026
+ def __new__ (cls , value ):
2027
+ return super ().__new__ (cls , value )
2028
+
2029
+ class HexMixin :
2030
+ def __repr__ (self ):
2031
+ return hex (self )
2032
+
2033
+ class MyIntEnum (HexMixin , MyInt , enum .Enum ):
2034
+ pass
2035
+
2036
+ class Foo (MyIntEnum ):
2037
+ TEST = 1
2038
+ self .assertTrue (isinstance (Foo .TEST , MyInt ))
2039
+ self .assertEqual (repr (Foo .TEST ), "0x1" )
2040
+
2041
+ class Fee (MyIntEnum ):
2042
+ TEST = 1
2043
+ def __new__ (cls , value ):
2044
+ value += 1
2045
+ member = int .__new__ (cls , value )
2046
+ member ._value_ = value
2047
+ return member
2048
+ self .assertEqual (Fee .TEST , 2 )
2049
+
2024
2050
def test_empty_globals (self ):
2025
2051
# bpo-35717: sys._getframe(2).f_globals['__name__'] fails with KeyError
2026
2052
# when using compile and exec because f_globals is empty
Original file line number Diff line number Diff line change
1
+ Enum: fix regression involving inheriting a multiply-inherited enum
<
32A9
div class="d-flex flex-column gap-2 pt-3 react-comments-container Comment-module__commit-discussion-comments--WaMOe" id="comments">