[Python-checkins] bpo-36517: Raise error on multiple inheritance with NamedTuple (GH-19363)

Serhiy Storchaka webhook-mailer at python.org
Sat Apr 4 14:31:35 EDT 2020


https://github.com/python/cpython/commit/a94e6272f16381349dbed74cdb738ec8ae23b4fe
commit: a94e6272f16381349dbed74cdb738ec8ae23b4fe
branch: master
author: Serhiy Storchaka <storchaka at gmail.com>
committer: GitHub <noreply at github.com>
date: 2020-04-04T21:31:30+03:00
summary:

bpo-36517: Raise error on multiple inheritance with NamedTuple (GH-19363)

files:
A Misc/NEWS.d/next/Library/2020-04-04-17-49-39.bpo-36517.Ilj1IJ.rst
M Lib/test/test_typing.py
M Lib/typing.py

diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py
index 8d6262b9c1b02..3a0edb9e2d323 100644
--- a/Lib/test/test_typing.py
+++ b/Lib/test/test_typing.py
@@ -3626,6 +3626,13 @@ def _source(self):
         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)
diff --git a/Lib/typing.py b/Lib/typing.py
index 53518830002ca..99355d0066647 100644
--- a/Lib/typing.py
+++ b/Lib/typing.py
@@ -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
index 0000000000000..cd5c0d729f1e7
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2020-04-04-17-49-39.bpo-36517.Ilj1IJ.rst
@@ -0,0 +1,2 @@
+Multiple inheritance with :class:`typing.NamedTuple` now raises an error
+instead of silently ignoring other types.



More information about the Python-checkins mailing list