]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-36517: Raise error on multiple inheritance with NamedTuple (GH-19363)
authorSerhiy Storchaka <storchaka@gmail.com>
Sat, 4 Apr 2020 18:31:30 +0000 (21:31 +0300)
committerGitHub <noreply@github.com>
Sat, 4 Apr 2020 18:31:30 +0000 (21:31 +0300)
Lib/test/test_typing.py
Lib/typing.py
Misc/NEWS.d/next/Library/2020-04-04-17-49-39.bpo-36517.Ilj1IJ.rst [new file with mode: 0644]

index 8d6262b9c1b02bc3f62b536e5bcf295d091dc752..3a0edb9e2d3237fabd16bd25b3c2fb616d1c35f7 100644 (file)
@@ -3626,6 +3626,13 @@ class XMethBad2(NamedTuple):
         return 'no chance for this as well'
 """)
 
+    def test_multiple_inheritance(self):
+        class A:
+            pass
+        with self.assertRaises(TypeError):
+            class X(NamedTuple, A):
+                x: int
+
     def test_namedtuple_keyword_usage(self):
         LocalEmployee = NamedTuple("LocalEmployee", name=str, age=int)
         nick = LocalEmployee('Nick', 25)
index 53518830002ca9949451a53b3062807a8c91e125..99355d0066647823544c51de08444bb29d78f265 100644 (file)
@@ -1728,6 +1728,9 @@ class NamedTupleMeta(type):
     def __new__(cls, typename, bases, ns):
         if ns.get('_root', False):
             return super().__new__(cls, typename, bases, ns)
+        if len(bases) > 1:
+            raise TypeError("Multiple inheritance with NamedTuple is not supported")
+        assert bases[0] is NamedTuple
         types = ns.get('__annotations__', {})
         nm_tpl = _make_nmtuple(typename, types.items())
         defaults = []
diff --git a/Misc/NEWS.d/next/Library/2020-04-04-17-49-39.bpo-36517.Ilj1IJ.rst b/Misc/NEWS.d/next/Library/2020-04-04-17-49-39.bpo-36517.Ilj1IJ.rst
new file mode 100644 (file)
index 0000000..cd5c0d7
--- /dev/null
@@ -0,0 +1,2 @@
+Multiple inheritance with :class:`typing.NamedTuple` now raises an error
+instead of silently ignoring other types.