diff options
author | Ethan Furman <ethan@stoneleaf.us> | 2023-08-31 12:45:12 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-08-31 12:45:12 -0700 |
commit | d48760b2f1e28dd3c1a35721939f400a8ab619b8 (patch) | |
tree | cf8dbe4f3bf0019e5359918e92fee16827a6b93a /Lib/enum.py | |
parent | 13a00078b81776b23b0b6add69b848382240d1f2 (diff) | |
download | cpython-d48760b2f1e28dd3c1a35721939f400a8ab619b8.tar.gz cpython-d48760b2f1e28dd3c1a35721939f400a8ab619b8.zip |
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.
Diffstat (limited to 'Lib/enum.py')
-rw-r--r-- | Lib/enum.py | 7 |
1 files changed, 7 insertions, 0 deletions
diff --git a/Lib/enum.py b/Lib/enum.py index 0c985b2c778..4b99e7bda2c 100644 --- a/Lib/enum.py +++ b/Lib/enum.py @@ -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 |