Mapping overloads cause incorrect variance inference
まだ誰も着手していません。
- 主要言語
- Python
- スター
- 5.1k
- フォーク
- 2.1k
- 平均マージ
- 1日 19時間
- マージ済み PR(30日)
- 82
説明
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.
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
まず、提供された例を mypy playground で再現し、次にリンク先の typeshed typing.pyi の 787-788 行にある Mapping の定義を調べます。それらの variance annotation が 3.12-style MyMapping の例とどのように相互作用するかを確認します。例が誤った variance error を生成しなくなり、同等の annotation の問題も導入していなければ完了です。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- python
- 領域
- tooling
- issue の種類
- バグ
- 難易度
- 3/5
- 見積もり時間
- 1〜2日
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 45/100