mirror of
https://github.com/python/cpython.git
synced 2025-09-26 10:19:53 +00:00
gh-85795: Raise a clear error when super()
is used in typing.NamedTuple
subclasses (#130082)
This commit is contained in:
parent
5e73ece95e
commit
293fa3433e
4 changed files with 27 additions and 0 deletions
|
@ -2398,6 +2398,10 @@ types.
|
|||
.. versionchanged:: 3.11
|
||||
Added support for generic namedtuples.
|
||||
|
||||
.. versionchanged:: next
|
||||
Using :func:`super` (and the ``__class__`` :term:`closure variable`) in methods of ``NamedTuple`` subclasses
|
||||
is unsupported and causes a :class:`TypeError`.
|
||||
|
||||
.. deprecated-removed:: 3.13 3.15
|
||||
The undocumented keyword argument syntax for creating NamedTuple classes
|
||||
(``NT = NamedTuple("NT", x=int)``) is deprecated, and will be disallowed
|
||||
|
|
|
@ -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):
|
||||
|
|
|
@ -2889,6 +2889,9 @@ _special = frozenset({'__module__', '__name__', '__annotations__', '__annotate__
|
|||
class NamedTupleMeta(type):
|
||||
def __new__(cls, typename, bases, ns):
|
||||
assert _NamedTuple in bases
|
||||
if "__classcell__" in ns:
|
||||
raise TypeError(
|
||||
"uses of super() and __class__ are unsupported in methods of NamedTuple subclasses")
|
||||
for base in bases:
|
||||
if base is not _NamedTuple and base is not Generic:
|
||||
raise TypeError(
|
||||
|
|
|
@ -0,0 +1,3 @@
|
|||
Using :func:`super` and ``__class__`` :term:`closure variable` in
|
||||
user-defined methods of :class:`typing.NamedTuple` subclasses is now
|
||||
explicitly prohibited at runtime. Contributed by Bartosz Sławecki in :gh:`130082`.
|
Loading…
Add table
Add a link
Reference in a new issue