Proposal: Programmatically create types
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 1.8k
- Forks
- 302
- Ø Merge
- 23 Std.
- Gemergte PRs (30 T.)
- 8
Beschreibung
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.
Beitragsleitfaden
Für dieses Repository ist kein Beitragsleitfaden indexiert
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginne mit dem Proposal und den darin verlinkten Referenzen auf tjax leaky_integral.py, NumPy-Typannotationen und JAX-pytrees. Prüfe den vorgeschlagenen SyntheticType-Workflow, den Schritt zur Sammlung des Type-Checkers, das Format der generierten Stubs und die Dataclass-Transformation; ermittle anschließend, ob der Umfang zu einem abgestimmten Design werden kann. Erledigt wäre die Aufgabe erst mit einer festgelegten Richtung oder Spezifikation und nicht mit einer kleinen isolierten Änderung.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- numpy, python
- Bereich
- devtools
- Issue-Typ
- Feature
- Schwierigkeit
- 5/5
- Geschätzter Aufwand
- Über eine Woche
- Aktivitätsstatus
- Veraltet
- Klarheit
- Muss geklärt werden
- Anfängerfreundlichkeit
- 20/100