python / python/mypy

Problems with `get` on a TypedDict

Aperta
#20,138 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

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

Descrizione

Bug Report

This bug is a bit hard to describe, so you should take a look at the test code. Feel free to edit the title.

As you can see in the test code, a expects dict[Literal["exclude"], list[str]]. test_dict, which is passed to b has a opional key named key with this type. As this type is optional, we use get here. Get returns {"exclude": []}) in case key does not exists in test_dict. {"exclude": []}) fits the type dict[Literal["exclude"], list[str]] but Mypy think it does not fit.

This bug does not exists in Mypy 1.17.

To Reproduce

https://mypy-play.net/?mypy=1.18.2&python=3.12&gist=932063170e6ad2c3ab798153870b82d4

Expected Behavior

Mypy should not find any errors

Actual Behavior

main.py:13: error: Argument 1 to "a" has incompatible type "dict[Literal['exclude'], list[str]] | dict[str, list[Never]]"; expected "dict[Literal['exclude'], list[str]]"  [arg-type]
Found 1 error in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: 1.18.2
  • Mypy command-line flags: None
  • Mypy configuration options from mypy.ini (and other config files): None
  • Python version used: 3.13.7

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 con la riproduzione collegata di mypy-play in main.py, eseguendola con mypy 1.17 e 1.18.2 per isolare la regressione. Traccia come le espressioni get delle chiavi opzionali di TypedDict deducono il loro tipo di fallback; il lavoro è completato quando la riproduzione non produce errori, mantenendo al contempo il corretto controllo dei tipi.

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
Abbastanza chiara
Idoneità per principianti
30/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.