Code:
from collections import namedtuple
from typing import Dict
class A(object):
pass
class B(A, namedtuple('bsuper', '')):
pass
d = {} # type: Dict[str, A]
d['hi'] = B()
d['hi2'] = A()
Error:
demo.py:14: error: Incompatible types in assignment (expression has type "B", target has type "A")
Expectation is that no error is thrown, because B inherits from A.
Yes, we are seeing a lot of namedtuple-related bugs. :-(
This _does_ appear to be a namedtuple problem (in that it goes away when you don't inherit from namedtuple). The usage of dict isn't necessary to repro -- B doesn't appear to be regarded as a subclass of A.
Shorter repro:
from collections import namedtuple
class A(object): pass
class B(A, namedtuple('bsuper', '')): pass
x = A()
x = B() # E: Incompatible types in assignment (expression has type "B", variable has type "A")
I believe this has been fixed. At least, the repro does not reproduce.
Can you find the commit that fixed it using bisection?
--Guido (mobile)
PR #2091, commit 590c15f
Great! One bug down
Most helpful comment
Yes, we are seeing a lot of namedtuple-related bugs. :-(