a way to refer to result type of an overloaded function
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 1.8k
- Forks
- 302
- Ø Merge
- 23 Std.
- Gemergte PRs (30 T.)
- 8
Beschreibung
I am looking for a way to refer to result type of an overloaded function.
To illustrate:
import typing as tp
T = tp.TypeVar("T")
@tp.overload
def gf1(i: int, j: tp.Any) -> str:
...
@tp.overload
def gf1(i: tp.Iterable[T], j: bool) -> tp.Dict[T, T]:
...
def gf1(i: tp.Union[int, tp.Iterable[T]], j: tp.Any) -> tp.Union[str, tp.Dict[T, T]]:
pass
gf1.resultType = lambda *a: None
class C(tp.Generic[T]):
def f1(self) -> gf1.resultType(T, bool):
pass
def f2(self) -> gf1.resultType(tp.List[T], bool):
pass
if not tp.TYPE_CHECKING:
def reveal_type(x):
pass
# outputs: builtins.str
reveal_type(gf1(1, True))
# wanted: builtins.str
reveal_type(C[int]().f1())
# outputs: builtins.dict[builtins.int*, builtins.int*]
reveal_type(gf1([1], True))
# wanted: builtins.dict[builtins.int*, builtins.int*]
reveal_type(C[int]().f2())
The f.resultOf syntax is, of course, tentative.
Issue #623 seems to suggest that this may be achievable with a clever use of of Protocol but I just cannot twist my head around that.
Upon request I can elaborate on my specific use case for this but I believe it would suffice to say that in real code gf1.resultType would be used multiple times. Specific argument types for gf1 were chosen arbitrarily for the sake of example.
Not sure whether #548 is anyhow related.
Beitragsleitfaden
Für dieses Repository ist kein Beitragsleitfaden indexiert
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 mit den Beispielen für überladene Funktionen in diesem Issue und sieh dir die zugehörigen Diskussionen in den Issues #623 und #548 an. Es ist keine Repository-Datei oder kein Test angegeben; als erledigt würde gelten, wenn ein abgestimmter, implementierbarer Umfang für die Bezugnahme auf den Ergebnistyp einer überladenen Funktion festgelegt 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
- 25/100