Ndarray static typing: Order of generic types
![](https://secure.gravatar.com/avatar/9567056931ee2f26907b211b69d018b2.jpg?s=120&d=mm&r=g)
Hey all, With the recent merging of numpy/numpy#16759<https://github.com/numpy/numpy/pull/16759> we're at the point where `ndarray` can be made generic w.r.t. its dtype and shape. An open question which yet remains is to order in which these two parameters should appear (numpy/numpy#16547<https://github.com/numpy/numpy/issues/16547>): * `ndarray[Dtype, Shape]` * `ndarray[Shape, Dtype]` There has been a some discussion about this question in issue 16547, but a consensus has not yet to be reached. Most people seem to slightly preferring one option over the other. Are there any further thoughts on this subject? Regards, Bas van Beek
![](https://secure.gravatar.com/avatar/93a76a800ef6c5919baa8ba91120ee98.jpg?s=120&d=mm&r=g)
On Wed, Oct 28, 2020 at 2:44 PM bas van beek <bas.vanbeek@hotmail.com> wrote:
Hey all,
With the recent merging of numpy/numpy#16759 <https://github.com/numpy/numpy/pull/16759> we’re at the point where ` ndarray` can be made generic w.r.t. its dtype and shape.
An open question which yet remains is to order in which these two parameters should appear (numpy/numpy#16547 <https://github.com/numpy/numpy/issues/16547>):
· `ndarray[Dtype, Shape]`
· `ndarray[Shape, Dtype]`
Hi Bas, Thanks for driving this forward! Just to speak for myself, I don't think the precise choice matters very much. There are arguments for consistency both ways. In the end Dtype and Shape are different enough that I doubt it will be a point of confusion. Also, I would guess many users will define their own type aliases, so can write something more succinct like Float64[shape] rather than ndarray[float64, shape]. We might even consider including some of these in numpy.typing. Cheers, Stephan
There has been a some discussion about this question in issue 16547, but a consensus has not yet to be reached.
Most people seem to slightly preferring one option over the other.
Are there any further thoughts on this subject?
Regards,
Bas van Beek
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@python.org https://mail.python.org/mailman/listinfo/numpy-discussion
![](https://secure.gravatar.com/avatar/6a93ed908ce9f56701c31ca29afbec61.jpg?s=120&d=mm&r=g)
Just to speak for myself, I don't think the precise choice matters very much. There are arguments for consistency both ways.
I agree with this. In the absence of strong theoretical considerations I'd fall back to a practical one-we can make ndarray generic over dtype _right now_, while for shape we will need to wait 1+ years for the variadic type variable PEP to settle etc. To me that suggests: - Do ndarray[DType] now - When the shape stuff is ready, do ndarray[DType, ShapeStuff] (or however ShapeStuff ends up being spelled) - Write a mypy plugin that rewrites ndarray[DType] to ndarray[DType, AnyShape] (or whatever) for backwards compatibility On Thu, Oct 29, 2020 at 1:37 PM Stephan Hoyer <shoyer@gmail.com> wrote:
On Wed, Oct 28, 2020 at 2:44 PM bas van beek <bas.vanbeek@hotmail.com> wrote:
Hey all,
With the recent merging of numpy/numpy#16759 we’re at the point where `ndarray` can be made generic w.r.t. its dtype and shape.
An open question which yet remains is to order in which these two parameters should appear (numpy/numpy#16547):
· `ndarray[Dtype, Shape]`
· `ndarray[Shape, Dtype]`
Hi Bas,
Thanks for driving this forward!
Just to speak for myself, I don't think the precise choice matters very much. There are arguments for consistency both ways. In the end Dtype and Shape are different enough that I doubt it will be a point of confusion.
Also, I would guess many users will define their own type aliases, so can write something more succinct like Float64[shape] rather than ndarray[float64, shape]. We might even consider including some of these in numpy.typing.
Cheers, Stephan
There has been a some discussion about this question in issue 16547, but a consensus has not yet to be reached.
Most people seem to slightly preferring one option over the other.
Are there any further thoughts on this subject?
Regards,
Bas van Beek
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@python.org https://mail.python.org/mailman/listinfo/numpy-discussion
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@python.org https://mail.python.org/mailman/listinfo/numpy-discussion
participants (3)
-
bas van beek
-
Joshua Wilson
-
Stephan Hoyer