Skip to content

DEP: Undo deprecation for np.dtype() signature used by old pickles#31177

Merged
charris merged 2 commits into
numpy:mainfrom
seberg:no-deprecation-old-pickles
Apr 10, 2026
Merged

DEP: Undo deprecation for np.dtype() signature used by old pickles#31177
charris merged 2 commits into
numpy:mainfrom
seberg:no-deprecation-old-pickles

Conversation

@seberg

@seberg seberg commented Apr 7, 2026

Copy link
Copy Markdown
Member

Some (very) old pickles pass align=0, copy=1 as integers rather than bools.
This just assumes no user will really do that, and allows it.

It may be possible to also use copyreg to work around it, but since it is a rather specific signature this seems easier/just as well.

(No special tooling)

Closes gh-30786

Some (very) old pickles pass align=0, copy=1 as integers rather than
bools.
This just assumes no user will really do that, and allows it.

It may be possible to also use `copyreg` to work around it, but
since it is a rather specific signature this seems easier/just as
well.
@seberg seberg force-pushed the no-deprecation-old-pickles branch from c2a2b90 to 86662bf Compare April 7, 2026 13:58
@seberg seberg added this to the 2.4.5 release milestone Apr 9, 2026
@seberg seberg added the 09 - Backport-Candidate PRs tagged should be backported label Apr 9, 2026
@ngoldbaum

Copy link
Copy Markdown
Member

This fixes #30786, right? Ping @flying-sheep.

@seberg

seberg commented Apr 9, 2026

Copy link
Copy Markdown
Member Author

Yeah, I just didn't check if the long discussion on docs warranted leaving it open.

EDIT: but probably just as well to do.

@ngoldbaum ngoldbaum left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Storing old test data in a pickle file seems like a reasonable use-case so I'm happy to un-deprecate this. I spotted some incorrect C API uses below (with my own eyes, no Claude this time 😜).

It also maybe needs a release note? Kinda on the bubble. It is changing how the deprecations works a little though so that seems worth mentioning.

Comment thread numpy/_core/src/multiarray/descriptor.c Outdated
// Some old pickles use 0, 1 exactly, assume no user passes it
// (It may also be possible to use `copyreg` instead.)
PyLong_CheckExact(oalign) && PyLong_AsLong(oalign) == 0 &&
ocopy != NULL && PyLong_CheckExact(ocopy) && PyLong_AsLong(ocopy) == 1)) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PyLong_AsLong can fail and set OverflowError: https://docs.python.org/3/c-api/long.html#c.PyLong_AsLong. Ditto for all the other PyLong_As functions. It also looks like PyLong_AsLong is soft-deprecated - maybe use PyLong_AsInt instead?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ack... yeah, annoying to add error handling here, but clearly needed...

PyLong_AsLong isn't soft depreated of course, PyLong_AS_LONG is.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, I pushed a pretty annoying fix (annoying, because of how the control flow is).

I don't think this can ever fail, so I just made it a critical error, but I don't like baking this in when it isn't guaranteed in the C-API docs.

(The logic is really strange, because if it fails res == -1 the if branch is taken, tested locally by removing the PyLong_CheckExact which makes it possible to fail them)

@seberg

seberg commented Apr 9, 2026

Copy link
Copy Markdown
Member Author

I dunno, I was about to amend the 2.4.0 release notes to add a note that this corner case affecting pickles was solved in 2.4.5. But it feels a bit weird also?

OTOH, I don't think a release note in 2.5 seems helpful. This eases a pain in 2.4-2.4.4 (2.4.5 should get this as a backport, I think), so the only interesting point would be where you actually run into the pickle loading failure...

I'll add another comment to the original PR, I think that is the most likely place for someone actually to find the info...

@seberg seberg force-pushed the no-deprecation-old-pickles branch from 0c35300 to 1edf7b6 Compare April 9, 2026 19:28
@gdementen

Copy link
Copy Markdown
Contributor

@seberg Thanks a lot for your work

@charris charris merged commit bf18717 into numpy:main Apr 10, 2026
86 checks passed
@charris

charris commented Apr 10, 2026

Copy link
Copy Markdown
Member

Thanks Sebastian.

@seberg seberg deleted the no-deprecation-old-pickles branch April 10, 2026 12:10
@charris charris removed the 09 - Backport-Candidate PRs tagged should be backported label Apr 11, 2026
charris added a commit that referenced this pull request Apr 11, 2026
DEP: Undo deprecation for np.dtype() signature used by old pickles (#31177)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

DOC: np.save{,z} does not make clear that it’s not suited for longterm storage

5 participants