mirror of
https://github.com/python/cpython.git
synced 2025-07-07 19:35:27 +00:00
gh-108682: [Enum] raise TypeError if super().__new__ called in custom __new__ (GH-108704)
When overriding the `__new__` method of an enum, the underlying data type should be created directly; i.e. . member = object.__new__(cls) member = int.__new__(cls, value) member = str.__new__(cls, value) Calling `super().__new__()` finds the lookup version of `Enum.__new__`, and will now raise an exception when detected.
This commit is contained in:
parent
13a00078b8
commit
d48760b2f1
4 changed files with 260 additions and 56 deletions
|
@ -856,6 +856,8 @@ class EnumType(type):
|
|||
value = first_enum._generate_next_value_(name, start, count, last_values[:])
|
||||
last_values.append(value)
|
||||
names.append((name, value))
|
||||
if names is None:
|
||||
names = ()
|
||||
|
||||
# Here, names is either an iterable of (name, value) or a mapping.
|
||||
for item in names:
|
||||
|
@ -1112,6 +1114,11 @@ class Enum(metaclass=EnumType):
|
|||
for member in cls._member_map_.values():
|
||||
if member._value_ == value:
|
||||
return member
|
||||
# still not found -- verify that members exist, in-case somebody got here mistakenly
|
||||
# (such as via super when trying to override __new__)
|
||||
if not cls._member_map_:
|
||||
raise TypeError("%r has no members defined" % cls)
|
||||
#
|
||||
# still not found -- try _missing_ hook
|
||||
try:
|
||||
exc = None
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue