8000 DEP: Deprecate setting the strides and dtype of a numpy array by eendebakpt · Pull Request #28901 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

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

Closed
wants to merge 15 commits into from
Next Next commit
DEP: Deprecate setting the dtype or strides of a numpy array
  • Loading branch information
eendebakpt committed May 4, 2025
commit 76303cf13778198e672b25f1274dbdc77a511210
15 changes: 13 additions & 2 deletions numpy/_core/src/multiarray/getset.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ array_shape_set(PyArrayObject *self, PyObject *val, void* NPY_UNUSED(ignored))
/* Free old dimensions and strides */
npy_free_cache_dim_array(self);
((PyArrayObject_fields *)self)->nd = nd;
((PyArrayObject_fields *)self)->dimensions = _dimensions;
((PyArrayObject_fields *)self)->dimensions = _dimensions;
((PyArrayObject_fields *)self)->strides = _dimensions + nd;

if (nd) {
Expand All @@ -95,7 +95,7 @@ array_shape_set(PyArrayObject *self, PyObject *val, void* NPY_UNUSED(ignored))
}
else {
/* Free old dimensions and strides */
npy_free_cache_dim_array(self);
npy_free_cache_dim_array(self);
((PyArrayObject_fields *)self)->nd = 0;
((PyArrayObject_fields *)self)->dimensions = NULL;
((PyArrayObject_fields *)self)->strides = NULL;
Expand Down Expand Up @@ -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",
Copy link
Member

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.

1);

if (obj == NULL) {
PyErr_SetString(PyExc_AttributeError,
"Cannot delete array strides");
Expand Down Expand Up @@ -372,6 +377,12 @@ array_descr_set(PyArrayObject *self, PyObject *arg, void *NPY_UNUSED(ignored))
{
PyArray_Descr *newtype = NULL;

/* DEPRECATED 2025-05-04, NumPy 2.3 */
PyErr_WarnEx(PyExc_DeprecationWarning,
"Setting the dtype on a Numpy array has been deprecated in Numpy 2.3.\n"
"Instead of changing the dtype on an array x, create a new array with numpy.frombuffer(x, dtype=new_dtype)",
1);

if (arg == NULL) {
PyErr_SetString(PyExc_AttributeError,
"Cannot delete array dtype");
Expand Down
4 changes: 2 additions & 2 deletions numpy/_core/tests/test_nditer.py
Original file line number Diff line number Diff line change
Expand Up @@ -854,7 +854,7 @@ def test_iter_nbo_align_contig():

# Unaligned input
a = np.zeros((6 * 4 + 1,), dtype='i1')[1:]
a.dtype = 'f4'
a = a.view('f4')
a[:] = np.arange(6, dtype='f4')
assert_(not a.flags.aligned)
# Without 'aligned', shouldn't copy
Expand Down Expand Up @@ -1799,7 +1799,7 @@ def test_iter_buffering():
arrays.append(np.arange(10, dtype='f4'))
# Unaligned array
a = np.zeros((4 * 16 + 1,), dtype='i1')[1:]
a.dtype = 'i4'
a = a.view('i4')
a[:] = np.arange(16, dtype='i4')
arrays.append(a)
# 4-D F-order array
Expand Down
0