Skip to content

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

Open
wants to merge 19 commits into
base: main
Choose a base branch
from

Conversation

eendebakpt
Copy link
Contributor

Addresses part of #28800. Also see the discussion in #29523.

Copy link
Member

@seberg seberg left a 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.)

@@ -60,6 +60,7 @@ array_shape_set(PyArrayObject *self, PyObject *val, void* NPY_UNUSED(ignored))
"Cannot delete array shape");
return -1;
}
Copy link
Member

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.

Copy link
Contributor Author

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))`.
Copy link
Member

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.

Copy link
Contributor Author

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."
Copy link
Member

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.

eendebakpt and others added 3 commits August 15, 2025 15:04
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.

2 participants