Skip to content

Commit 8f2ebc8

Browse files
committed
Raise TypeError instead of RuntimeError if __classcell__ is used in typed namedtuples
1 parent ab6131d commit 8f2ebc8

File tree

4 files changed

+8
-8
lines changed

4 files changed

+8
-8
lines changed

Doc/library/typing.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2359,10 +2359,6 @@ types.
23592359
# A functional syntax is also supported
23602360
Employee = NamedTuple('Employee', [('name', str), ('id', int)])
23612361

2362-
.. note::
2363-
Using :func:`super` (and the ``__class__`` :term:`closure variable`) in methods of ``NamedTuple`` subclasses
2364-
is unsupported and causes a :class:`RuntimeError`.
2365-
23662362
.. versionchanged:: 3.6
23672363
Added support for :pep:`526` variable annotation syntax.
23682364

@@ -2380,6 +2376,10 @@ types.
23802376
.. versionchanged:: 3.11
23812377
Added support for generic namedtuples.
23822378

2379+
.. versionchanged:: next
2380+
Using :func:`super` (and the ``__class__`` :term:`closure variable`) in methods of ``NamedTuple`` subclasses
2381+
is unsupported and causes a :class:`TypeError`.
2382+
23832383
.. deprecated-removed:: 3.13 3.15
23842384
The undocumented keyword argument syntax for creating NamedTuple classes
23852385
(``NT = NamedTuple("NT", x=int)``) is deprecated, and will be disallowed

Lib/test/test_typing.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8365,12 +8365,12 @@ def test_super_explicitly_disallowed(self):
83658365
"in methods of NamedTuple subclasses"
83668366
)
83678367

8368-
with self.assertRaises(RuntimeError, msg=expected_message):
8368+
with self.assertRaises(TypeError, msg=expected_message):
83698369
class ThisWontWork(NamedTuple):
83708370
def __repr__(self):
83718371
return super().__repr__()
83728372

8373-
with self.assertRaises(RuntimeError, msg=expected_message):
8373+
with self.assertRaises(TypeError, msg=expected_message):
83748374
class ThisWontWorkEither(NamedTuple):
83758375
@property
83768376
def name(self):

Lib/typing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2962,7 +2962,7 @@ class NamedTupleMeta(type):
29622962
def __new__(cls, typename, bases, ns):
29632963
assert _NamedTuple in bases
29642964
if "__classcell__" in ns:
2965-
raise RuntimeError(
2965+
raise TypeError(
29662966
"uses of super() and __class__ are unsupported in methods of NamedTuple subclasses")
29672967
for base in bases:
29682968
if base is not _NamedTuple and base is not Generic:
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
Using :func:`super` and ``__class__`` :term:`closure variable` in
22
user-defined methods of :class:`typing.NamedTuple` subclasses is now
3-
explicitly prevented at runtime.
3+
explicitly prohibited at runtime.

0 commit comments

Comments
 (0)