python / python/typing

Typing for multi-dimensional arrays

オープン
#513 コメント 21 件 リアクション 74 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

topic: feature
主要言語
Python
スター
1.8k
フォーク
302
平均マージ
23時間
マージ済み PR(30日)
8

説明

I'd like to open a discussion about typing for multi-dimensional arrays in general, and more specifically for NumPy. We have already been discussing this over in the NumPy issue tracker (https://github.com/numpy/numpy/issues/7370) and recently opened a new repository to start writing type stubs (https://github.com/numpy/numpy_stubs).

To help guide discussion, I wrote a document outlining ideas for array shape typing.

To summarize:

  • We would like to be able to type-check both data types (e.g., float64) and shapes (e.g., a 3x4 array) for multi-dimensional arrays.
  • There are many uses cases where support for checks using dimension identity would be valuable, e.g., to indicate that a function transforms an array with shape (N, M) to shape (N,) for arbitrary integers N and M. These dimension variables look very similar to TypeVar, if TypeVar supported integers as types.
  • A notion of "zero or more additional dimensions" would also be quite valuable, and is a core part of the type for many NumPy operations (generalized ufuncs). This might be naturally written with Ellipsis, e.g., (...., N) for an array with a last dimension of length N and any number of proceeding dimensions. There are particular rules (broadcasting) that should be enforced for matching multiple arguments with variable numbers of dimensions.

This will likely require some new typing features (as well as type-checker support). Notably:

  • Support for literal values (https://github.com/python/typing/issues/478), so we can type check operations like array.sum(axis=0).
  • Variadic generics (https://github.com/python/typing/issues/193), we can write types like NDArray[N] and NDArray[N, M].
  • Some sort of support for dimension identity in shapes (e.g., integer types, or DimensionVar as described in my doc).
  • Standard syntax for writing array dtype/shape annotations: what should these look like?

コントリビューションガイド

このリポジトリのコントリビューションガイドは索引されていません

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

まず issue、リンクされている NumPy issue、numpy_stubs リポジトリを読み、その後、配列の shape の型付けに関するリンク先のドキュメントを確認してください。リテラル値、可変長ジェネリクス、次元の同一性、アノテーション構文に関する提案を比較してください。この issue では、具体的な実装目標も、完了を確認できるテストも定義されていません。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
numpy, python
領域
tooling
issue の種類
機能追加
難易度
5/5
見積もり時間
1週間以上
活発さ
停滞
明瞭さ
説明が足りない
初心者へのやさしさ
25/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。