Function with generic class and generic value arguments are not inferred when generic is a TypedDict
まだ誰も着手していません。
- 主要言語
- Python
- スター
- 20.6k
- フォーク
- 3.3k
- PR マージ指標
- PR 指標を取得中
説明
Bug Report
When defining a function expecting a generic class and a generic value — tied to the same TypeVar —, mypy is not able to infer the type of the argument if the generic type is a TypedDict.
It works well for scalar types, if the variable is explicitly defined as the typed dict type, or even more surprising, if the first generic argument is binded via a functools.partial.
To Reproduce
https://mypy-play.net/?mypy=latest&python=3.12&gist=a6d36519fbd58e5c9b77999eedd16132
import typing
import functools
T = typing.TypeVar("T")
class A(typing.Generic[T]): ...
def f(a: A[T], t: T) -> None: ...
class D(typing.TypedDict):
x: int
a_typed_dict = A[D]()
f(a_typed_dict, {"x": 1}) # error: Cannot infer type argument 1 of "f" [misc]
Expected Behavior
The t argument is correctly validated as a D typed dict.
Actual Behavior
The following error message is returned:
error: Cannot infer type argument 1 of "f" [misc]
Your Environment
- Mypy version used: 1.15
- Python version used: 3.12
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
まず、リンクされた mypy-play の再現コードを mypy 1.15 と Python 3.12 で実行します。TypedDict 引数の検証時における A[D] のジェネリック型推論を追跡します。完了条件は、呼び出しが "Cannot infer type argument 1" エラーなしで {"x": 1} を D として受け入れることです。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- python
- 領域
- compilers
- issue の種類
- バグ
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 45/100