]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-85795: Raise a clear error when `super()` is used in `typing.NamedTuple` subclasse...
authorBartosz Sławecki <bartosz@ilikepython.com>
Thu, 6 Mar 2025 04:45:47 +0000 (05:45 +0100)
committerGitHub <noreply@github.com>
Thu, 6 Mar 2025 04:45:47 +0000 (20:45 -0800)
Doc/library/typing.rst
Lib/test/test_typing.py
Lib/typing.py
Misc/NEWS.d/next/Library/2025-02-13-15-10-56.gh-issue-85795.jeXXI9.rst [new file with mode: 0644]

index d4ab6eaea12d238f56fa02fcdc0b7797508e9367..aa613ee9f52f0a7bdcaac1e64a5cab44429fe8c7 100644 (file)
@@ -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
index e88c811bfcac52f9a87e3421633c6b2bb14b8ce7..a7901dfa6a4ef0480f634dc2aecd73479f4febae 100644 (file)
@@ -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):
index 4b3c63b25aeeab43f786fc294aed324f6a958fb0..1dd115473fb9276800b319d18e871d13e7a9b18c 100644 (file)
@@ -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(
diff --git a/Misc/NEWS.d/next/Library/2025-02-13-15-10-56.gh-issue-85795.jeXXI9.rst b/Misc/NEWS.d/next/Library/2025-02-13-15-10-56.gh-issue-85795.jeXXI9.rst
new file mode 100644 (file)
index 0000000..dec162b
--- /dev/null
@@ -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`.