Dict unpacking fails when used for building TypedDict with required keys
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 dict unpacking is used to dynamically add optional keys to a TypedDict, mypy raises a typeddict-item error.
As dict unpacking is used to fill some keys from the TypedDict, my understanding is that mypy could validate the unpacked dictionaries considering all keys as NotRequired, and only the final TypedDict respecting its formal definition.
To Reproduce
Gist: https://mypy-play.net/?mypy=latest&python=3.13&gist=b3377281f7f5a6f83dd9962d60302827
from typing import NotRequired, TypedDict
class Project(TypedDict):
name: str
version: NotRequired[str]
def build_project(name: str, version: str | None = None) -> Project:
return {
"name": name,
**({"version": version} if version is not None else {})
}
Actual Behavior
$ mypy .
main.py:12: error: Missing key "name" for TypedDict "Project" [typeddict-item]
Found 1 error in 1 file (checked 1 source file)
Your Environment
- Mypy version used:
1.17.0 - Mypy command-line flags: N/A
- Mypy configuration options from
mypy.ini(and other config files): N/A - Python version used:
3.13.5
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
Inizia con la riproduzione collegata su mypy-play ed esegui l'esempio mostrato con Python 3.13. Leggi la gestione di TypedDict e dict-unpacking raggiunta da questo caso, quindi aggiungi un test di regressione per l'errore segnalato. Il lavoro è completato quando l'esempio accetta la chiave richiesta "name" e il "version" spacchettato facoltativo senza un errore typeddict-item.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- python
- Ambito
- tooling
- Tipo di issue
- Bug
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 45/100