python / python/typeshed

Mapping overloads cause incorrect variance inference

Open
#15,500 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
5.1k
Forks
2.1k
Avg merge
1d 19h
Merged PRs (30d)
82

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:

https://github.com/python/typeshed/blob/a9fbf47e676865bd5bcd94e121624d9e4b53c882/stdlib/typing.pyi#L787-L788

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the supplied example in the mypy playground, then inspect the Mapping definitions at the linked typeshed typing.pyi lines 787-788. Determine how their variance annotations interact with the 3.12-style MyMapping example; done means the example no longer produces the incorrect variance error without introducing an equivalent annotation issue.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.