Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
5 changes: 3 additions & 2 deletions array_api_strict/_manipulation_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,9 @@ def reshape(x: Array, /, shape: tuple[int, ...], *, copy: bool | None = None) ->

reshaped = np.reshape(data, shape)

if copy is False and not np.shares_memory(data, reshaped):
raise AttributeError("Incompatible shape for in-place modification.")
# NB: `.size>0` works around that `x=np.asarray([]); np.shares_memory(x, x)` is False
if copy is False and data.size > 0 and not np.shares_memory(data, reshaped):
Comment thread
ev-br marked this conversation as resolved.
raise ValueError("Incompatible shape for a no-copy reshape.")

return Array._new(reshaped, device=x.device)

Expand Down
12 changes: 11 additions & 1 deletion array_api_strict/tests/test_manipulation_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,15 @@ def test_reshape_copy():

a = asarray(np.ones((2, 3)).T)
b = reshape(a, (3, 2), copy=True)
assert_raises(AttributeError, lambda: reshape(a, (2, 3), copy=False))
assert_raises(ValueError, lambda: reshape(a, (2, 3), copy=False))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Strictly speaking, a back-compat break. That said, there is no reason for an AttributeError here, and the spec says ValueError, https://data-apis.org/array-api/2022.12/API_specification/generated/array_api.reshape.html



def test_reshape_copy_false_keeps_an_empty_view():
a = asarray(np.empty((0,)))
b = reshape(a, (0, 2), copy=False)
assert b.shape == (0, 2)

a = asarray(np.empty((0, 2)))
b = reshape(a, (2, 0), copy=False)
assert b.shape == (2, 0)

Loading