There should be an inverse of `Type`
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
Specifically, it would be nice if there was a keyword Instance such that Instance[Type[_T]] is equivalent to _T
I'm thinking about this mostly in the context of working with metaclasses.
Suppose I have a class with a class method that creates an instance of the class written so it can be reused by subclasses. This can be typed in the following way:
_ThingInstance = TypeVar('_ThingInstance', bound='Thing')
class Thing():
@classmethod
def make_new(cls: Type[_ThingInstance]) -> _ThingInstance:
return cls()
And this is automatically usable by subclasses.
What I want is an Instance type such that I can correctly type the same logic defined within a metaclass like so:
_ThingClass = TypeVar('_ThingClass', bound='MetaThing')
class MetaThing(type):
def make_new(cls: _ThingClass) -> Instance[_ThingClass]:
return cls()
As far as I can tell, there's no way to correctly annotate this
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
Partite dall’annotazione proposta Instance[_ThingClass] e confrontatela con il pattern Type[_ThingInstance] mostrato. Definite come dovrebbe comportarsi l’inversa per i metodi della metaclasse e verificate gli esempi con una copertura del type checking; il lavoro è completo quando le annotazioni richieste vengono accettate con i tipi di istanza inferiti previsti.
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
- Abbastanza chiara
- Idoneità per principianti
- 28/100