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: Correct dtype in `__array_finalize__` if _set_dtype=None by seberg · Pull Request #31293 · numpy/numpy

Conversation

@seberg

@seberg seberg commented Apr 21, 2026 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Member

This avoids using the correct dtype in array_finalize unless _set_dtype = None is set on the class.
We also use this new path to make record arrays follow the main class deprecation correctly.

This means that recarray does live in the future.

CC @mhvk, hopefully the last iteration (unless the larger view change works out)...

(no AI tool use)

Closes gh-31281

Sorry, something went wrong.

Uh oh!

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

All reactions
seberg added 2 commits April 21, 2026 12:20
@seberg
DEP: Correct dtype in __array_finalize__ if _set_dtype=None
3e4dab3
This avoids using the correct dtype in __array_finalize__ *unless*
`_set_dtype = None` is set on the class.
We also use this new path to make record arrays follow the main
class deprecation correctly.

This means that `recarray` does live in the future.
@seberg
Add release note fragment
28fb29f
@seberg

seberg commented Apr 21, 2026 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Member Author

Hmmmm, or should we even only allow _set_dtype = None, but implementing it as a function does seem more obvious... moving things into __array_finalize__ should be possible, but might be tricky...

EDIT: So one thing of course is, that if _set_dtype is None, you cannot use subclass._set_dtype() to avoid the dtype attribute setting deprecation. At some point maybe that is niche enough that it doesn't matter... But I guess it may be enough of a reason to need the function pattern...

All reactions

Sorry, something went wrong.

Uh oh!

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

seberg
seberg commented Apr 21, 2026
View reviewed changes
Comment thread numpy/_core/records.py Outdated

def __array_finalize__(self, obj):
if self.dtype.type is not record and self.dtype.names is not None:
if issubclass(self.dtype.type, nt.void) and self.dtype.names is not None:

@seberg seberg Apr 21, 2026

Copy link
Copy Markdown
Member 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.

This is the logic used in __setattr__, basically the same I guess, but thought I'd just copy paste it.

Sorry, something went wrong.

Uh oh!

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

All reactions

@seberg seberg Apr 21, 2026 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Member 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.

EDIT: Actually, no, I'll just undo this to avoid creating a new dtype here if it isn't necessary. After that dtype.type is always record, so that check should be enough.

Made it to have both checks. The second one is needed for some ridiculous tests to pass, but the original one is definitely OK as a fast-path.

Sorry, something went wrong.

Uh oh!

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

All reactions
@seberg
__array_finalize__ path: keep old precise check, but also __setattr__…
6895cbc
… one
@seberg
seberg force-pushed the no-array-finalize-dtype branch from ddec556 to 6895cbc Compare April 21, 2026 11:03
mhvk
mhvk approved these changes Apr 21, 2026
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.

This does seem a good path forward! My comments are minimal, mostly about comments, so will approve now.

p.s. I may still follow up with changing the dims & strides beforehand.

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 doc/release/upcoming_changes/31293.deprecation.rst Outdated
* Set ``_set_dtype = None`` in which case ``arr.view(dtype=new_dtype)``
will call ``__array_finalize__`` with the new dtype, ensuring that
any validation ``__array_finalize__`` will run is done.
* Define ``_set_dtype`` as a function (calling ``ndarray._set_dtype()``

@mhvk mhvk Apr 21, 2026

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.

How about, "Of, for a quick fix to avoid DeprecationWarning, define ..."

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/convert.c Outdated
* setter): create subclass view first, use the setter, but
* emit a deprecation asking to implement _set_dtype instead.
* 3. Otherwise (including plain ndarray): create an ndarray base
* 3. If _set_dtype is None (or base-class): create an ndarray base

@mhvk mhvk Apr 21, 2026

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 "or base-class" is incorrect, otherwise how would one ever get to path 4?

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/convert.c Outdated
* 3. If _set_dtype is None (or base-class): create an ndarray base
* view, set dtype internally, then create the subclass view
* if needed. __array_finalize__ sees the final dtype+shape.
* 4. Otherwise, call `__array_finalize__` with old dtype and forcibly

@mhvk mhvk Apr 21, 2026

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.

Maybe you meant to write it here, like If _set_dtype is NULL or base-class: ...

Sorry, something went wrong.

Uh oh!

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

All reactions
Comment thread numpy/_core/src/multiarray/convert.c Outdated
* with correct dtype+shape, this will call `__array_finalize__`
* with the final dtype+shape.
*/
if (use_dtype_in_finalize && subtype != &PyArray_Type) {

@mhvk mhvk Apr 21, 2026

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.

use_dtype_in_finalize can only be set if it is a subclass, so second part can be omitted.

Sorry, something went wrong.

Uh oh!

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

All reactions
@seberg
Address review comments/tweak in-line comments a bit more
36867f5
@seberg
seberg merged commit 388bd75 into numpy:main Apr 21, 2026
84 checks passed

Uh oh!

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

@seberg
seberg deleted the no-array-finalize-dtype branch April 21, 2026 14:57
@charris charris added the 07 - Deprecation label Apr 21, 2026
@mhvk

mhvk commented Apr 22, 2026

Copy link
Copy Markdown
Contributor

@seberg - the addition of _set_dtype = None to np.recarray breaks a FITS_rec subclass in astropy. This means older astropy cannot be used with numpy 2.5. Personally, I don't think this is a disaster -- the fix is easy and can be backported (and people should not expect to be able to run newest numpy with older astropy), but I've raised it to ask for others' opinion. See astropy/astropy#19584

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

@mhvk mhvk mentioned this pull request Apr 22, 2026
Dealing with deprecation of setting dtype in record arrays, and unitless times astropy/astropy#19584
Merged
1 task done
MaanasArora pushed a commit to MaanasArora/numpy that referenced this pull request May 7, 2026
@seberg @MaanasArora
DEP: Correct dtype in __array_finalize__ if _set_dtype=None (numpy#…
fe1f8b2
…31293)

This avoids using the correct dtype in array_finalize unless _set_dtype = None is set on the class.
We also use this new path to make record arrays follow the main class deprecation correctly.

This means that recarray does live in the future.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@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

No milestone

Development

Successfully merging this pull request may close these issues.

BUG: a regression from gh-31234 seen in astropy (broadcasting error while creating views of a Masked array)

Uh oh!

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

3 participants

@seberg @mhvk @charris
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.