[DEP] Deprecate setting the shape attribute of a numpy array #29492
+0
−0
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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: usingreshape
is not possible as we have to modify theself
. 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)