Skip to content

Commit 62fc6f5

Browse files
authored
Merge pull request #454 from stanbot8/fix/reshape-copy-false
ENH: use native numpy.reshape for numpy>=2.1
2 parents 44b614c + 6a46752 commit 62fc6f5

3 files changed

Lines changed: 30 additions & 1 deletion

File tree

src/array_api_compat/numpy/_aliases.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,10 @@
5050
cumulative_sum = get_xp(np)(_aliases.cumulative_sum)
5151
cumulative_prod = get_xp(np)(_aliases.cumulative_prod)
5252
permute_dims = get_xp(np)(_aliases.permute_dims)
53-
reshape = get_xp(np)(_aliases.reshape)
53+
if np.__version__ < '2.1':
54+
reshape = get_xp(np)(_aliases.reshape)
55+
else:
56+
reshape = np.reshape
5457
argsort = get_xp(np)(_aliases.argsort)
5558
sort = get_xp(np)(_aliases.sort)
5659
nonzero = get_xp(np)(_aliases.nonzero)

tests/test_cupy.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import pytest
22
from array_api_compat import device, to_device
3+
import warnings
34

45
xp = pytest.importorskip("array_api_compat.cupy")
56
from cupy.cuda import Stream
@@ -43,3 +44,13 @@ def test_to_device_with_dlpack_stream():
4344
# device context.
4445
b = to_device(a, dev, stream=s1.ptr)
4546
assert device(b) == dev
47+
48+
49+
def test_reshape_copy_false_returns_a_view_without_deprecation():
50+
x = xp.arange(12)
51+
with warnings.catch_warnings():
52+
warnings.simplefilter("error", DeprecationWarning)
53+
y = xp.reshape(x, (3, 4), copy=False)
54+
55+
assert y.shape == (3, 4)
56+
assert xp.shares_memory(x, y)

tests/test_numpy.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,3 +139,18 @@ def test_matrix_is_not_array_api_obj():
139139
with warnings.catch_warnings():
140140
warnings.simplefilter("ignore", PendingDeprecationWarning)
141141
assert not is_array_api_obj(np.matrix(3))
142+
143+
144+
def test_reshape_copy_false_returns_a_view_without_deprecation():
145+
x = np.arange(12)
146+
with warnings.catch_warnings():
147+
warnings.simplefilter("error", DeprecationWarning)
148+
y = xp.reshape(x, (3, 4), copy=False)
149+
150+
assert y.shape == (3, 4)
151+
assert np.shares_memory(x, y)
152+
153+
154+
def test_reshape_copy_false_rejects_an_input_that_needs_a_copy():
155+
with pytest.raises((ValueError, AttributeError)):
156+
xp.reshape(np.arange(12).reshape(3, 4).T, (2, 6), copy=False)

0 commit comments

Comments
 (0)