bpo-36517: Raise error on multiple inheritance with NamedTuple (GH-19363)
diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py
index 8d6262b..3a0edb9 100644
--- a/Lib/test/test_typing.py
+++ b/Lib/test/test_typing.py
@@ -3626,6 +3626,13 @@
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 5351883..99355d0 100644
--- a/Lib/typing.py
+++ b/Lib/typing.py
@@ -1728,6 +1728,9 @@
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 = []