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

Class variable in multiple base classes incompatible #17079

Open
jeremander opened this issue Mar 30, 2024 · 0 comments
Open

Class variable in multiple base classes incompatible #17079

jeremander opened this issue Mar 30, 2024 · 0 comments
Labels
bug mypy got something wrong

Comments

@jeremander
Copy link

I'm getting a mypy error when I inherit from multiple base classes which define the same class variable.

A minimal script looks like:

class Obj:
    ...

class Left(Obj):
    ...

class Right(Obj):
    ...

class A:
    val: Obj

class B(A):
    val = Left()

class C(A):
    val = Right()

class D(B, C):
    ...

The error I get is:

Definition of "val" in base class "B" is incompatible with definition in base class "C"  [misc]

I was expecting there to be no error here. The following script works, for instance:

class Obj:
    ...

class Left(Obj):
    ...

class Right(Obj):
    ...

val: Obj
val = Left()
val = Right()

I can avoid the error by changing the definitions of B and C to:

class B(A):
    val: Obj = Left()

class C(A):
    val: Obj = Right()

But this seems redundant.

Environment

  • Mypy version used: 1.9.0
  • Mypy command-line flags: None
  • Python version used: 3.11
@jeremander jeremander added the bug mypy got something wrong label Mar 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

No branches or pull requests

1 participant