8000 DEP: Deprecate setting the dtype or strides of a numpy array · numpy/numpy@76303cf · GitHub
[go: up one dir, main page]

Skip to content

Commit 76303cf

Browse files
committed
DEP: Deprecate setting the dtype or strides of a numpy array
1 parent 00f2733 commit 76303cf

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

numpy/_core/src/multiarray/getset.c

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ array_shape_set(PyArrayObject *self, PyObject *val, void* NPY_UNUSED(ignored))
8585
/* Free old dimensions and strides */
8686
npy_free_cache_dim_array(self);
8787
((PyArrayObject_fields *)self)->nd = nd;
88-
((PyArrayObject_fields *)self)->dimensions = _dimensions;
88+
((PyArrayObject_fields *)self)->dimensions = _dimensions;
8989
((PyArrayObject_fields *)self)->strides = _dimensions + nd;
9090

9191
if (nd) {
@@ -95,7 +95,7 @@ array_shape_set(PyArrayObject *self, PyObject *val, void* NPY_UNUSED(ignored))
9595
}
9696
else {
9797
/* Free old dimensions and strides */
98-
npy_free_cache_dim_array(self);
98+
npy_free_cache_dim_array(self);
9999
((PyArrayObject_fields *)self)->nd = 0;
100100
((PyArrayObject_fields *)self)->dimensions = NULL;
101101
((PyArrayObject_fields *)self)->strides = NULL;
@@ -124,6 +124,11 @@ array_strides_set(PyArrayObject *self, PyObject *obj, void *NPY_UNUSED(ignored))
124124
npy_intp upper_offset = 0;
125125
Py_buffer view;
126126

127+
/* DEPRECATED 2025-05-04, NumPy 2.3 */
128+
PyErr_WarnEx(PyExc_DeprecationWarning,
129+
"Setting the strides on a Numpy array has been deprecated in Numpy 2.3.\n",
130+
1);
131+
127132
if (obj == NULL) {
128133
PyErr_SetString(PyExc_AttributeError,
129134
"Cannot delete array strides");
@@ -372,6 +377,12 @@ array_descr_set(PyArrayObject *self, PyObject *arg, void *NPY_UNUSED(ignored))
372377
{
373378
PyArray_Descr *newtype = NULL;
374379

380+
/* DEPRECATED 2025-05-04, NumPy 2.3 */
381+
PyErr_WarnEx(PyExc_DeprecationWarning,
382+
"Setting the dtype on a Numpy array has been deprecated in Numpy 2.3.\n"
383+
"Instead of changing the dtype on an array x, create a new array with numpy.frombuffer(x, dtype=new_dtype)",
384+
1);
385+
375386
if (arg == NULL) {
376387
PyErr_SetString(PyExc_AttributeError,
377388
"Cannot delete array dtype");

numpy/_core/tests/test_nditer.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -854,7 +854,7 @@ def test_iter_nbo_align_contig():
854854

855855
# Unaligned input
856856
a = np.zeros((6 * 4 + 1,), dtype='i1')[1:]
857-
a.dtype = 'f4'
857+
a = a.view('f4')
858858
a[:] = np.arange(6, dtype='f4')
859859
assert_(not a.flags.aligned)
860860
# Without 'aligned', shouldn't copy
@@ -1799,7 +1799,7 @@ def test_iter_buffering():
17991799
arrays.append(np.arange(10, dtype='f4'))
18001800
# Unaligned array
18011801
a = np.zeros((4 * 16 + 1,), dtype='i1')[1:]
1802-
a.dtype = 'i4'
1802+
a = a.view('i4')
18031803
a[:] = np.arange(16, dtype='i4')
18041804
arrays.append(a)
18051805
# 4-D F-order array

0 commit comments

Comments
 (0)
0