Proposal: Programmatically create types
Nessuno ha ancora preso questa issue.
- Lingua principale
- Python
- Stelle
- 1.8k
- Fork
- 302
- Merge medio
- 23h
- PR unite (30g)
- 8
Descrizione
This proposal is aimed at solving two related problem. First, when defining a multi-ary operator on Numpy arrays, e.g., leaky integral, then you ideally want to bake in Numpy type promotion rules. However, even Numpy doesn't use its own type promotion rules in its type annotations.
So, my suggestion is the following:
from typing import SyntheticType
def ResultType(*args: Any) -> SyntheticType[DType]:
return result_type(*args) # Something like this, but would have to deal with e.g., numpy.ndarray[typing.Any, numpy.dtype[numpy.floating[typing.Any]]]
# Then...
@overload
def __add__(self: T, other: U) -> NDArray[ResultType[T, U]]: ... # type: ignore[misc]
The type checker could be called with a special argument, like --create_synthetic_stubs. This would
- Run type checking and collect a list of unique argument tuples to synthetic type functions like
ResultType. - Start a Python interpreter and call the synthetic type functions using the the arguments (which are Python objects representing types, e.g.,
T=floatandU=numpy.ndarray[typing.Any, numpy.dtype[numpy.floating[typing.Any]]]). - It would then write the results to some
pyifile in some canonical table-like format, like:
`ResultType: SyntheticTypeMapping = {(float, float): float, ...}
The table could either be just stored in the cache. Users of the library would have to generate this file, which means type checkers run code in the library.
The other problem this solves is one that I would like to annotate dataclasses whose elements can be None or int besides whatever they've specified as. See here for a description as to why. It would be pretty easy to code a Python transformation from a dataclass type to a new dataclass type with the transformed field types.
I realize this is pretty extreme, but the payoff would be commensurate.
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 proposta e dai riferimenti collegati a tjax leaky_integral.py, alle annotazioni di tipo di NumPy e ai pytrees di JAX. Esamina il workflow SyntheticType proposto, il passaggio di raccolta del type-checker, il formato degli stub generati e la trasformazione di dataclass, quindi determina se l'ambito può diventare un design concordato. Il lavoro sarebbe concluso con una direzione o una specifica decisa, non con una piccola modifica isolata.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- numpy, 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
- 20/100