-
-
Notifications
You must be signed in to change notification settings - Fork 10.9k
__reduce__ no longer works on user defined types #17294
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Note that if we fix this, it will not be backported to NumPy earlier than 1.19. |
Thanks for the report and good analysis! I think the solution has to be to not build the format string (or leave it empty), and then raise an error later if the |
Thanks for the interest. I'm fine with it not being backported all the way to 1.17 - I'm sure I'd be able to use a more recent version. |
@brian2brian I have a fix, but What dtype are you using (I am generally interested), and does pickling its scalar type object work (i.e. it has correct |
Ok, I guess its just that |
Yes, rational makes for an easy test case but it won't pickle even if reduce works. I have some types that represent things like dates and times but with carefully chosen representation to get the range and precision we want in the minimum size. Pickling and unpickling has been working just fine up until now including |
@brian2brian gh-17295 should fix this. Would you be able to share that code/project, especially if it has a test-suit? I am mainly curious, because I am doing larger changes, and knowing what you use, as well as additional testing to make sure we have deprecations rather than regressions, is always good. (In the long-term, chances are that all of such user-dtype/scalars will have to be reworked unfortunately). |
I wish I could share the code, but it belongs to my employer is and quite intertwined with other parts of the codebase
As long as we can still do these things then I think we'd be fine with any future refactoring |
Thanks, those things seem fine, but it may be prudent to keep an eye on the/test NumPy 1.20 release if you can. There are big changes internally, and we have to expect some smaller external things. Although, I hope none of that will affect you. Three things I would be worried about (But, my guess is that most of this isn't even possible):
|
@brian2brian one thing that would be helpful, is if there is a public compiled version to try out. Looking at code is nice, but probably not really necessary. Another thing you could do (I know its probably a much to big ask) is to set up testing against up-to-date master, which is available at: https://anaconda.org/scipy-wheels-nightly/numpy |
Note that the "nightly" builds are actually "weekly" :) |
Previously we had code that would allow exporting the buffer, but then fail for any reasonable subclass, because such a subclass should have its own user-dtype. The change is, that now a subclass without its own user-dtype will inherit the correct behaviour directly. This allows pickling of of such user-defined scalars (with user-defined dtype) if no FORMAT was requested in the buffer export. The latter allows the generic pickling code to succeed. Closes numpygh-17294
@brian2brian would it be helpful for you if this is fixed in 1.19 or is it OK if we defer it to 1.20? We can backport it, but basically we are now in a fairly late iteration of the 1.19.x branch, so if it doesn't matter for you we will likely not backport. |
I'm happy to leave it for 1.20. Many thanks for your help with this. |
I'm migrating from 1.13.3 to 1.17.4 and calling
__reduce__
no longer works on our user defined types. Ultimately this stops me being able to pickle these objects. I've verified this problem is still present in the latest release 1.19.2 and the latest master.Reproducing code example:
This can be demonstrated with the rational user defined type that is provided with numpy
On 1.13.3 this worked fine
Numpy/Python version information:
1.17.4 3.8.2 (default, Jul 16 2020, 14:00:26)
[GCC 9.3.0]
Analysis
Looks like this arises from migrating to the new buffer protocol in #10564 involving @vanossj and @eric-wieser This invokes a different code path that calls _buffer_format_string and this does not support user defined types. In fact, PyObject_GetBuffer is being called with PyBUF_SIMPLE so it won't even use the results from _buffer_format_string.
One possible fix is to support user defined types in buffer.c:_buffer_format_string
From
To
The text was updated successfully, but these errors were encountered: