-
-
Notifications
You must be signed in to change notification settings - Fork 10.9k
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
Conversation
This is great to see! A few questions/concerns:
|
|
Hi, since you were soliciting thoughts on the naming, how about |
So for now I've removed the generic aliases ( |
ndarray
generic w.r.t. its shape and dtypendarray
generic w.r.t. its shape and dtype
Thanks Bas. |
Do we have some user-facing documentation to guide in usage of these annotations? |
Remove nptyping, as numpy/numpy#17719 will add support for shape & dtype parametric ndarray types in the future NumPy release.
@stefanv There is the |
@BvB93 This is great work! Thanks for pushing 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? |
PEP 646 has been added to Python 3.11 now. So any update from Numpy side? |
This interests me too. The most recent python version is 3.12 now. Is there a place to follow the discussion on making |
Closes #16545; closes #16547.
Well folks, it's finally here: this pull requests makes the
np.ndarray
class generic w.r.t. itsshape and dtype:
np.ndarray[~Shape, ~DType]
. The shape's bound is currently setto
Any
(see "Non-Goals") while the dtype's bound is set tonp.dtype
.The "generification" of
np.ndarray
is accompanied by the addition of three types to the publicAPI of
numpy.typing
, all of which are subscriptable during runtime (similar to the likes oftyping.Sequence[~T]
):NDArray
: A generic version ofnp.ndarray[~Shape, ~DType]
.DType
: A generic version ofnp.dtype[~Scalar]
.ArrayND
: A shortcut fornp.ndarray[Any, np.dtype[~Scalar]]
. I imagine that, at some point inthe future, we can expand this with the likes of
Array0D
,Array1D
,Array2D
, etc..Non-Goals
ndarray
, similar to the likes ofcollections.abc.Sequence
versustyping.Sequence
. This will be reserved for a futurePR as the exact details might warrant some further discussion.
As the
~Shape
bound is likelly to change in the future it is recommended to type it asAny
(for now).See Typing support for shapes #16544 for the discussion on shape typing.
the arrays dtype (for now!). This is considered work for future PRs due to the jobs sheer size.