Rank-typing
@jorenham がすでに取り組んでいます。
2025年5月4日 から。
評価
この issue はまだ評価されていません。
説明
Shape-typing is often described as being able to distinguish between axes or their their sizes. But in Python's current type-system, that isn't realistically possible. If you remove the individual axes from a shape, you end up with just a rank, a.k.a. ndim. At runtime it's trivially simple, because it's just an int. But helpfully describing it using static typing is quite the opposite, and is probably the biggest typing-challenge I've ever had to solve.
For a long time, I thought that integer tuples would be the only option we could use for this. But for many situations, that would be very impractical to use, and limited in its expressiveness. I'm not sure if it would have even been worth it.
But I recently figured out an overpowered trick that I've been calling "static embedding", with which I was able to encode the complete set of NEP 50 promotion rules within the scalar types. That made it possible to write a couple of Casts* type-aliases (powered by typing.Protocol), which, when applied to the binary scalar operator methods, reduced __init__.pyi by over 5,000 LOC (!). That made me realize that the underlying "static embedding" principle could also be used for the broadcasting rules, i.e. the main use-case of shape- rank-typing.
And so far, it's looking pretty promising. It may also provide a way to simplify the array-like typing aliases for specific ndim, by associating e.g. Sequence[Sequence[T]] with the 2-d rank-type, Rank2.
This issue is intended as a tracker for the rank-typing progress. See the PR's below for the juicy details:
- #538
- #544
- #545
- #550
- #552
- #559
- #714
For relevant discussion with additional details, see:
- numpy/numpy#16544
- python/typing#513
- #578
- 主要言語
- Python
- スター
- 79
- フォーク
- 8
- PR マージ指標
- 30日以内にマージされた PR はありません
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
numpy/numtype のほかの issue
-
topic: documentation
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
-
topic: documentation
難易度 4/5 3〜5日 初心者へのやさしさ 35/100
-
blame: Pyright tool: basedpyright
-
numpy.generic stubs: Incomplete
難易度 3/5 1〜2日 初心者へのやさしさ 35/100
-
_numtype stubs: Refactor
難易度 3/5 1〜2日 初心者へのやさしさ 45/100
似ている issue
-
bug confirmed issue
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
open-webui/open-webui#30750 · コメント 1 件 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
-
enhancement
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
OpenwaterHealth/openmotion-bloodflow-app#604 · コメント 1 件 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 70/100
-
good first issue
難易度 1/5 1時間未満 初心者へのやさしさ 90/100