python / python/mypy

Allow Callable type hint before function definition

Aperta
#16,946 2 commenti 6 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

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

Descrizione

Feature

Allow

f: Callable[[int], int]


def f(n):
    return 42

as an alternative to

def f(n: int) -> int:
    return 42

Pitch
Currently, the proposed syntax produces a Name "f" already defined ... [no-redef] error, unlike similar code for non-function values:

# OK
x: int
x = 3

For some complicated function signatures, it would be clearer to provide a single Callable hint than to have argument types
mixed in with the arguments themselves and the return type added after the parameter list. For that matter, the two styles above need not be mutually exclusive:

f: Callable[[int], int]
def f(n: int) -> int:
    return 42

mypy should report an error only if the types are identical. (Or perhaps compatible; I don't know if there would be use-cases
for the initial type to be a supertype of the one implied by the function annotations.)

This would be consistent with the def statement being a kind of assignment, as the following is currently allowed:

x: int
x = 3

as well as eliminate the asymmetry between function parameters and "ordinary" names.

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

L'issue non indica alcun file, test o punto di ingresso. Inizia individuando come mypy gestisce le assegnazioni annotate e le definizioni di funzioni, quindi confronta come segnala i nomi duplicati. Il lavoro è completato quando la dichiarazione Callable prima di def viene accettata, le annotazioni compatibili vengono gestite in modo coerente e i tipi in conflitto continuano a produrre un errore.

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

Valutazione

Stack tecnologico
python
Ambito
devtools
Tipo di issue
Funzionalità
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
25/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.