Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Join computed suboptimally with multiple inheritance #4373

Open
gvanrossum opened this issue Dec 15, 2017 · 6 comments
Open

Join computed suboptimally with multiple inheritance #4373

gvanrossum opened this issue Dec 15, 2017 · 6 comments
Labels
bug mypy got something wrong topic-inheritance Inheritance and incompatible overrides

Comments

@gvanrossum
Copy link
Member

gvanrossum commented Dec 15, 2017

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
Copy link
Member Author

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
Copy link
Member

I think this is another symptom of #3257

@ilevkivskyi
Copy link
Member

Also this is yet another reason to have intersections.

@AlexWaygood AlexWaygood added the topic-join-v-union Using join vs. using unions label Mar 24, 2022
@AlexWaygood AlexWaygood added topic-inheritance Inheritance and incompatible overrides bug mypy got something wrong labels Apr 6, 2022
@hauntsaninja hauntsaninja removed the topic-join-v-union Using join vs. using unions label Jul 3, 2024
@hauntsaninja
Copy link
Collaborator

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

@JelleZijlstra
Copy link
Member

JelleZijlstra commented Oct 6, 2024

Another example, distilled from mypy-primer output on python/typeshed#12740:

class A: pass
class B: pass
class C: pass

class Cls1(A, B, C): pass
class Cls2(A, B, C): pass

def func(cond: bool, c1: Cls1, c2: Cls2) -> None:
    a: A = c1 if cond else c2
    b: B = c1 if cond else c2  # error
    c: C = c1 if cond else c2  # error
    print(a, b, c)

@hauntsaninja
Copy link
Collaborator

hauntsaninja commented Oct 6, 2024

I think mypy latest no longer joins for ternary, so the specific primer hit should go away when 1.12 is released

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-inheritance Inheritance and incompatible overrides
Projects
None yet
Development

No branches or pull requests

5 participants