.. 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
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):
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(
--- /dev/null
+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`.