python / mypy

Optional static typing for Python
https://www.mypy-lang.org/
Other
18.21k stars 2.78k forks source link

Join computed suboptimally with multiple inheritance #4373

Open gvanrossum opened 6 years ago

gvanrossum commented 6 years ago
class Base:
    def foo(self): pass

class Mix: pass

class A(Mix, Base): pass

class B(Mix, Base): pass

def foo() -> None:
    a = [A(), B()]
    reveal_type(a)  # Shows Mix
    a.foo()  # E: "Mix" has no attribute "foo"

Could we do something better here? Ideally the inferred type of a should be an anonymous subclass of Mix and Base.

gvanrossum commented 6 years ago

Note that this can't be fixed by adding an annotation for a -- neither annotating it as Union[A, B] nor Base helps. (Basically you have to use a cast or rework the inheritance pattern.)

ilevkivskyi commented 6 years ago

I think this is another symptom of https://github.com/python/mypy/issues/3257

ilevkivskyi commented 6 years ago

Also this is yet another reason to have intersections.

hauntsaninja commented 2 months ago

(modified the example to force the join given #17427 )