8000 bpo-46242: better error message for extending `Enum` with members by sobolevn · Pull Request #30357 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

bpo-46242: better error message for extending Enum with members #30357

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to fi 8000 le
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions Lib/enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions Lib/test/test_enum.py
8017
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
Original file line number Diff line number Diff line change
@@ -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__`.
0