-
-
Notifications
You must be signed in to change notification settings - Fork 11.2k
DEP: Deprecate setting the shape attribute of a numpy array #29536
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
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice, I much prefer this, I think! I don't care if we even consider _set_shape()
semi-public. It's the users problem to know that they are mutating a fresh array if they insist, although in general, I hope there isn't really a reason to ever do so.
(__array_finalize__
is hopefully one of the very few ones.)
numpy/_core/src/multiarray/getset.c
Outdated
@@ -60,6 +60,7 @@ array_shape_set(PyArrayObject *self, PyObject *val, void* NPY_UNUSED(ignored)) | |||
"Cannot delete array shape"); | |||
return -1; | |||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess we can delete the "cannot delete" path here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. This is supposes to be called from the python side or via array_shape_set
(which checks on val
), so val
cannot be null. I will add an assert just in case.
Setting the shape attribute is now deprecated since mutating | ||
an array is unsafe if an array is shared, especially by multiple | ||
threads. As an alternative, you can create a new view via | ||
`np.reshape` or `array.reshape`. For example: `x = np.arange(15); x = np.reshape(x, (3, 5))`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should point out the option to use reshape(..., copy=False)
to ensure an error (which is one reason why some places used the shape attribute setting).
We could actually roll that out in the places where things were changed here, although overall, I am not really worried about accidental copies here.
I feel we should mention _set_shape()
somewhere maybe. Probably just here? I.e. say that if there is no alternative and you know that you are the only owner (for example within __array_finalize__
) setting the shape is still possible via _set_shape()
). This is mostly used by matrices although we discourage the behavior.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added a mention to _set_shape
without further explanation (to discourage usage).
/* Deprecated NumPy 2.4, 2025-07-29 */ | ||
if (DEPRECATE("Setting the shape on a NumPy array has been deprecated" | ||
" in NumPy 2.4.\nAs an alternative, you can create a new" | ||
" view using np.reshape." |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could also mention copy=False
here.
Co-authored-by: Sebastian Berg <sebastian@sipsolutions.net>
… deprecate_shape_v3
Addresses part of #28800. Also see the discussion in #29523.