python / python/mypy

Poor inference of dict literal when dict type is unioned with certain types

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

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

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

説明

Bug Report

To Reproduce

https://mypy-play.net/?mypy=master&python=3.11&gist=5cf1ab002a6dc63c5013fa0f6a00ba4d

Expected Behavior
I expect the dict literal to be inferred as dict[str, str |int] when passed to both foo and bar

Actual Behavior
It's inferred as dict[str, object] when passed to foo, which causes a false positive.

Note that the inference depends on the type you union the dict with. E.g. if you replace Iterable[tuple[str] with list[tuple[str]] you get the expected inference.

Your Environment

  • Mypy version used: Tried 1.0.0, 1.0.1, 1.1.1, 1.2.0 and master in the playground, all have the same behavior
  • Mypy command-line flags: See playground
  • Mypy configuration options from mypy.ini (and other config files): See playground
  • Python version used: 3.11

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

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

はじめの一歩

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

調査の方向性

リンクされている mypy playground の例を、Python 3.11 と報告されているバージョンで開始し、foo と bar で推論結果が異なることを再現します。Iterable[tuple[str]] と list[tuple[str]] を含むユニオンに対する dict リテラルの推論動作を追跡します。両方の呼び出しで dict が dict[str, str | int] と推論され、このケースを回帰テストでカバーできれば完了です。

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

評価

技術スタック
python
領域
devtools
issue の種類
バグ
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
説明が足りない
初心者へのやさしさ
38/100

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

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