python / python/typing

Proposal: Programmatically create types

Open
#1,371 18 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

topic: feature
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=float and U=numpy.ndarray[typing.Any, numpy.dtype[numpy.floating[typing.Any]]]).
  • It would then write the results to some pyi file 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

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.