]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-46242: [Enum] better error message for extending `Enum` with members (GH-30357)
authorNikita Sobolev <mail@sobolevn.me>
Fri, 14 Jan 2022 22:18:00 +0000 (01:18 +0300)
committerGitHub <noreply@github.com>
Fri, 14 Jan 2022 22:18:00 +0000 (14:18 -0800)
Lib/enum.py
Lib/test/test_enum.py
Misc/NEWS.d/next/Library/2022-01-03-16-25-06.bpo-46242.f4l_CL.rst [new file with mode: 0644]

index 86928b4f79f0b3f7bb9162072ff2543c28e128b3..93ea1bea36db7ffaef8554ccb6254ea61a771549 100644 (file)
@@ -763,7 +763,7 @@ class EnumType(type):
         """
         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 @@ class EnumType(type):
                             % (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 @@ class EnumType(type):
         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
index 04a68cc9ea2044d498c5fe0915d3e9e2d6f8b519..43f98c1c1efb613cf679d98aa1c2f65faf93cde6 100644 (file)
@@ -1433,6 +1433,8 @@ class TestEnum(unittest.TestCase):
         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 (file)
index 0000000..6a5b5fd
--- /dev/null
@@ -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__`.