[Python-checkins] bpo-46242: [Enum] better error message for extending `Enum` with members (GH-30357)

ethanfurman webhook-mailer at python.org
Fri Jan 14 17:18:23 EST 2022


https://github.com/python/cpython/commit/e674e48ddc2712f28cc7ecdc66a6c328066694b0
commit: e674e48ddc2712f28cc7ecdc66a6c328066694b0
branch: main
author: Nikita Sobolev <mail at sobolevn.me>
committer: ethanfurman <ethan at stoneleaf.us>
date: 2022-01-14T14:18:00-08:00
summary:

bpo-46242: [Enum] better error message for extending `Enum` with members (GH-30357)

files:
A Misc/NEWS.d/next/Library/2022-01-03-16-25-06.bpo-46242.f4l_CL.rst
M Lib/enum.py
M Lib/test/test_enum.py

diff --git a/Lib/enum.py b/Lib/enum.py
index 86928b4f79f0b..93ea1bea36db7 100644
--- a/Lib/enum.py
+++ b/Lib/enum.py
@@ -763,7 +763,7 @@ def _create_(cls, class_name, names, *, module=None, qualname=None, type=None, s
         """
         metacls = cls.__class__
         bases = (cls, ) if type is None else (type, cls)
-        _, first_enum = cls._get_mixins_(cls, bases)
+        _, first_enum = cls._get_mixins_(class_name, bases)
         classdict = metacls.__prepare__(class_name, bases)
 
         # special processing needed for names?
@@ -848,8 +848,8 @@ def _check_for_existing_members(class_name, bases):
                             % (class_name, base.__name__)
                             )
 
-    @staticmethod
-    def _get_mixins_(class_name, bases):
+    @classmethod
+    def _get_mixins_(cls, class_name, bases):
         """
         Returns the type for creating enum members, and the first inherited
         enum class.
@@ -890,9 +890,8 @@ def _find_data_type(bases):
         if not issubclass(first_enum, Enum):
             raise TypeError("new enumerations should be created as "
                     "`EnumName([mixin_type, ...] [data_type,] enum_type)`")
+        cls._check_for_existing_members(class_name, bases)
         member_type = _find_data_type(bases) or object
-        if first_enum._member_names_:
-            raise TypeError("Cannot extend enumerations")
         return member_type, first_enum
 
     @staticmethod
diff --git a/Lib/test/test_enum.py b/Lib/test/test_enum.py
index 04a68cc9ea204..43f98c1c1efb6 100644
--- a/Lib/test/test_enum.py
+++ b/Lib/test/test_enum.py
@@ -1433,6 +1433,8 @@ class MoreColor(Color):
         with self.assertRaisesRegex(TypeError, "EvenMoreColor: cannot extend enumeration 'Color'"):
             class EvenMoreColor(Color, IntEnum):
                 chartruese = 7
+        with self.assertRaisesRegex(TypeError, "Foo: cannot extend enumeration 'Color'"):
+            Color('Foo', ('pink', 'black'))
 
     def test_exclude_methods(self):
         class whatever(Enum):
diff --git a/Misc/NEWS.d/next/Library/2022-01-03-16-25-06.bpo-46242.f4l_CL.rst b/Misc/NEWS.d/next/Library/2022-01-03-16-25-06.bpo-46242.f4l_CL.rst
new file mode 100644
index 0000000000000..6a5b5fdffda40
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2022-01-03-16-25-06.bpo-46242.f4l_CL.rst
@@ -0,0 +1 @@
+Improve error message when creating a new :class:`enum.Enum` type subclassing an existing ``Enum`` with ``_member_names_`` using :meth:`enum.Enum.__call__`.



More information about the Python-checkins mailing list