-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Open
Description
https://mypy-play.net/?gist=42b235155079dd44a79e66cbcffb0060
from collections.abc import Mapping
class A: ...
class B(A): ...
def test_mapping() -> None:
class MyMapping[K, V](Mapping[K, V]): ...
def _0[K](arg: Mapping[K, B]) -> Mapping[K, A]: return arg
def _1[K](arg: MyMapping[K, B]) -> MyMapping[K, A]: return arg # ❌️mypy doesn't seem to reuse known variance of parent classes, but instead rechecks everything. For mapping, this breaks and mypy incorrectly infers invariance in the value type because of:
Lines 787 to 788 in a9fbf47
| @overload | |
| def get(self, key: _KT, default: _VT_co, /) -> _VT_co: ... # type: ignore[misc] # pyright: ignore[reportGeneralTypeIssues] # Covariant type as parameter |
While I do think mypy could improve this behavior on their side, we really, really should avoid using covariant parameters in contravariant positions, especially with the knowledge that it can screw up variance inference when using 3.12 style annotations.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels