Syntax for typing multi-dimensional arrays
还没有人认领这个 Issue。
- 主要语言
- Python
- 星标
- 1.8k
- 派生
- 302
- 平均合并
- 23 小时
- 30 天内合并 PR
- 8
描述
As part of the larger project for multi-dimensional arrays (https://github.com/python/typing/issues/513), one of the first questions I would like to settle is what syntax for typing data-types and shapes should look like.
Both dtype and shape should be optional, and it should be possible to define multi-dimensional arrays for which either or both of these are generic:
dtype: indicates the data type for array elements, e.g.,np.float64shape: indicates the shape of the multi-dimensional array, a tuple of zero or more integers. We would like to support integer and variable sized dimensions, and variable numbers of dimensions. These are most naturally represented with indexing by a variadic number of integer, variable, colon:and/or ellipsis...arguments, e.g.,NDArray[1, N, :, ...]for an array with dimensions of size 1, sizeN, and arbitrary size, followed by 0 or more arbitrary sized dimensions.
For NumPy, ideally we would like to add basic typing support for dtype (using Generic) even before typing for shape is possible. But we'd like to know what the ultimate syntax should look like, so we don't paint ourselves into a corner.
One key question: can we safely rely on using a single generic argument for dtypes (e.g., np.ndarray[np.float64]) as indicating an array without any shape constraints?
My doc (same as in the master issue) considers a number of options under the "Possible syntax" section.
So far, I think the best option is some variation of "two generic arguments", for dtype and shape. But this could quickly get annoyingly verbose when sprinkled all over a code-base, e.g., np.ndarray[np.float32, Shaped[..., N, M]]:
- It would be nice to support syntax like
np.ndarray[np.float32](the multi-dimensional equivalent ofList[float]) as an alias fornp.ndarray[np.float32, Any], but we don't yet have optional arguments for generics (variadic arguments are a somewhat awkward fit for a single argument). - It would also be nice to allow omitting
Shaped[], e.g., by writing dimensions as variadic generics to the array type likenp.ndarray[np.float32, ..., N, M]. One possible ambiguity is how to specify scalar arrays:np.ndarray[np.float32,]looks very similar tonp.ndarray[np.float32]. But scalar arrays are rare enough that these could potentially be resolved by disallowingnp.ndarray[np.float32,]in favor of requiringnp.ndarray[np.float32, Shape[()]].
贡献指南
这个仓库没有索引到贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
从更大的多维数组 issue (#513) 和链接的设计文档开始,尤其是其中的“Possible syntax”部分。比较提议的 dtype 和 shape 形式,包括泛型参数和可变参数,并审阅现有讨论。完成的标准是确定并记录一种已达成共识的语法,支持所述的可选和泛型情况。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- numpy, python
- 领域
- developer-experience, tooling
- Issue 类型
- 功能
- 难度
- 5/5
- 预计耗时
- 一周以上
- 活跃度
- 停滞
- 描述清晰度
- 需要澄清
- 新手友好度
- 25/100