a way to refer to result type of an overloaded function
Nessuno ha ancora preso questa issue.
- Lingua principale
- Python
- Stelle
- 1.8k
- Fork
- 302
- Merge medio
- 23h
- PR unite (30g)
- 8
Descrizione
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.
Guida per i contributori
Nessuna guida per i contributori indicizzata per questo repository
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia con gli esempi di funzioni sovraccariche in questa issue e rivedi le discussioni correlate nelle issue #623 e #548. Non sono identificati file del repository né test; il lavoro sarebbe considerato completato quando sarà definito un ambito concordato e implementabile per fare riferimento al tipo di risultato di una funzione sovraccarica.
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
- 25/100