GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — DEP: Deprecate setting the shape attribute of a numpy array by eendebakpt · Pull Request #29536 · numpy/numpy

Conversation

@eendebakpt

@eendebakpt eendebakpt commented Aug 10, 2025

Copy link
Copy Markdown
Contributor

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

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
eendebakpt added 14 commits August 6, 2025 23:46
@eendebakpt
[DEP] Deprecate setting the shape attribute of a numpy array
98264cb
@eendebakpt
add release note
27ef5f2
@eendebakpt
update tests
b7842a8
@eendebakpt
more conversions
9af1e83
@eendebakpt
convert more tests
1695917
@eendebakpt
ma
1d71fc5
@eendebakpt
fix generator and mrecords
e8cbd57
@eendebakpt
review comments
354a04e
@eendebakpt
remove debugging code
bea4884
@eendebakpt
remove examples
5c8a65e
@eendebakpt
restore examples wiht only get of shape
914462d
@eendebakpt
remove doc with deprecated shape setting
7e06ce2
@eendebakpt
update release note
2e80bf0
@eendebakpt
warn for all subclasses of ndarray
d49ab1e
@github-actions github-actions Bot added the 07 - Deprecation label Aug 10, 2025
eendebakpt added 2 commits August 10, 2025 22:55
@eendebakpt
convert one more shape set
37d1b42
@eendebakpt
add deprecation test
b888253
seberg
seberg reviewed Aug 11, 2025
View reviewed changes

@seberg seberg left a comment

Copy link
Copy Markdown
Member

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.)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread 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;
}

@seberg seberg Aug 11, 2025

Copy link
Copy Markdown
Member

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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@eendebakpt eendebakpt Aug 15, 2025

Copy link
Copy Markdown
Contributor Author

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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread numpy/_core/tests/test_umath.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread doc/release/upcoming_changes/29492.deprecation.rst Outdated
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))`.

@seberg seberg Aug 11, 2025

Copy link
Copy Markdown
Member

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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@eendebakpt eendebakpt Aug 15, 2025

Copy link
Copy Markdown
Contributor Author

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).

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

seberg reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
Comment thread numpy/_core/src/multiarray/getset.c Outdated
/* 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."

@seberg seberg Aug 11, 2025

Copy link
Copy Markdown
Member

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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@mhvk mhvk Aug 28, 2025

Copy link
Copy Markdown
Contributor

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.

Specifically, maybe add " using np.reshape (with copy=False if needed)."

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
eendebakpt and others added 3 commits August 15, 2025 15:04
@eendebakpt @seberg
Update numpy/_core/tests/test_umath.py
5576aa2
Co-authored-by: Sebastian Berg <[email protected]>
@eendebakpt
review comments
5de80b9
@eendebakpt
Merge branch 'deprecate_shape_v3' of github.com:eendebakpt/numpy into…
0ff8bb0
… deprecate_shape_v3
seberg
seberg approved these changes Aug 19, 2025
View reviewed changes

@seberg seberg left a comment

Copy link
Copy Markdown
Member

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.

FWIW, I am happy to try this, if others agree mildly let's give it a shot. Might apply the suggestiong or do a minor thing, but I think this should be ready rally.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread doc/release/upcoming_changes/29492.deprecation.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread doc/release/upcoming_changes/29492.deprecation.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

eendebakpt and others added 3 commits August 24, 2025 21:27
@eendebakpt @seberg
Apply suggestions from code review
f096794
Co-authored-by: Sebastian Berg <[email protected]>
@eendebakpt
Merge branch 'main' into deprecate_shape_v3
1a237cf
@eendebakpt
Merge branch 'main' into deprecate_shape_v3
198e62a
@eendebakpt

eendebakpt commented Aug 28, 2025

Copy link
Copy Markdown
Contributor Author

Scipy and astropy are using the shape setting. I creates issues to keep track scipy/scipy#23522, astropy/astropy#18561.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@eendebakpt eendebakpt mentioned this pull request Aug 28, 2025
MAINT: Replace deprecated usage of numpy array shape, dtype and strides setting scipy/scipy#23522
Closed
mhvk
mhvk approved these changes Aug 28, 2025
View reviewed changes

@mhvk mhvk left a comment

Copy link
Copy Markdown
Contributor

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 think this all looks good! I really have only very minor comments -- to be honest hardly worth running CI again, but maybe just...

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread numpy/_core/tests/test_umath.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread numpy/_core/tests/test_umath.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread numpy/lib/_function_base_impl.py
# switch first and second axis
output[0].shape = (1, -1) + s0[2:]
output[1].shape = (-1, 1) + s0[2:]
output[0] = output[0].reshape((1, -1) + s0[2:])

@mhvk mhvk Aug 28, 2025

Copy link
Copy Markdown
Contributor

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'd tend to instead generate output separately for this case, but obviously no big deal.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@mhvk mhvk Oct 18, 2025

Copy link
Copy Markdown
Contributor

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.

Still think if makes more sense to do,

if indexing == 'xy' and ndim > 1:
    output = [np.asanyarray(x).reshape((1, -1) + s0[2:]
              for x in xi]
else:
    output = [np.asanyarray(x).reshape(s0[:i] + (-1,) + s0[i + 1:])
              for i, x in enumerate(xi)]

But I can also see that you want to minimize changes...

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread numpy/linalg/_linalg.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread numpy/ma/core.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread numpy/ma/core.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread numpy/random/_generator.pyx Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread numpy/random/mtrand.pyx Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread doc/release/upcoming_changes/29492.deprecation.rst Outdated
`np.reshape` or `np.ndarray.reshape`. For example: ``x = np.arange(15); x = np.reshape(x, (3, 5))``.
To ensure no copy is made from the data, one can use ``np.reshape(..., copy = False)``.

Directly setting the shape on an array is possible with the private method `np.ndarray._set_shape`.

@mhvk mhvk Aug 28, 2025

Copy link
Copy Markdown
Contributor

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'd add ", though is discouraged" (on a new line).

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread numpy/_core/src/multiarray/getset.c Outdated
/* 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."

@mhvk mhvk Aug 28, 2025

Copy link
Copy Markdown
Contributor

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.

Specifically, maybe add " using np.reshape (with copy=False if needed)."

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@mhvk

mhvk commented Aug 28, 2025

Copy link
Copy Markdown
Contributor

p.s. I just saw that #29523 is still alive. Is that still being considered, or are we just going for it here? (I'm fine with that.)

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@eendebakpt
review comments
f739d54
This was referenced Oct 19, 2025
BUG: avoid soon-to-be-deprecated direct mutations of ndarray.shape attributes (1/2) yt-project/yt#5307
Merged
RFC: avoid direct mutations to ndarray.shape attributes h5py/h5py#2728
Merged
@eendebakpt
review comments
f45274e
tylerjereddy
tylerjereddy reviewed Oct 19, 2025
View reviewed changes
Comment thread doc/release/upcoming_changes/29536.deprecation.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

@eendebakpt
update pr number
96e029b
@eendebakpt
eendebakpt requested a review from mhvk October 29, 2025 22:12
mhvk
mhvk approved these changes Nov 4, 2025
View reviewed changes

@mhvk mhvk left a comment

Copy link
Copy Markdown
Contributor

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.

Looks all good to me, thanks!

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@seberg

seberg commented Nov 4, 2025

Copy link
Copy Markdown
Member

I think we even checked and fixed larger downstream packages for this, right? Because in that case I think we can try to do this (can always revert if there is larger fallout).
If not, maybe let's wait until after branching since it may be a bit noisier (although I have to say that a deprecation doesn't break an end-user workflow, worst case it may slow it a bit).

neutrinoceros and mhvk reacted with thumbs up emoji
All reactions
  • 👍 2 reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@eendebakpt

eendebakpt commented Nov 7, 2025

Copy link
Copy Markdown
Contributor Author

If I recall correctly astropy, h5py and scipy have been addressed and there are PRs open for yt-project. scikit-learn still has a couple of deprecated operations I think.

neutrinoceros reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@seberg

seberg commented Nov 8, 2025

Copy link
Copy Markdown
Member

Thanks, I am tempted to wait for 2.5 then. If we are worried about fix, I think there is far more disruption to worry about here.

eendebakpt reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@seberg seberg added this to the 2.5.0 Release milestone Nov 8, 2025
@jorenham jorenham mentioned this pull request Nov 22, 2025
DEP, TYP: ndarray.shape setter pending deprecation #30282
Merged
@eendebakpt
Merge branch 'main' into deprecate_shape_v3
48cdec4
@eendebakpt

eendebakpt commented Dec 15, 2025

Copy link
Copy Markdown
Contributor Author

Thanks, I am tempted to wait for 2.5 then. If we are worried about fix, I think there is far more disruption to worry about here.

@seberg Is this ready to go in?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

eendebakpt added 2 commits January 2, 2026 20:26
@eendebakpt
Merge branch 'main' into deprecate_shape_v3
9482110
@eendebakpt
Merge branch 'deprecate_shape_v3' of github.com:eendebakpt/numpy into…
2ed9a48
… deprecate_shape_v3
seberg
seberg approved these changes Jan 5, 2026
View reviewed changes

@seberg seberg left a comment

Copy link
Copy Markdown
Member

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.

Yeah, let's get this in (assuming everything still passes).

I suppose we may see some downstream fallout, but this is why now is a good time, thanks!

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread numpy/_core/src/multiarray/getset.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

@seberg
Update numpy/_core/src/multiarray/getset.c
bfed477
@seberg

seberg commented Jan 8, 2026

Copy link
Copy Markdown
Member

Windows CI failures are just still fallout from the new warnings.

Thanks @eendebakpt, let's give this a shot. If anyone notices issues due to this during the 2.5 development cycle please leave a comment and/or open an issue. We may want to tweak this, and if it is just to advertise the hidden _set_shape() function as semi-public while documenting it to be thread (and generally logically) unsafe for non-exclusively owned arrays.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@seberg
seberg merged commit cd20e7c into numpy:main Jan 8, 2026
@neutrinoceros neutrinoceros mentioned this pull request Jan 8, 2026
BUG: incoming deprecation warnings from numpy 2.5 (dev): direct mutations to array shape attributes are deprecated astropy/astropy#19126
Closed
This was referenced Jan 9, 2026
dev numpy has deprecated shape assignment spacetelescope/jwst#10121
Closed
avoid array shape assignment spacetelescope/stdatamodels#648
Merged
@neutrinoceros neutrinoceros mentioned this pull request Jan 15, 2026
NumPy 2.5 (dev) deprecates direct mutations to ndarray.shape attributes brandon-rhodes/python-jplephem#63
Closed
@eendebakpt eendebakpt mentioned this pull request May 12, 2026
TST: suppress shape deprecation warning in test_recarray_conflict_fields #31418
Merged
@jakirkham jakirkham mentioned this pull request Jun 25, 2026
[ENH] reshape with copy keyword argument cupy/cupy#10039
Closed
This was referenced Jul 10, 2026
fix: use reshape instead of raw shape assignment for numpy arrays google-deepmind/dm_control#539
Open
DeprecationWarning: Setting the shape on a NumPy array has been deprecated in NumPy 2.5. google-deepmind/dm_control#540
Open
@erensh27 erensh27 mentioned this pull request Aug 9, 2026
fix: replace deprecated ndarray.shape assignment (NumPy 2.5) google-deepmind/dm_control#546
Open
@shubhxho shubhxho mentioned this pull request Aug 15, 2026
Reshape arrays rather than setting shape on them. google-deepmind/dm_control#548
Open
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@tylerjereddy tylerjereddy tylerjereddy left review comments
@seberg seberg seberg approved these changes
@mhvk mhvk mhvk approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

07 - Deprecation

Projects

None yet

Milestone

2.5.0 Release

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

4 participants

@eendebakpt @mhvk @seberg @tylerjereddy
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.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.