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

Incompatible definition of generic field on multiple base classes #9031

Open
dn-kialo opened this issue Jun 22, 2020 · 4 comments
Open

Incompatible definition of generic field on multiple base classes #9031

dn-kialo opened this issue Jun 22, 2020 · 4 comments
Labels
bug mypy got something wrong false-positive mypy gave an error on correct code priority-1-normal topic-type-variables

Comments

@dn-kialo
Copy link

dn-kialo commented Jun 22, 2020

[BUG] Edited for a clearer example
Given this code:

# mypy version 0.770
from typing import Generic, TypeVar

T = TypeVar("T", int, str)

class A:
  foo: int

class B(Generic[T]):
  foo: T

class C(B[int], A):
  pass

reveal_type(A().foo)
reveal_type(B().foo)
reveal_type(C().foo)

I get this output:

example.py:11: error: Definition of "foo" in base class "B" is incompatible with definition in base class "A"
example.py:14: note: Revealed type is 'builtins.int'
example.py:15: note: Revealed type is 'builtins.int*'
example.py:16: note: Revealed type is 'builtins.int*'

I couldn't find what builtins.int*. The code makes sense though, right?

@ilevkivskyi
Copy link
Member

Yeah, this is a mypy bug. Likely related to #7724

@ilevkivskyi ilevkivskyi added bug mypy got something wrong false-positive mypy gave an error on correct code priority-1-normal topic-type-variables labels Jul 3, 2020
@headtr1ck
Copy link

Still happening with mypy 0.960 :(
Although, now without "*" in the revealed types.

@adamchainz
Copy link
Sponsor

I think I just encountered a modified version of this in django-stubs: typeddjango/django-stubs#1227 . Due to the structure of the classes we are trying to type, the attribute is defined as generic in both base classes, and passed as a type parameter to each:

from typing import Generic, TypeVar

T = TypeVar("T", int, str)

class A(Generic[T]):
  foo: T

class B(Generic[T]):
  foo: T

class C(Generic[T], B[T], A[T]):
  pass

c: C[int] = C()

reveal_type(A().foo)
reveal_type(B().foo)
reveal_type(c.foo)

@tonysyu
Copy link

tonysyu commented Dec 20, 2022

I just ran into the same issue. I don't see it explicitly mentioned above, but one workaround is to explicitly type the attribute in the concrete class (if that's an option; i.e. if the type is resolved in the class definition rather than the variable declaration); e.g.

from typing import Generic, TypeVar

T = TypeVar('T')

class A(Generic[T]):
    value: T

class B(Generic[T]):
    value: T

class C(A[int], B[int]):
    value: int

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong false-positive mypy gave an error on correct code priority-1-normal topic-type-variables
Projects
None yet
Development

No branches or pull requests

5 participants