python / python/mypy

Problems with `get` on a TypedDict

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

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

bug topic-type-context topic-typed-dict
主要言語
Python
スター
20.6k
フォーク
3.3k
PR マージ指標
PR 指標を取得中

説明

Bug Report

This bug is a bit hard to describe, so you should take a look at the test code. Feel free to edit the title.

As you can see in the test code, a expects dict[Literal["exclude"], list[str]]. test_dict, which is passed to b has a opional key named key with this type. As this type is optional, we use get here. Get returns {"exclude": []}) in case key does not exists in test_dict. {"exclude": []}) fits the type dict[Literal["exclude"], list[str]] but Mypy think it does not fit.

This bug does not exists in Mypy 1.17.

To Reproduce

https://mypy-play.net/?mypy=1.18.2&python=3.12&gist=932063170e6ad2c3ab798153870b82d4

Expected Behavior

Mypy should not find any errors

Actual Behavior

main.py:13: error: Argument 1 to "a" has incompatible type "dict[Literal['exclude'], list[str]] | dict[str, list[Never]]"; expected "dict[Literal['exclude'], list[str]]"  [arg-type]
Found 1 error in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: 1.18.2
  • Mypy command-line flags: None
  • Mypy configuration options from mypy.ini (and other config files): None
  • Python version used: 3.13.7

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

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

はじめの一歩

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

調査の方向性

main.py にあるリンク先の mypy-play の再現コードから始め、mypy 1.17 と 1.18.2 に対して実行して回帰を切り分けます。TypedDict のオプションキーに対する get 式がフォールバック型をどのように推論するかを追跡します。再現コードが正しい型チェックを維持したままエラーを生成しなくなれば完了です。

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

評価

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

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

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