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 an exact numpy array by eendebakpt · Pull Request #29523 · numpy/numpy

Conversation

@eendebakpt

@eendebakpt eendebakpt commented Aug 6, 2025

Copy link
Copy Markdown
Contributor

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

Sorry, something went wrong.

Uh oh!

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

All reactions
eendebakpt added 9 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 eendebakpt mentioned this pull request Aug 6, 2025
[DEP] Deprecate setting the shape attribute of a numpy array #29492
Closed
@eendebakpt eendebakpt changed the title Deprecate setting the shape attribute of a numpy array DEP] Deprecate setting the shape attribute of a numpy array Aug 6, 2025
@jorenham jorenham changed the title DEP] Deprecate setting the shape attribute of a numpy array DEP: Deprecate setting the shape attribute of a numpy array Aug 6, 2025
@jorenham jorenham added the 07 - Deprecation label Aug 6, 2025
eendebakpt added 2 commits August 7, 2025 20:48
@eendebakpt
remove examples
5c8a65e
@eendebakpt
restore examples wiht only get of shape
914462d
@ngoldbaum

ngoldbaum commented Aug 7, 2025

Copy link
Copy Markdown
Member

Looks like there's one docstring that still needs an update:

_________________________ [doctest] basics.copies.rst __________________________
111 
112     >>> import numpy as np
113     >>> x = np.ones((2, 3))
114     >>> y = x.T  # makes the array non-contiguous
115     >>> y
116     array([[1., 1.],
117            [1., 1.],
118            [1., 1.]])
119     >>> z = y.view()
120     >>> z.shape = 6
Differences (unified diff with -expected +actual):
    @@ -1,4 +1,8 @@
     Traceback (most recent call last):
    -   ...
    -AttributeError: Incompatible shape for in-place modification. Use
    -`.reshape()` to make a copy with the desired shape.
    +  File "/home/circleci/.pyenv/versions/3.11.10/lib/python3.11/doctest.py", line 1355, in __run
    +    exec(compile(example.source, filename, "single",
    +  File "<doctest basics.copies.rst[22]>", line 1, in <module>
    +    z.shape = 6
    +    ^^^^^^^
    +DeprecationWarning: Setting the shape on a NumPy array has been deprecated in NumPy 2.4.
    +As an alternative, you can create a new view using np.reshape.

/home/circleci/repo/doc/source/user/basics.copies.rst:120: DocTestFailure
All reactions

Sorry, something went wrong.

Uh oh!

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

ngoldbaum
ngoldbaum reviewed Aug 7, 2025
View reviewed changes
Comment thread doc/release/upcoming_changes/29492.deprecation.rst
an array is unsafe if an array is shared, especially by multiple
threads. As an alternative, you can create a new view (no copy) via
`np.reshape` or `array.reshape`.

@ngoldbaum ngoldbaum Aug 7, 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.

any chance you can add a more fleshed out concrete code example showing exactly what to do?

Sorry, something went wrong.

Uh oh!

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

All reactions

@ngoldbaum ngoldbaum Aug 7, 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 wonder if someone could write a ruff lint for this?

Sorry, something went wrong.

Uh oh!

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

All reactions

@jorenham jorenham Aug 7, 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.

A NPY rule would indeed be nice. But those of us that use a type-checker should already see a deprecation message appear in their IDE.

Sorry, something went wrong.

Uh oh!

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

All reactions

@ngoldbaum ngoldbaum Aug 7, 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.

The nice thing about adding ruff rules is it can automatically fix issues for you.

Sorry, something went wrong.

Uh oh!

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

All reactions

@seberg seberg Aug 8, 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.

Can you add a ruff rule though? You have a new object, so it just isn't a safe rule to just apply (in part for the exact same reason that we want this, because it is weird to change the shape on the other side of a program).

Sorry, something went wrong.

Uh oh!

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

All reactions
eendebakpt added 2 commits August 7, 2025 23:02
@eendebakpt
remove doc with deprecated shape setting
7e06ce2
@eendebakpt
update release note
2e80bf0
@seberg

seberg commented Aug 8, 2025

Copy link
Copy Markdown
Member

iv) might give issues with subclasses that override setting the shape (is that possible?)

It seems possible, but rather raw:

class arr(np.ndarray):
    @property
    def shape(self):
        return super().shape
    @shape.setter
    def shape(self, val):
        np.ndarray.shape.__set__(self, val)

(I suppose you may be able to walk the __mro__ like super there somehow). But it seems arcane enough that nobody should do it (and why would you?)).

I still think option iv might be nicer, just because the need for this should only arise in the subclass implementation itself and not for end-users. But happy to go with this initially (even this might be quite noisy, of course).

All reactions

Sorry, something went wrong.

Uh oh!

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

@eendebakpt

eendebakpt commented Aug 10, 2025

Copy link
Copy Markdown
Contributor Author

I still think option iv might be nicer, just because the need for this should only arise in the subclass implementation itself and not for end-users. But happy to go with this initially (even this might be quite noisy, of course).

For setting the shape the only complex part is in np.matrixlib which is already deprecated (7 years ago, maybe time to remove it?) so option iv) might indeed be better. I will open a PR with this approach.

All reactions

Sorry, something went wrong.

Uh oh!

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

@eendebakpt eendebakpt changed the title DEP: Deprecate setting the shape attribute of a numpy array DEP: Deprecate setting the shape attribute of an exact numpy array Aug 10, 2025
@eendebakpt eendebakpt mentioned this pull request Aug 10, 2025
DEP: Deprecate setting the shape attribute of a numpy array #29536
Merged
@seberg

seberg commented Aug 11, 2025

Copy link
Copy Markdown
Member

Matrix deprecation always waited on SciPy sparse matrix being replaced. That process finally started, but it may not be quite far enough yet to actually do it.
(Plus, maybe we need to escalate the deprecation a bit first considering the impact.)

All reactions

Sorry, something went wrong.

Uh oh!

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

@seberg

seberg commented Aug 11, 2025

Copy link
Copy Markdown
Member

Ah, sorry, I see you just had to change how the shape is handled there (i.e. that weird offender) and not actually need to get rid of it. To me that seems actually pretty nice and not too complex if it works!

All reactions

Sorry, something went wrong.

Uh oh!

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

@eendebakpt
Merge branch 'main' into deprecate_shape_v2
1d086e8
@mhvk mhvk mentioned this pull request Oct 7, 2025
TSK: deprecate setting strides, dtype, and shape on arrays. #28800
Closed
3 of 4 tasks
@eendebakpt

eendebakpt commented Oct 18, 2025

Copy link
Copy Markdown
Contributor Author

Closing this PR in favor of #29536

All reactions

Sorry, something went wrong.

Uh oh!

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

@eendebakpt eendebakpt closed this Oct 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@seberg seberg seberg left review comments
@ngoldbaum ngoldbaum ngoldbaum left review comments
@jorenham jorenham jorenham left review comments

Uh oh!

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

Assignees

No one assigned

Labels

07 - Deprecation

Projects

None yet

Milestone

No milestone

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 @ngoldbaum @seberg @jorenham
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.