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 dtype of an exact numpy.ndarray by eendebakpt · Pull Request #29244 · numpy/numpy

Conversation

@eendebakpt

@eendebakpt eendebakpt commented Jun 20, 2025 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Contributor

Partly addresses #28800. A continuation of #28901.

We only deprecate setting the dtype of an exact array. For masked and record arrays it will take some more refactoring to avoid setting the dtype. We skip warnings for pypy as we cannot rely on reference counting to avoid warnings when the dtype is changed via array.view(new_dtype).

Sorry, something went wrong.

Uh oh!

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

jorenham reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
@github-actions github-actions Bot added the 07 - Deprecation label Jun 20, 2025
@eendebakpt eendebakpt mentioned this pull request Jun 20, 2025
DEP: Deprecate setting the strides and dtype of a numpy array #28901
Closed
eendebakpt added 8 commits June 24, 2025 16:13
@eendebakpt
DEP: Deprecate setting the dtype of an exact numpy.ndarray
32d5bb1
@eendebakpt
lint
2104016
@eendebakpt
fix case for deleting dtype
8046764
@eendebakpt
Fix tests
82b03dc
@eendebakpt
Fix tests
4325801
@eendebakpt
fix pypy
5c4e089
@eendebakpt
add release note
e419b84
@eendebakpt
skip test on pypy
401cbca
@eendebakpt
eendebakpt force-pushed the deprecate_array_dtype_set branch from 3ad7ba0 to 401cbca Compare June 24, 2025 14:13
eendebakpt added 2 commits June 24, 2025 16:23
@eendebakpt
lint
f0d6eb0
@eendebakpt
fix doctest
ba0501b
eendebakpt
eendebakpt commented Jun 24, 2025
View reviewed changes
Comment thread numpy/typing/__init__.py Resolved

Uh oh!

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

eendebakpt added 2 commits July 1, 2025 12:21
@eendebakpt
news entry
3bff7b4
@eendebakpt
Merge branch 'main' into deprecate_array_dtype_set
6012691
seberg
seberg reviewed Jul 31, 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.

I had some old comments and thought I'd submit the review.

What I think I should ponder a bit is how much we need this "allow subclasses" thing. It just doesn't feel very neat TBH, even using _set_dtype() semi-publically a lot would seem nicer, but not sure that is feasible either.

Sorry, something went wrong.

Uh oh!

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

All reactions
Comment thread doc/release/upcoming_changes/29244.deprecation.rst Outdated
threads. As an alternative, you can create a new view (no copy) via:
* `np.lib.stride_tricks.strided_window_view` if applicable,
* `np.lib.stride_tricks.as_strided` for the general case,
* or the `np.ndarray` constructor (``buffer`` is the original array) for a light-weight version.

@seberg seberg Jun 25, 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.

You should be using arr.view() for this, that is what it is for!

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

#if !defined(PYPY_VERSION)
// On PyPy we skip the warning because we cannot rely on reference counts
// to be replaced with PyUnstable_Object_IsUniquelyReferenced https://github.com/python/cpython/pull/133144

@seberg seberg Jun 25, 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 are already using this in check_unique_temporary based on the Python version, would be nice to unify it.
(Maybe also with resize.)

Sorry, something went wrong.

Uh oh!

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

All reactions

@eendebakpt eendebakpt Aug 5, 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 looked at check_unique_temporary and the goal is difference, so unification does not seem worthwhile at this stage.

Here we want to check we have a guaranteed non-unique temporary. I did refactor a bit.

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.

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

Uh oh!

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

Comment thread numpy/_core/src/multiarray/methods.c Resolved

Uh oh!

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

Comment thread numpy/_core/tests/test_dtype.py
assert arr.strides == (0,)
with pytest.raises(ValueError):
arr.dtype = "i1"
with warnings.catch_warnings(): # gh-28901

@seberg seberg Jun 25, 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.

Please just use pytest.warns(). That way you don't keep it around forever.

Sorry, something went wrong.

Uh oh!

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

All reactions

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

Using pytest.warns makes the test fail on pypy (there no deprecation warning is generated). If you want, I can skip this test on pypy instead of using the catch_warnings, but it seems have to pick the lesser of two evils.

Sorry, something went wrong.

Uh oh!

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

All reactions
Comment thread numpy/lib/_stride_tricks_impl.py
# The route via `__interface__` does not preserve structured
# dtypes. Since dtype should remain unchanged, we set it explicitly.
array.dtype = x.dtype
array._set_dtype(x.dtype)

@seberg seberg Jun 25, 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.

Do you even still need this here? At this point, it's a base class and you can just assign?

Sorry, something went wrong.

Uh oh!

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

All reactions

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

The array is indeed a base class due to the asarray. But if I assign to dtype here it will generate a deprecation warning.

Maybe we can combine it with the line above as

array = np.asarray(DummyArray(interface, base=x), dtype=dtype)

But I am not sure this covers all the corner cases.

I would prefer to leave this as is for now to keep this PR as safe and simple as possible. At some later point we will probably have to tackle this one (and some others).

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.h Resolved

Uh oh!

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

Comment thread numpy/typing/__init__.py Resolved

Uh oh!

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

eendebakpt and others added 2 commits August 5, 2025 22:47
@eendebakpt @seberg
Apply suggestions from code review
b269700
Co-authored-by: Sebastian Berg <[email protected]>
@eendebakpt
refactor
800db1d
@eendebakpt
eendebakpt requested a review from seberg August 6, 2025 19:03
jorenham
jorenham reviewed Aug 6, 2025
View reviewed changes
Comment thread numpy/tests/test_warnings.py
Comment on lines 36 to +37
if p.ls[-1] == 'simplefilter' or p.ls[-1] == 'filterwarnings':
if getattr(node.args[0], "value", None) == "ignore":
if node.args and getattr(node.args[0], "value", None) == "ignore":

@jorenham jorenham Aug 6, 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 know it's very much out of scope, so please ignore my neurotic rant, but this could be so much prettier with a match / case 😅

Sorry, something went wrong.

Uh oh!

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

All reactions
@eendebakpt eendebakpt mentioned this pull request Aug 16, 2025
DEP: Deprecate setting the dtype of an numpy.ndarray #29575
Merged
@eendebakpt

eendebakpt commented Aug 16, 2025

Copy link
Copy Markdown
Contributor Author

I had some old comments and thought I'd submit the review.

What I think I should ponder a bit is how much we need this "allow subclasses" thing. It just doesn't feel very neat TBH, even using _set_dtype() semi-publically a lot would seem nicer, but not sure that is feasible either.

I created #29575 with the _set_dtype approach. Both approaches have pros and cons, I have no preference atm.

All reactions

Sorry, something went wrong.

Uh oh!

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

@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 30, 2025

Copy link
Copy Markdown
Contributor Author

Closing in favor of #29575

All reactions

Sorry, something went wrong.

Uh oh!

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

@eendebakpt eendebakpt closed this Oct 30, 2025
@johnthagen johnthagen mentioned this pull request Jun 30, 2026
Numpy 2.5 deprecation warning: Setting the dtype on a NumPy array has been deprecated MapIV/pypcd4#62
Closed
@urasakikeisuke urasakikeisuke mentioned this pull request Sep 14, 2026
Fix numpy 2.5 deprecation warning in RGB encode/decode MapIV/pypcd4#64
Merged
2 tasks done
@johannes-mueller johannes-mueller mentioned this pull request Oct 1, 2026
Setting the dtype on a NumPy array has been deprecated in NumPy 2.5. adamreeve/npTDMS#368
Closed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@jorenham jorenham jorenham left review comments
@seberg seberg Awaiting requested review from seberg

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.

3 participants

@eendebakpt @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.