python / python/mypy

Misleading issue text when return type doesn't match type annotation

Aperta
#14,773 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

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

Descrizione

Bug Report

When you return a function call like return list(...), return dict(...), and the type doesn't match the return annotation, the error message says that list(...) is not valid code.

To Reproduce

def foo() -> list[int]:
    tup = ("a", "b", "c")
    return list(tup)

Expected Behavior

I was expecting mypy to tell me that the type annotation doesn't match the return type.

Instead it says that the arguments to list() are wrong, i.e. list doesn't take this type.

Like so:

$ mypy asd.py
asd.py:3: error: Incompatible return value type (got "List[str]", expected "List[int]")  [return-value]
Found 1 error in 1 file (checked 1 source file)

Actual Behavior

$ mypy asd.py 
asd.py:3: error: Argument 1 to "list" has incompatible type "Tuple[str, str, str]"; expected "Iterable[int]"  [arg-type]
Found 1 error in 1 file (checked 1 source file)

I think this is misleading.

Your Environment

  • Mypy version used: v1.0.1
  • Mypy command-line flags: N/A
  • Mypy configuration options from mypy.ini (and other config files): N/A
  • Python version used: 3.10.9

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

Riproduci la diagnostica con l'esempio in asd.py, usando la versione e il comando di mypy riportati. Traccia come vengono controllate l'espressione di ritorno e la relativa annotazione, quindi fai in modo che la diagnostica identifichi il valore di ritorno incompatibile e aggiungi la copertura per i casi list(...) e dict(...).

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
42/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.