python / python/mypy

Confusing error from bidirectional inference?

Aperta
#18,297 1 commento 1 reazione 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

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

Descrizione

Bug Report

When the return type from dict.pop is incompatible with existing variable type, mypy gives an arg-type error when an assignment error seems more intuitive.

(Originally noted in #10152)

To Reproduce

values_by_name = {"one": 1, "two": 2}
value: int
value = values_by_name.pop("one", None)

https://gist.github.com/mypy-play/972f4514ebe5620ece42583192907f62

Expected Behavior

example.py:3: error: Incompatible types in assignment (expression has type "int | None", variable has type "int")  [assignment]

Actual Behavior

example.py:3: error: Argument 2 to "pop" of "dict" has incompatible type "None"; expected "int"  [arg-type]

Note: this is a lot more surprising if value has this type because it was unintentionally reused from some code further up, e.g. if you're using value in a loop or something; it can be very difficult to spot what the real error is. See https://github.com/python/mypy/issues/10152#issuecomment-2538685996 for example.

Your Environment

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

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 riproducendo la diagnostica dell’issue in example.py con mypy 1.13.0, quindi traccia il modo in cui l’inferenza bidirezionale gestisce il valore predefinito di dict.pop e il tipo della variabile esistente. Il lavoro è completato quando l’esempio segnala un’assegnazione incompatibile con [assignment] anziché un errore di tipo dell’argomento.

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.