python / python/mypy

Function with generic class and generic value arguments are not inferred when generic is a TypedDict

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

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

bug topic-typed-dict
主要言語
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

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

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

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. 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

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

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