8000 gh-85795: Raise a clear error when `super()` is used in `typing.NamedTuple` subclasses by bswck · Pull Request #130082 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Doc/library/typing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2376,6 +2376,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
Expand Down
17 changes: 17 additions & 0 deletions Lib/test/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -8359,6 +8359,23 @@ class VeryAnnoying(metaclass=Meta): pass
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):
Expand Down
3 changes: 3 additions & 0 deletions Lib/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2961,6 +2961,9 @@ def annotate(format):
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(
Expand Down
Original file line number Diff line number Diff line change
@@ -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
0