Mapping overloads cause incorrect variance inference
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:
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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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