Invalid type inference for generic typed dicts
Nessuno ha ancora preso questa issue.
- Lingua principale
- Python
- Stelle
- 20.6k
- Fork
- 3.3k
- Metriche di merge delle PR
- Metriche PR in attesa
Descrizione
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
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Non viene indicato alcun file del repository né alcun percorso di test; inizia eseguendo la riproduzione Python fornita con mypy 1.15.0 e ispezionando il tipo inferito per params.get('i', 0). Il lavoro è completato quando questa espressione viene inferita come int anziché object, con una copertura di regressione per il caso generico di TypedDict.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- python
- Ambito
- devtools
- Tipo di issue
- Bug
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Stato di attività
- Ferma
- Chiarezza
- Specificata chiaramente
- Idoneità per principianti
- 35/100