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

Skip to content

DEP: Deprecate setting the strides attribute of a numpy array #28925

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

Merged
merged 28 commits into from
Jun 20, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
18c02c8
DEP: Deprecate setting the strides attribute of a numpy array
eendebakpt May 8, 2025
687cf72
update
eendebakpt May 8, 2025
b32319e
add missing changes
eendebakpt May 8, 2025
0fda654
ci
eendebakpt May 8, 2025
d7cfed3
review comments part 1
eendebakpt May 9, 2025
730c9a9
Update doc/release/upcoming_changes/28925.deprecation.rst
eendebakpt May 9, 2025
5874636
Update doc/release/upcoming_changes/28925.deprecation.rst
charris May 11, 2025
2dad334
review comments
eendebakpt May 11, 2025
288690d
apply ruff
eendebakpt May 12, 2025
8502a2f
fix merge issues
eendebakpt May 15, 2025
0c4929e
clarify release notes
eendebakpt May 15, 2025
61eee97
update warning
eendebakpt May 15, 2025
141bbb7
linter
eendebakpt May 15, 2025
88950f2
fix merge conflicts again
eendebakpt May 15, 2025
347b8d3
merge conflicts
eendebakpt May 15, 2025
4bbbfbc
correct ruff version
eendebakpt May 15, 2025
60df1f7
review comment
eendebakpt May 15, 2025
36a8ca0
Merge branch 'main' into array_stride_set
eendebakpt May 18, 2025
bce4d22
ci
eendebakpt May 18, 2025
423fd5a
review comments
eendebakpt May 26, 2025
d8c22e5
Update numpy/_core/src/multiarray/getset.c
seberg May 30, 2025
6ea5c80
Update numpy/_core/src/multiarray/getset.c
eendebakpt Jun 3, 2025
73551a3
Update numpy/__init__.pyi
seberg Jun 3, 2025
01c04e7
Update numpy/__init__.pyi
eendebakpt Jun 4, 2025
bd59c8d
Merge branch 'main' into array_stride_set
eendebakpt Jun 18, 2025
cf1288a
ruff
eendebakpt Jun 19, 2025
88c1261
Merge branch 'main' into array_stride_set
eendebakpt Jun 19, 2025
08d34a2
submodule fix
eendebakpt Jun 19, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
review comments
  • Loading branch information
eendebakpt committed May 15, 2025
commit 2dad334f621c15f991c8e934a6efd7b862ad0e76
1 change: 1 addition & 0 deletions numpy/_core/src/multiarray/getset.c
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ array_strides_set(PyArrayObject *self, PyObject *obj, void *NPY_UNUSED(ignored))
return -1;
}

/* Deprecated NumPy 2.3, 2025-05-11 */
if( DEPRECATE("Setting the strides on a NumPy array has been deprecated in NumPy 2.3.") < 0 ) {
Copy link
Member

Choose a reason for hiding this comment

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

It may be nice to point out the work-arounds, but considering that stride setting is a bit niche maybe we can also just do that in the release note.

Something like: See np.lib.stride_tricks.strided_window_view and np.lib.stride_tricks.as_strided.

Copy link
Member
@charris charris May 10, 2025

Choose a reason for hiding this comment

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

And here is a long line, which needs space fixes also.

Copy link
Member

Choose a reason for hiding this comment

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

If you are at it, adding a /* Deprecated NumPy 2.3, 2025-05-11 */ above it would also be nice.

Copy link
Member

Choose a reason for hiding this comment

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

I agree the warning needs a suggestion for what to do instead and maybe a pointer to stride_tricks if someone really does need to mutate the strides of an existing array.

Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if( DEPRECATE("Setting the strides on a NumPy array has been deprecated in NumPy 2.3.") < 0 ) {
if (DEPRECATE(
"Setting the strides on a NumPy array has been"
" deprecated in NumPy 2.3.") < 0 ) {

return -1;
}
Expand Down
12 changes: 7 additions & 5 deletions numpy/_core/tests/test_multiarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ def make_array(size, offset, strides):
assert_raises(RuntimeError, make_array, 8, 3, 1)
# Check that the true extent of the array is used.
# Test relies on as_strided base not exposing a buffer.
x = np.lib.stride_tricks.as_strided(np.arange(1), (10, 10), (0, 0))
x = stride_tricks.as_strided(np.arange(1), (10, 10), (0, 0))

def set_strides(arr, strides):
with pytest.warns(DeprecationWarning):
Expand All @@ -420,7 +420,7 @@ def set_strides(arr, strides):
assert_raises(ValueError, set_strides, x, (10 * x.itemsize, x.itemsize))

# Test for offset calculations:
x = np.lib.stride_tricks.as_strided(np.arange(10, dtype=np.int8)[-1],
x = stride_tricks.as_strided(np.arange(10, dtype=np.int8)[-1],
shape=(10,), strides=(-1,))
assert_raises(ValueError, set_strides, x[::-1], -1)
a = x[::-1]
Expand Down Expand Up @@ -8360,11 +8360,13 @@ def test_padded_struct_array(self):
self._check_roundtrip(x3)

@pytest.mark.valgrind_error(reason="leaks buffer info cache temporarily.")
def test_relaxed_strides(self, c=np.ones((1, 10, 10), dtype='i8')):
def test_relaxed_strides(self, c=stride_tricks.as_strided(
np.ones((1, 10, 10), dtype='i8'),
strides=(-1, 80, 8)
)
):
# Note: c defined as parameter so that it is persistent and leak
# checks will notice gh-16934 (buffer info cache leak).
with pytest.warns(DeprecationWarning):
c.strides = (-1, 80, 8) # strides need to be fixed at export

assert_(memoryview(c).strides == (800, 80, 8))

Expand Down
22 changes: 8 additions & 14 deletions numpy/_core/tests/test_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import numpy as np
from numpy._utils import asbytes, asunicode
from numpy.exceptions import AxisError, ComplexWarning
from numpy.lib.stride_tricks import as_strided
from numpy.testing import (
HAS_REFCOUNT,
IS_64BIT,
Expand All @@ -31,7 +32,6 @@
)
from numpy.testing._private.utils import _no_tracing, requires_memory


class TestRegression:
def test_invalid_round(self):
# Ticket #3
Expand Down Expand Up @@ -208,7 +208,7 @@ def test_mem_dot(self):
# Dummy array to detect bad memory access:
_z = np.ones(10)
_dummy = np.empty((0, 10))
z = np.lib.stride_tricks.as_strided(_z, _dummy.shape, _dummy.strides)
z = as_strided(_z, _dummy.shape, _dummy.strides)
np.dot(x, np.transpose(y), out=z)
assert_equal(_z, np.ones(10))
# Do the same for the built-in dot:
Expand Down Expand Up @@ -438,22 +438,16 @@ def test_lexsort_zerolen_custom_strides(self):
xs = np.array([], dtype='i8')
assert np.lexsort((xs,)).shape[0] == 0 # Works

with pytest.warns(DeprecationWarning):
xs.strides = (16,)
xs = as_strided(xs, strides=(16,))
assert np.lexsort((xs,)).shape[0] == 0 # Was: MemoryError

def test_lexsort_zerolen_custom_strides_2d(self):
xs = np.array([], dtype='i8')
xt = as_strided(xs, shape=(0, 2), strides=(16, 16))
assert np.lexsort((xt,), axis=0).shape[0] == 0

xs.shape = (0, 2)
with pytest.warns(DeprecationWarning):
xs.strides = (16, 16)
assert np.lexsort((xs,), axis=0).shape[0] == 0

xs.shape = (2, 0)
with pytest.warns(DeprecationWarning):
xs.strides = (16, 16)
assert np.lexsort((xs,), axis=0).shape[0] == 2
xt = as_strided(xs, shape=(2, 0), strides=(16, 16))
assert np.lexsort((xt,), axis=0).shape[0] == 2

def test_lexsort_invalid_axis(self):
assert_raises(AxisError, np.lexsort, (np.arange(1),), axis=2)
Expand Down Expand Up @@ -647,7 +641,7 @@ def test_reshape_order(self):
def test_reshape_zero_strides(self):
# Issue #380, test reshaping of zero strided arrays
a = np.ones(1)
a = np.lib.stride_tricks.as_strided(a, shape=(5,), strides=(0,))
a = as_strided(a, shape=(5,), strides=(0,))
assert_(a.reshape(5, 1).strides[0] == 0)

def test_reshape_zero_size(self):
Expand Down
0