Skip to content

[DEP] Deprecate setting the shape attribute of a numpy array #29492

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

Closed
wants to merge 0 commits into from

Conversation

eendebakpt
Copy link
Contributor

@eendebakpt eendebakpt commented Jul 31, 2025

Addresses part of #28800.

Notes:

  • for small arrays the changes could have a minor performance impact as x.reshape(shape) returns a view (e.g. a new object). We could optimize perhaps by returning the same array if the shape is unchanged, but I am not sure this is worth it.

  • Setting the shape in __array_finalize__ (e.g. here) is problematic: using reshape is not possible as we have to modify the self. This is seems rather safe (since we are still in __array_finalize__ it would be strange for other threads to already have access to the object), but we have to deal with it somehow.
    Some options: i) suppress the warning here ii) issue the warning only for exact np.ndarray iii) refactor the matrixlib (and others) to not set the shape iv) use in private method to set the shape.
    Option i) is not very performant iii) I do not know how to do, iv) might give issues with subclasses that override setting the shape (is that possible?). So I am picking option ii) for now (same approach as in the PR where dtype setting is deprecated [DEP] Deprecate setting the shape attribute of a numpy array #29492)

@eendebakpt eendebakpt changed the title Draft: [DEP] Deprecate setting the shape attribute of a numpy array [DEP] Deprecate setting the shape attribute of a numpy array Aug 6, 2025
@eendebakpt eendebakpt requested a review from jorenham August 6, 2025 20:34
Copy link
Member

@jorenham jorenham left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The stub changes are nice and pretty now.

You seem to have accidentally updated the vendored meson git submodule, though

@jorenham
Copy link
Member

jorenham commented Aug 6, 2025

what happened?

@eendebakpt
Copy link
Contributor Author

My mistake. Closing in favor of #29523

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