Add ReturnType-like utility for getting function return types
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 20.6k
- Forks
- 3.3k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
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.
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginne damit, die in diesem Issue angeforderte ReturnType-ähnliche Utility und das verknüpfte mypy-Issue #4409 zur automatischen oder manuellen Typinferenz zu prüfen. Ermittle die beabsichtigte Typsyntax und das Verhalten für das Beispiel mit foo.py und bar.py; done soll bedeuten, dass die angeforderte Wiederverwendung des Rückgabetyps unterstützt wird oder der bereits unterstützte Ansatz dokumentiert ist.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- devtools
- Issue-Typ
- Feature
- Schwierigkeit
- 5/5
- Geschätzter Aufwand
- Über eine Woche
- Aktivitätsstatus
- Veraltet
- Klarheit
- Muss geklärt werden
- Anfängerfreundlichkeit
- 30/100