Skip to content

Mapping overloads cause incorrect variance inference #15500

@randolf-scholz

Description

@randolf-scholz

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:

typeshed/stdlib/typing.pyi

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions