Annotate function which return a specific module (e.g. `-> Literal[np]`)
Nessuno ha ancora preso questa issue.
- Lingua principale
- Python
- Stelle
- 1.8k
- Fork
- 302
- Merge medio
- 23h
- PR unite (30g)
- 8
Descrizione
Problem
Currently it's not possible to annotate a function which return a specific module, like:
np = load_numpy() # def load_numpy() -> ??:
x = np.array(123) # << I want to have auto-complete & type checking here
-> types.ModuleTypedoes not work as too generic (no auto-complete, nor static type checking,...)Protocolis not applicable in practice: maintaining a numpy protocol which has 500+ symbols is just not realistic
Use case
Here are some concrete use-case where this feature is needed (also collected from this thread comments):
-
Numpy Enancement Propopsal 37 propose a recipe to have code working with various numpy implementation (
numpy,jax.numpy,tensorflow.numpy):def duckarray_add_random(array): module = np.get_array_module(array) # def get_array_module() -> Literal[np] noise = module.random.randn(*array.shape) # << I want to have auto-complete & type checking here return array + noiseI developed my version at https://github.com/google/etils/tree/main/etils/enp#code-that-works-with-nparray-jnparray-tftensor
-
Lazy imports is a common pattern to only import a module if needed. Like: https://github.com/tensorflow/datasets/blob/76f8591def26afaca16340b06d057553582f6163/tensorflow_datasets/core/lazy_imports_lib.py#L40-L197
beam = lazy_import.apache_beam beam.Pipeline() # << No auto-completion -
From another user comment: Similar issue to to 1. encountered at: https://github.com/data-apis/array-api/issues/267
-
From another user comment:
I encountered a similar issue before. Although not with with the return type but rather version dependent imports, e.g. assign either ast (Py >= 3.8) or typed_ast.ast3 to a common variable.
Proposal
I would like to annotate my function as:
def load_numpy() -> Literal[np]:
Or:
def load_numpy() -> np:
For the lazy-loading case, typing.TYPE_CHECKING pattern could be used:
if typing.TYPE_CHECKING:
import numpy as np
def load_numpy() -> Literal[np]:
import numpy as np
return np
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 dalla Proposal e dagli esempi collegati di importazioni di NumPy, lazy-import e dipendenti dalla versione, quindi esamina la discussione esistente sul typing issue. Non viene indicato alcun file di implementazione né alcun test; il lavoro è considerato completato quando sono specificati il modo supportato per esprimere un valore restituito che sia un modulo e il relativo comportamento di autocompletamento e controllo dei tipi.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- python
- Ambito
- tooling
- Tipo di issue
- Funzionalità
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 25/100