python / python/mypy

Redefinition of function arguments can give unclear error message

Aperta
#17,439 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

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

Descrizione

With

import typing
# simulate an untyped module function
def function() -> typing.Any:
    return []

def foo(var: list|str) -> list:
   # insert a lot of code so that it's totally unobvious that "var" is a function parameter
    var = function()
    return var

You get the output

$ mypy ./test2.py 
test2.py:8: error: Incompatible return value type (got "list[Any] | str", expected "list[Any]")  [return-value]
Found 1 error in 1 file (checked 1 source file)
$ mypy--version
mypy 1.10.0 (compiled: yes)

which is technically correct, as var was defined as list[Any] | str as a parameter. But if you don't realise that, say there is a ton of code between where that's defined and where the return value is, you are left looking at var = function(); return var and wondering why the heck function() has been inferred to return list[Any] | str.

For a "standard" redefinition a [no-redef] warning comes out making it fairly clear. It would be great if somehow it remembered function arguments too, and then put out for this case Name "var" already defined as function parameter on line 7 which would make this very clear.

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 Python fornita ed eseguila con mypy 1.10.0 per osservare l'errore attuale relativo al valore restituito. Segui la diagnostica esistente [no-redef] per le ridefinizioni ordinarie e determina dove vengono gestite le ridefinizioni dei parametri delle funzioni. Il lavoro è terminato quando l'esempio segnala il nome del parametro e la relativa riga di dichiarazione con una diagnostica chiara.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
compilers
Tipo di issue
Funzionalità
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.