diff --git a/Lib/enum.py b/Lib/enum.py index 8efc38c3d78dbc..ad24fa118f6fa1 100644 --- a/Lib/enum.py +++ b/Lib/enum.py @@ -767,7 +767,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? @@ -852,8 +852,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. @@ -894,9 +894,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 eecb9fd4835c40..22737700fa3103 100644 --- a/Lib/test/test_enum.py +++ b/Lib/test/test_enum.py @@ -1423,6 +1423,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 00000000000000..6a5b5fdffda40b --- /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__`.