Associated types on generics
Nessuno ha ancora preso questa issue.
- Lingua principale
- Python
- Stelle
- 20.6k
- Fork
- 3.3k
- Metriche di merge delle PR
- Metriche PR in attesa
Descrizione
-
Request Type: Feature Request
-
Mock-up repro (more detailed mock-up here):
import abc import typing # Define two base types `ABase` & `BBase` with a similar interface, # that each return a different object type `AValue` & `BValue` from # some method # # The object type is also reexposed as a class attribute on the # respective base type for easier access. class AValue: value: bytes def __init__(self, value: bytes): self.value = value class ABase(metaclass=abc.ABCMeta): VALUE_T = AValue @abc.abstractmethod def get_some_value(self) -> AValue: ... class BValue: value: str def __init__(self, value: str): self.value = value class BBase(metaclass=abc.ABCMeta): VALUE_T = BValue @abc.abstractmethod def get_some_value(self) -> BValue: ... # Define generic type over the two defined above AnyBase = typing.TypeVar("AnyBase", ABase, BBase) # # HOW WOULD I NAME THE RETURN TYPE IN THE NEXT LINE? # # In particular note that the return type could not be just `AnyBase` # (over which we are generic) since we aren't returning *that* type, # but some other type determined by which type `AnyBase` is. def magic_convert(input: AnyBase) -> AnyBase.VALUE_T: #??? return input.VALUE_T(self.get_some_value().value * 2)``` -
Actual behaviour: There does seem to be anything like
AnyBase.VALUE_Tavailable for use inmypyand the given syntax results in both type-time and runtime errors. -
Expected behaviour: Some way to do this.
-
mypy version: 0.730
-
Python version: 3.7 (Debian)
-
mypy flags: None
I used some title that probably is close to the real (type theory) name of this feature, feel free to change it to something more fitting. 🙂
Guida per i contributori
Apri la guida per i contributori
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 la riproduzione in Python 3.7 dell’issue e leggi il mock-up dettagliato collegato per comprendere la relazione di tipo associato desiderata. Definisci come dovrebbero essere la sintassi supportata e il comportamento del controllo dei tipi per il tipo restituito generico, incluso l’esempio magic_convert mostrato; l’issue è completata quando mypy può esprimere e validare tale relazione senza gli errori segnalati in fase di controllo dei tipi o di esecuzione.
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