[Python-checkins] bpo-41889: [Enum] fix multiple-inheritance regression (GH-22487) (GH-23673)

ethanfurman webhook-mailer at python.org
Mon Dec 7 18:51:20 EST 2020


https://github.com/python/cpython/commit/be52ca45d9eb9c3e8941e46ddc67a210f0f4c6fa
commit: be52ca45d9eb9c3e8941e46ddc67a210f0f4c6fa
branch: 3.9
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: ethanfurman <ethan at stoneleaf.us>
date: 2020-12-07T15:51:16-08:00
summary:

bpo-41889: [Enum] fix multiple-inheritance regression (GH-22487) (GH-23673)

(cherry picked from commit c266736ec1f9ebef38b134ceb4832df015711b38)

files:
A Misc/NEWS.d/next/Library/2020-10-01-16-17-11.bpo-41889.qLkNh8.rst
M Lib/enum.py
M Lib/test/test_enum.py

diff --git a/Lib/enum.py b/Lib/enum.py
index ebadd9f662a13..3ce205d48fc2a 100644
--- a/Lib/enum.py
+++ b/Lib/enum.py
@@ -145,8 +145,9 @@ def __new__(metacls, cls, bases, classdict):
         for key in ignore:
             classdict.pop(key, None)
         member_type, first_enum = metacls._get_mixins_(cls, bases)
-        __new__, save_new, use_args = metacls._find_new_(classdict, member_type,
-                                                        first_enum)
+        __new__, save_new, use_args = metacls._find_new_(
+                classdict, member_type, first_enum,
+                )
 
         # save enum items into separate mapping so they don't get baked into
         # the new class
@@ -500,12 +501,16 @@ def _find_data_type(bases):
                 for base in chain.__mro__:
                     if base is object:
                         continue
+                    elif issubclass(base, Enum):
+                        if base._member_type_ is not object:
+                            data_types.append(base._member_type_)
+                            break
                     elif '__new__' in base.__dict__:
                         if issubclass(base, Enum):
                             continue
                         data_types.append(candidate or base)
                         break
-                    elif not issubclass(base, Enum):
+                    else:
                         candidate = base
             if len(data_types) > 1:
                 raise TypeError('%r: too many data types: %r' % (class_name, data_types))
diff --git a/Lib/test/test_enum.py b/Lib/test/test_enum.py
index 0e6b1b16ca323..cd73d26e63e22 100644
--- a/Lib/test/test_enum.py
+++ b/Lib/test/test_enum.py
@@ -1998,6 +1998,32 @@ class Decision2(MyEnum):
             REVERT_ALL = "REVERT_ALL"
             RETRY = "RETRY"
 
+    def test_multiple_mixin_inherited(self):
+        class MyInt(int):
+            def __new__(cls, value):
+                return super().__new__(cls, value)
+
+        class HexMixin:
+            def __repr__(self):
+                return hex(self)
+
+        class MyIntEnum(HexMixin, MyInt, enum.Enum):
+            pass
+
+        class Foo(MyIntEnum):
+            TEST = 1
+        self.assertTrue(isinstance(Foo.TEST, MyInt))
+        self.assertEqual(repr(Foo.TEST), "0x1")
+
+        class Fee(MyIntEnum):
+            TEST = 1
+            def __new__(cls, value):
+                value += 1
+                member = int.__new__(cls, value)
+                member._value_ = value
+                return member
+        self.assertEqual(Fee.TEST, 2)
+
     def test_empty_globals(self):
         # bpo-35717: sys._getframe(2).f_globals['__name__'] fails with KeyError
         # when using compile and exec because f_globals is empty
diff --git a/Misc/NEWS.d/next/Library/2020-10-01-16-17-11.bpo-41889.qLkNh8.rst b/Misc/NEWS.d/next/Library/2020-10-01-16-17-11.bpo-41889.qLkNh8.rst
new file mode 100644
index 0000000000000..768865ae62116
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2020-10-01-16-17-11.bpo-41889.qLkNh8.rst
@@ -0,0 +1 @@
+Enum: fix regression involving inheriting a multiply-inherited enum



More information about the Python-checkins mailing list