variance okay for literals, but not expressions ([1, 2] okay for list[int | None] but [1] + [2] is error)
オープン
まだ誰も着手していません。
bug
topic-type-context
- 主要言語
- Python
- スター
- 20.6k
- フォーク
- 3.3k
- PR マージ指標
- PR 指標を取得中
説明
a: list[int | None] = [1] + [2] + [None]
b: list[int | None] = [1] + [2]
c: list[int | None] = [1, 2]
a is okay, b is an error, c is okay
Apologies if this is a repeat / common issue. I couldn't find anything in the docs. This is popping up for me a lot, my interpretation is that mypy allows type invariance on literals that it will not for expressions.
To Reproduce
https://mypy-play.net/?mypy=latest&python=3.11&gist=b999a1bd2e975645b868c0b376c910e8
Expected Behavior
Either b and c should both be errors or (preferably) neither.
Actual Behavior
b is an error but c isn't
Your Environment
- Mypy version used: 1.7.1 and master
- Python version used: 3.11
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
まず、リンク先の mypy-play の再現コードを mypy 1.7.1 または master と Python 3.11 で実行し、代入 a、b、c の診断を比較します。Issue に記載された期待される動作に従い、[1] + [2] の動作が [1, 2] の動作と一致すれば完了です。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- python
- 領域
- compilers
- issue の種類
- バグ
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 35/100