Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Raise TypeError instead of RuntimeError if __classcell__ is use…
…d in typed namedtuples
  • Loading branch information
johnslavik committed Mar 4, 2025
commit 8f2ebc8147e7d4ab70165fbc2032e6a054e1f1bd
8 changes: 4 additions & 4 deletions Doc/library/typing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2359,10 +2359,6 @@ types.
# A functional syntax is also supported
Employee = NamedTuple('Employee', [('name', str), ('id', int)])

.. note::
Using :func:`super` (and the ``__class__`` :term:`closure variable`) in methods of ``NamedTuple`` subclasses
is unsupported and causes a :class:`RuntimeError`.

.. versionchanged:: 3.6
Added support for :pep:`526` variable annotation syntax.

Expand All @@ -2380,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
Comment thread
johnslavik marked this conversation as resolved.
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
4 changes: 2 additions & 2 deletions Lib/test/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -8365,12 +8365,12 @@ def test_super_explicitly_disallowed(self):
"in methods of NamedTuple subclasses"
)

with self.assertRaises(RuntimeError, msg=expected_message):
with self.assertRaises(TypeError, msg=expected_message):
class ThisWontWork(NamedTuple):
def __repr__(self):
return super().__repr__()

with self.assertRaises(RuntimeError, msg=expected_message):
with self.assertRaises(TypeError, msg=expected_message):
class ThisWontWorkEither(NamedTuple):
@property
def name(self):
Expand Down
2 changes: 1 addition & 1 deletion Lib/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2962,7 +2962,7 @@ class NamedTupleMeta(type):
def __new__(cls, typename, bases, ns):
assert _NamedTuple in bases
if "__classcell__" in ns:
raise RuntimeError(
raise TypeError(
"uses of super() and __class__ are unsupported in methods of NamedTuple subclasses")
Comment thread
johnslavik marked this conversation as resolved.
for base in bases:
if base is not _NamedTuple and base is not Generic:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Using :func:`super` and ``__class__`` :term:`closure variable` in
user-defined methods of :class:`typing.NamedTuple` subclasses is now
explicitly prevented at runtime.
explicitly prohibited at runtime.