Skip to content

BUG: Allow pickling all relevant DType types/classes#18332

Merged
charris merged 1 commit intonumpy:masterfrom
seberg:issue-18325
Feb 6, 2021
Merged

BUG: Allow pickling all relevant DType types/classes#18332
charris merged 1 commit intonumpy:masterfrom
seberg:issue-18325

Conversation

@seberg
Copy link
Copy Markdown
Member

@seberg seberg commented Feb 4, 2021

Introducing the metaclass without a canonical top-level name broke
pickling of type(np.dtype(...)) (which was always just np.dtype).

While a better solution will likely be possible by making the
DTypes HeapTypes and this solution may not work for all imaginable
cases (i.e. it is plausible for a dtype to not have a scalar type
associated), using a copyreg registration for the metaclass
surprisingly works without any issues and seems like the simplest
solution right now.

Closes gh-16692, gh-18325

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.

PEP 8 wants two blank line between top level function definitions.

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.

Fixed, had copied it from above, but rather changed it there (and moved the copyreg to be in one spot).

@seberg seberg force-pushed the issue-18325 branch 3 times, most recently from ed17fd4 to dea777d Compare February 4, 2021 22:42
Copy link
Copy Markdown
Member Author

@seberg seberg Feb 4, 2021

Choose a reason for hiding this comment

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

There was a subtlety/issue here. the change would have made np.dtype pickle in a way that older NumPy versions cannot unpickle. But when we pickle np.dtype("f") for example, we also pickle np.dtype to unpickle it.

That would have meant that any pickle containing a dtype instance (not class) would not have been unpickable on older NumPy versions. For classes we cannot avoid that, but I am pretty sure that it would be inconvenient for the instances.

(EDIT: To be clear, in the initial version, I had return None and then special cased that in the reconstruct function.)

Introducing the metaclass without a canonical top-level name broke
pickling of `type(np.dtype(...))` (which was always just `np.dtype`).

While a better solution will likely be possible by making the
DTypes HeapTypes and this solution may not work for all imaginable
cases (i.e. it is plausible for a dtype to not have a scalar type
associated), using a `copyreg` registration for the metaclass
surprisingly works without any issues and seems like the simplest
solution right now.

Closes numpygh-16692, numpygh-18325
@charris
Copy link
Copy Markdown
Member

charris commented Feb 5, 2021

@seberg are you happy with this now?

@seberg
Copy link
Copy Markdown
Member Author

seberg commented Feb 5, 2021

Yes, I think it should be good; if someone else with a pickle mind has a quick look, that would be great, though :).

@charris charris merged commit cba30db into numpy:master Feb 6, 2021
@charris charris removed the 09 - Backport-Candidate PRs tagged should be backported label Feb 6, 2021
@charris charris removed this from the 1.20.1 release milestone Feb 6, 2021
@seberg seberg deleted the issue-18325 branch February 6, 2021 19:44
@jakirkham
Copy link
Copy Markdown
Contributor

Thanks all! 😀

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.

np.dtype('float64').__class__ not picklable in numpy master

3 participants