Mypy: Multiple inheritance from namedtuple causes error on assignment

Created on 21 Jun 2016  路  7Comments  路  Source: python/mypy

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.

bug topic-named-tuple

Most helpful comment

Yes, we are seeing a lot of namedtuple-related bugs. :-(

All 7 comments

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

datnamer picture datnamer  路  49Comments

JukkaL picture JukkaL  路  27Comments

msullivan picture msullivan  路  26Comments

Starwort picture Starwort  路  32Comments

JukkaL picture JukkaL  路  30Comments