python / python/mypy

variance okay for literals, but not expressions ([1, 2] okay for list[int | None] but [1] + [2] is error)

オープン
#16,623 コメント 1 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

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

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. 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

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。