gh-85795: Raise a clear error when super() is used in typing.NamedTuple subclasses (#130082)

This commit is contained in:
Bartosz Sławecki 2025-03-06 05:45:47 +01:00 committed by GitHub
parent 5e73ece95e
commit 293fa3433e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 27 additions and 0 deletions

View file

@ -8349,6 +8349,23 @@ class NamedTupleTests(BaseTestCase):
class Foo(NamedTuple):
attr = very_annoying
def test_super_explicitly_disallowed(self):
expected_message = (
"uses of super() and __class__ are unsupported "
"in methods of NamedTuple subclasses"
)
with self.assertRaises(TypeError, msg=expected_message):
class ThisWontWork(NamedTuple):
def __repr__(self):
return super().__repr__()
with self.assertRaises(TypeError, msg=expected_message):
class ThisWontWorkEither(NamedTuple):
@property
def name(self):
return __class__.__name__
class TypedDictTests(BaseTestCase):
def test_basics_functional_syntax(self):