[3.11] gh-108682: [Enum] raise TypeError if super().__new__ called in custom __new__ (GH-108704) (GH-108739)

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.

(cherry picked from commit d48760b2f1)
This commit is contained in:
Ethan Furman 2023-09-07 18:57:48 -07:00 committed by GitHub
parent e46be0d2fa
commit effa2ecdcf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 42 additions and 1 deletions

View file

@ -322,6 +322,17 @@ def spam(cls):
with self.assertRaises(AttributeError):
del Season.SPRING.name
def test_bad_new_super(self):
with self.assertRaisesRegex(
TypeError,
'has no members defined',
):
class BadSuper(self.enum_type):
def __new__(cls, value):
obj = super().__new__(cls, value)
return obj
failed = 1
def test_basics(self):
TE = self.MainEnum
if self.is_flag: