python / python/mypy

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

Aperta
#19,201 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

bug topic-typed-dict
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

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

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia eseguendo la riproduzione collegata di mypy-play con mypy 1.15 e Python 3.12. Traccia l’inferenza dei tipi generici per A[D] durante la validazione dell’argomento TypedDict; il lavoro è completato quando la chiamata accetta {"x": 1} come D senza l’errore "Cannot infer type argument 1".

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
compilers
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
45/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.