mirror of
https://github.com/python/cpython.git
synced 2025-10-09 16:34:44 +00:00
bpo-46242: [Enum] better error message for extending Enum
with members (GH-30357)
This commit is contained in:
parent
2402f1e1f8
commit
e674e48ddc
3 changed files with 7 additions and 5 deletions
|
@ -763,7 +763,7 @@ class EnumType(type):
|
||||||
"""
|
"""
|
||||||
metacls = cls.__class__
|
metacls = cls.__class__
|
||||||
bases = (cls, ) if type is None else (type, cls)
|
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)
|
classdict = metacls.__prepare__(class_name, bases)
|
||||||
|
|
||||||
# special processing needed for names?
|
# special processing needed for names?
|
||||||
|
@ -848,8 +848,8 @@ class EnumType(type):
|
||||||
% (class_name, base.__name__)
|
% (class_name, base.__name__)
|
||||||
)
|
)
|
||||||
|
|
||||||
@staticmethod
|
@classmethod
|
||||||
def _get_mixins_(class_name, bases):
|
def _get_mixins_(cls, class_name, bases):
|
||||||
"""
|
"""
|
||||||
Returns the type for creating enum members, and the first inherited
|
Returns the type for creating enum members, and the first inherited
|
||||||
enum class.
|
enum class.
|
||||||
|
@ -890,9 +890,8 @@ class EnumType(type):
|
||||||
if not issubclass(first_enum, Enum):
|
if not issubclass(first_enum, Enum):
|
||||||
raise TypeError("new enumerations should be created as "
|
raise TypeError("new enumerations should be created as "
|
||||||
"`EnumName([mixin_type, ...] [data_type,] enum_type)`")
|
"`EnumName([mixin_type, ...] [data_type,] enum_type)`")
|
||||||
|
cls._check_for_existing_members(class_name, bases)
|
||||||
member_type = _find_data_type(bases) or object
|
member_type = _find_data_type(bases) or object
|
||||||
if first_enum._member_names_:
|
|
||||||
raise TypeError("Cannot extend enumerations")
|
|
||||||
return member_type, first_enum
|
return member_type, first_enum
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
|
|
|
@ -1433,6 +1433,8 @@ class TestEnum(unittest.TestCase):
|
||||||
with self.assertRaisesRegex(TypeError, "EvenMoreColor: cannot extend enumeration 'Color'"):
|
with self.assertRaisesRegex(TypeError, "EvenMoreColor: cannot extend enumeration 'Color'"):
|
||||||
class EvenMoreColor(Color, IntEnum):
|
class EvenMoreColor(Color, IntEnum):
|
||||||
chartruese = 7
|
chartruese = 7
|
||||||
|
with self.assertRaisesRegex(TypeError, "Foo: cannot extend enumeration 'Color'"):
|
||||||
|
Color('Foo', ('pink', 'black'))
|
||||||
|
|
||||||
def test_exclude_methods(self):
|
def test_exclude_methods(self):
|
||||||
class whatever(Enum):
|
class whatever(Enum):
|
||||||
|
|
|
@ -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__`.
|
Loading…
Add table
Add a link
Reference in a new issue