Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
bpo-36517: Raise error on multiple inharitence with NamedTuple.
  • Loading branch information
serhiy-storchaka committed Apr 4, 2020
commit 46cb614fb20eb1a453264da62ec7c6ebfb8ae2d0
7 changes: 7 additions & 0 deletions Lib/test/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions Lib/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Multiple inheritance with :class:`typing.NamedTuple` raises now an error
instead of silently ignoring other types.