Proposal: Programmatically create types
まだ誰も着手していません。
- 主要言語
- Python
- スター
- 1.8k
- フォーク
- 302
- 平均マージ
- 23時間
- マージ済み PR(30日)
- 8
説明
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.
コントリビューションガイド
このリポジトリのコントリビューションガイドは索引されていません
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
提案と、そこからリンクされている tjax leaky_integral.py、NumPy の型アノテーション、JAX の pytrees の参照から始めます。提案されている SyntheticType ワークフロー、type-checker の収集ステップ、生成されるスタブの形式、dataclass の変換を確認し、そのうえでスコープを合意された設計にできるか判断します。完了とみなすには、小さく孤立した変更ではなく、方向性または仕様が決定されている必要があります。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- numpy, python
- 領域
- devtools
- issue の種類
- 機能追加
- 難易度
- 5/5
- 見積もり時間
- 1週間以上
- 活発さ
- 停滞
- 明瞭さ
- 説明が足りない
- 初心者へのやさしさ
- 20/100