-
-
Notifications
You must be signed in to change notification settings - Fork 10.8k
DEP: Deprecate setting the strides and dtype of a numpy array #28901
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
base: main
Are you sure you want to change the base?
Conversation
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.
Error paths are both incorrect and untested, so that is definitely required.
Other than that, should have a release note as well. But I guess you had it as draft for a reason :).
numpy/_core/src/multiarray/getset.c
Outdated
@@ -124,6 +124,11 @@ array_strides_set(PyArrayObject *self, PyObject *obj, void *NPY_UNUSED(ignored)) | |||
npy_intp upper_offset = 0; | |||
Py_buffer view; | |||
|
|||
/* DEPRECATED 2025-05-04, NumPy 2.3 */ | |||
PyErr_WarnEx(PyExc_DeprecationWarning, | |||
"Setting the strides on a Numpy array has been deprecated in Numpy 2.3.\n", |
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 could point to strided_window_view
and stride_tricks.as_strided
. Although, not sure it is needed for this one.
msg = "Setting the strides on a Numpy array has been deprecated" | ||
arr = np.arange(48) | ||
with pytest.warns(DeprecationWarning, match=msg): | ||
arr.strides = arr.strides |
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 move the tests to test_deprecations.py
. They should test the error path as well (which test_deprecations
machinery does for you, although you can do it explicitly also.).
This comment was marked as resolved.
This comment was marked as resolved.
The We could use Any other ideas on how to approach this? Update: refactored the PR to use unique references for warning. This will exclude the calls from inside |
df42838
to
2fa5086
Compare
vendored-meson/meson
Outdated
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 submodule update seems accidental
Unique reference cheeck seems OK to me, also just to be nice for a bit. Overall, I think there is no reason not to pass the As for some newer code changes here, let's avoid any warning filtering (yes I guess on new Python it's threadsafe at least, but...). Either, this still works fine now that you did the refcount check, or we should just use a (I am not certain the refcount checks will work on PyPy as is, so there might be a problem with using it for avoiding th |
af05298
to
c27f61a
Compare
The warning filtering is indeed not nice. But using a view won't work directly. For example for the recarray the dtype is updated in the array finalizer. Lines 409 to 416 in c27f61a
I can create a view with the new dtype (maybe this will create an infinite recursion, I will have to try), but I cannot replace self with the new view. |
Right, this was maybe the reason for why it wasn't deprecated before. I think we need a solution for it, though. And as much as I dislike this record array stuff and we should maybe discourage its use. My first thought is one or both of:
Unless the "unique reference" path can fix this (but I guess it didn't). The point is, if our code needs a warning manager, then we are just kicking the real solution down the road, since eventually that should be an error and the warning context manager will stop working anyway. |
b933d58
to
11f6569
Compare
Do you means checking inside the dtype setter whether the object is a subclass of masked array or recarray? That is possible (a bit inconvenient as the masked array and rec array are defined in python).
This seems a reasonable approach. Any user making use of It would be much nicer if we could rewrite the code for masked array and recarray to not update the dtype at all. If we are not able to do this, then perhaps there are users with their own subclasses that will face the same issues. For masked array the most changes are introduced in #10314 and #5943. @ahaldane @mhvk do you know whether avoiding setting the dtype in the finalizer would be possible?
The unique reference path does not help here, at the point where the recarray or masked array sets the dtype, we already have 5 references to the object.
Kicking down the solution might be ok at first. This PR is to signal users we are deprecating setting the |
I was thinking for It would be nice to just not need any of this and a great improvement to try and do something about it. But it might be a rather deep rabbit hole...
Yeah it may be fine. It would be nice not to if it isn't hard, for three reasons (none of which are quite blocking maybe):
Yes, but especially for record arrays it may be a rather deep rabbit hole as mentioned above. |
See #28800