8000 ENH: Make `ndarray` generic w.r.t. its shape and dtype by BvB93 · Pull Request #17719 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

ENH: Make ndarray generic w.r.t. its shape and dtype #17719

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 5, 2020

Conversation

BvB93
Copy link
Member
@BvB93 BvB93 commented Nov 5, 2020

Closes #16545; closes #16547.

Well folks, it's finally here: this pull requests makes the np.ndarray class generic w.r.t. its
shape and dtype: np.ndarray[~Shape, ~DType]. The shape's bound is currently set
to Any (see "Non-Goals") while the dtype's bound is set to np.dtype.

The "generification" of np.ndarray is accompanied by the addition of three types to the public
API of numpy.typing, all of which are subscriptable during runtime (similar to the likes of
typing.Sequence[~T]):

  • NDArray: A generic version of np.ndarray[~Shape, ~DType].
  • DType: A generic version of np.dtype[~Scalar].
  • ArrayND: A shortcut for np.ndarray[Any, np.dtype[~Scalar]]. I imagine that, at some point in
    the future, we can expand this with the likes of Array0D, Array1D, Array2D, etc..

Non-Goals

  • EDIT: Providing runtime-subscriptable aliases for ndarray, similar to the likes of
    collections.abc.Sequence versus typing.Sequence. This will be reserved for a future
    PR as the exact details might warrant some further discussion.
  • We currently lack the tools to properly type the ndarray's shape (i.e. we lack variadics).
    As the ~Shape bound is likelly to change in the future it is recommended to type it as Any (for now).
    See Typing support for shapes #16544 for the discussion on shape typing.
  • The various functions and methods in numpy currently lack the necessary overloads to the utilize
    the arrays dtype (for now!). This is considered work for future PRs due to the jobs sheer size.

@shoyer
Copy link
Member
shoyer commented Nov 6, 2020

This is great to see!

A few questions/concerns:

  1. Why NDArray[~Shape, ~DType] instead of NDArray[~Shape, ~Scalar]?
  2. In principle, Array0D, Array1D, etc, sounds useful (it really depends on how shape typing works out), but the similarity of the names NDArray and ArrayND feels a little confusing to me. I wonder if we could think of another name?

@BvB93
Copy link
Member Author
BvB93 commented Nov 6, 2020

@shoyer

  1. This was based on earlier discussion in ENH: make ndarray generic over dtype numpy-stubs#48. The main concern was that the scalar types (hierarchy) might not always be quite enough on its own, especially with the big dtype update of NEP 41 & 42 in mind. Making ndarray generic w.r.t. np.dtype instead of np.generic thus provides us a bit more flexibility.
  2. I see your point, Yes. Perhaps something along the lines of SimpleNDArray or SimpleArray?
    If anyone has a suggestion here, feel free to chime in.

@a-reich
Copy link
a-reich commented Nov 15, 2020

Hi, since you were soliciting thoughts on the naming, how about ArrayAnyShape or ArrayUnshaped?

@BvB93
Copy link
Member Author
BvB93 commented Nov 26, 2020

So for now I've removed the generic aliases (NDAr 8000 ray, ArrayND, etc.) from this PR.
As they might warrant some additional discussion, they'll be reserved for a follow-up (and I'd like to get this PR merged ASAP in 1.21).

@BvB93 BvB93 changed the title ENH,API: Make ndarray generic w.r.t. its shape and dtype ENH: Make ndarray generic w.r.t. its shape and dtype Nov 28, 2020
@charris charris merged commit 643bc75 into numpy:master Dec 5, 2020
@charris
Copy link
Member
charris commented Dec 5, 2020

Thanks Bas.

@stefanv
Copy link
Contributor
stefanv commented Dec 29, 2020

Do we have some user-facing documentation to guide in usage of these annotations?

Zeta611 added a commit to Zeta611/golpy that referenced this pull request Jan 1, 2021
Remove nptyping, as numpy/numpy#17719 will add
support for shape & dtype parametric ndarray types in the future NumPy
release.
@BvB93
Copy link
Member Author
BvB93 commented Jan 11, 2021

@stefanv There is the numpy.typing docs page, though nothing specifically for ndarray[~Shape, ~DType] yet, as this PR was just aimed at making ndarray a parametrized generic. I'm planning on exposing a runtime-subscriptable version of np.ndarray soon, something which I'd like to accompany with a documentation update.

@janosh
Copy link
janosh commented Aug 25, 2021

@BvB93 This is great work! Thanks for pushing numpy.typing!

I don't have a good overview though. What's the status quo? Is it possible to type hint the dimensionality of an array? Or even the specific shape?

@BvB93
Copy link
Member Author
BvB93 commented Aug 26, 2021

@janosh for shape-typing (xref #16544) we're still waiting on PEP 646; until that time it's unfortunately a no on both dimensionality- and shape-typing.

@forrestbao
Copy link

PEP 646 has been added to Python 3.11 now. So any update from Numpy side?

@muxator
Copy link
muxator commented Oct 17, 2023

This interests me too. The most recent python version is 3.12 now. Is there a place to follow the discussion on making ndarray parametric wrt its shape?

@Jacob-Stevens-Haas
Copy link
Contributor
9DEB
Jacob-Stevens-Haas commented Jul 3, 2024

@muxator, see #26081 and #16544

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Order of generic types for ndarray Typing support for dtypes
9 participants
0