Proposal: Programmatically create types
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 1.8k
- Forks
- 302
- Avg merge
- 23h
- Merged PRs (30d)
- 8
Description
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.
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the proposal and its linked tjax leaky_integral.py, NumPy type annotations, and JAX pytrees references. Review the suggested SyntheticType workflow, type-checker collection step, generated stub format, and dataclass transformation, then determine whether the scope can become an agreed design. Done would require a decided direction or specification rather than a small isolated change.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- numpy, python
- Domain
- devtools
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 20/100