bpo-36517: Raise error on multiple inheritance with NamedTuple (GH-19363)
This commit is contained in:
parent
1ae6445391
commit
a94e6272f1
|
@ -3626,6 +3626,13 @@ class XMethBad2(NamedTuple):
|
||||||
return 'no chance for this as well'
|
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):
|
def test_namedtuple_keyword_usage(self):
|
||||||
LocalEmployee = NamedTuple("LocalEmployee", name=str, age=int)
|
LocalEmployee = NamedTuple("LocalEmployee", name=str, age=int)
|
||||||
nick = LocalEmployee('Nick', 25)
|
nick = LocalEmployee('Nick', 25)
|
||||||
|
|
|
@ -1728,6 +1728,9 @@ class NamedTupleMeta(type):
|
||||||
def __new__(cls, typename, bases, ns):
|
def __new__(cls, typename, bases, ns):
|
||||||
if ns.get('_root', False):
|
if ns.get('_root', False):
|
||||||
return super().__new__(cls, typename, bases, ns)
|
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__', {})
|
types = ns.get('__annotations__', {})
|
||||||
nm_tpl = _make_nmtuple(typename, types.items())
|
nm_tpl = _make_nmtuple(typename, types.items())
|
||||||
defaults = []
|
defaults = []
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
Multiple inheritance with :class:`typing.NamedTuple` now raises an error
|
||||||
|
instead of silently ignoring other types.
|
Loading…
Reference in New Issue