Invalid type inference for generic typed dicts
まだ誰も着手していません。
- 主要言語
- Python
- スター
- 20.6k
- フォーク
- 3.3k
- PR マージ指標
- PR 指標を取得中
説明
Bug Report
When we try to assert the type of an object obtained from get with default value from generic object bound to typed dict, then mypy shows next error:
error: Incompatible types in assignment (expression has type "object", variable has type "int") [assignment]
To Reproduce
from typing import NotRequired, TypedDict, TypeVar
class A(TypedDict):
i: NotRequired[int]
ParamsT = TypeVar('ParamsT', bound=A)
def foo(params: ParamsT) -> None:
i: int = params.get('i', 0)
foo(A())
Gist: https://gist.github.com/mypy-play/effc3f0d297540be8ca1bad7b08c5902
Play: https://mypy-play.net/?mypy=latest&python=3.12&gist=effc3f0d297540be8ca1bad7b08c5902
Expected Behavior
It is expected that result of expression params.get('i', 0) would be of type int since typed dict attribute is annotated as <int> and the default value type is <int> as well.
Actual Behavior
Mypy reports that result of expression params.get('i', 0) has type <object>.
Your Environment
- Mypy version used: mypy 1.15.0 (compiled: yes)
- Python version used: 3.13
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
リポジトリのファイルもテストパスも指定されていません。まず、提供された Python の再現コードを mypy 1.15.0 で実行し、params.get('i', 0) に対して推論される型を確認してください。完了の条件は、この式が object ではなく int として推論され、汎用 TypedDict のケースに対するリグレッションカバレッジがあることです。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- python
- 領域
- devtools
- issue の種類
- バグ
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 活発さ
- 停滞
- 明瞭さ
- 明確に書かれている
- 初心者へのやさしさ
- 35/100