Skip to content

Incompatible definition of generic field on multiple base classes #9031

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

Closed
dn-kialo opened this issue Jun 22, 2020 · 4 comments · Fixed by #18415
Closed

Incompatible definition of generic field on multiple base classes #9031

dn-kialo opened this issue Jun 22, 2020 · 4 comments · Fixed by #18415
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

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

chuckwondo added a commit to chuckwondo/xarray that referenced this issue Feb 7, 2025
Mypy 1.15 includes fix for <python/mypy#9031>,
allowing several "type: ignore" comments to be removed.
chuckwondo added a commit to chuckwondo/xarray that referenced this issue Feb 7, 2025
Mypy 1.15 includes fix for <python/mypy#9031>,
allowing several "type: ignore" comments to be removed.
chuckwondo added a commit to chuckwondo/xarray that referenced this issue Feb 14, 2025
Mypy 1.15 includes fix for <python/mypy#9031>,
allowing several "type: ignore" comments to be removed.
max-sixty pushed a commit to pydata/xarray that referenced this issue Feb 19, 2025
* Upgrade mypy to 1.15

Mypy 1.15 includes fix for <python/mypy#9031>,
allowing several "type: ignore" comments to be removed.

* Add type annotations to DataTree.pipe tests

* More precisely type `pipe` methods.

In addition, enhance mypy job configuration to support running it
locally via `act`.

Fixes #9997

* Pin mypy to 1.15 in CI

* Revert mypy CI job changes

* Add pytest-mypy-plugin and typestub packages

* Add pytest-mypy-plugins to all conda env files

* Remove dup pandas-stubs dep

* Revert pre-commit config changes

* Place mypy tests behind pytest mypy marker

* Set default pytest numprocesses to 4

* Ignore pytest-mypy-plugins for min version check
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
5 participants