python / python/mypy

Add ReturnType-like utility for getting function return types

Aperta
#8,385 5 commenti 28 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

feature needs discussion priority-2-low topic-usability
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

This is a feature request, so if there is a way to achieve this already, please point me and future me in the right way.

In TypeScript, we can use the ReturnType utility type to get the return type of a function to reuse in other places. This is very useful to make the code short, readable, and easy to change if necessary, especially in cases where I want to wrap the call of a function from another module in some other function.

An example of how it might be used:

# module: foo.py
def foo() -> None | baz.Baz | tuple[int, int]:
    ...

# module: bar.py
def bar() -> ReturnType[foo.foo]:
    ...

In this case, I could maybe give the return type a name like FooReturn and import that in bar, but that seems less elegant rather than just reusing the function I'm already using.


Note that if automatic or manual type inference (https://github.com/python/mypy/issues/4409) was implemented, most of my usages would be covered.

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 esaminando l'utility simile a ReturnType richiesta in questa issue e l'issue #4409 di mypy collegata sull'inferenza automatica o manuale dei tipi. Determina la sintassi dei tipi e il comportamento previsti per l'esempio con foo.py e bar.py; done deve significare che il riutilizzo richiesto del tipo di ritorno è supportato oppure che l'approccio già supportato è documentato.

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
Da chiarire
Idoneità per principianti
30/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.