python / python/mypy

False negative when lvalue has union type

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

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

affects-mypyc bug topic-union-types
主要言語
Python
スター
20.6k
フォーク
3.3k
PR マージ指標
PR 指標を取得中

説明

Mypy generates no error, even though the code is unsafe:

class C[T]:
    a: T

def f(x: C[int] | C[str]) -> None:
    x.a = 1  # No error

c = C[str]()
f(c)
print(c.a, type(c.a))  # 1, int

I'd expect x.a = 1 to generate an error, since the assignment is unsafe.

I suspect that the lvalue type is inferred as int | str, and mypy thinks it's fine. instead, assignment should be valid for each union item separately.

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

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

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

まず、提供された generic-union の例を mypy で再現し、union 型の lvalues への代入に対する型チェックの経路を調べてください。いずれかの union メンバーに対して安全でない場合は代入が拒否され、妥当な代入は引き続き通過すれば完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
python
領域
compilers, devtools
issue の種類
バグ
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
35/100

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

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