From 46cb614fb20eb1a453264da62ec7c6ebfb8ae2d0 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Sat, 4 Apr 2020 17:49:59 +0300 Subject: [PATCH 1/2] bpo-36517: Raise error on multiple inharitence with NamedTuple. --- Lib/test/test_typing.py | 7 +++++++ Lib/typing.py | 3 +++ .../next/Library/2020-04-04-17-49-39.bpo-36517.Ilj1IJ.rst | 2 ++ 3 files changed, 12 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2020-04-04-17-49-39.bpo-36517.Ilj1IJ.rst diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py index 8d6262b9c1b02b..3a0edb9e2d3237 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 0a685d304bd96f..5b16c4f4e26976 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 00000000000000..315313cd017ec1 --- /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` raises now an error +instead of silently ignoring other types. From b7e45e12dc88a6bbb2217d4928e79416b114765f Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Sat, 4 Apr 2020 21:01:07 +0300 Subject: [PATCH 2/2] Update Misc/NEWS.d/next/Library/2020-04-04-17-49-39.bpo-36517.Ilj1IJ.rst Co-Authored-By: Ivan Levkivskyi --- .../next/Library/2020-04-04-17-49-39.bpo-36517.Ilj1IJ.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index 315313cd017ec1..cd5c0d729f1e7f 100644 --- 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 @@ -1,2 +1,2 @@ -Multiple inheritance with :class:`typing.NamedTuple` raises now an error +Multiple inheritance with :class:`typing.NamedTuple` now raises an error instead of silently ignoring other types.