mirror of
https://github.com/python/cpython.git
synced 2025-12-08 06:10:17 +00:00
[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:
parent
e46be0d2fa
commit
effa2ecdcf
4 changed files with 42 additions and 1 deletions
|
|
@ -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:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue