Skip to content

Fix asarray(copy=False) #15

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 3 commits into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 0 additions & 4 deletions array-api-tests-xfails.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
# copy=False is not yet implemented
# https://github.com/numpy/numpy/pull/25168
array_api_tests/test_creation_functions.py::test_asarray_arrays

# Some fft tests are currently wrong
# (https://github.com/data-apis/array-api-tests/issues/231)
array_api_tests/test_fft.py::test_fft
Expand Down
25 changes: 16 additions & 9 deletions array_api_strict/_creation_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def asarray(
*,
dtype: Optional[Dtype] = None,
device: Optional[Device] = None,
copy: Optional[Union[bool, np._CopyMode]] = None,
copy: Optional[bool] = None,
) -> Array:
"""
Array API compatible wrapper for :py:func:`np.asarray <numpy.asarray>`.
Expand All @@ -53,15 +53,22 @@ def asarray(
_np_dtype = dtype._np_dtype
if device not in [CPU_DEVICE, None]:
raise ValueError(f"Unsupported device {device!r}")
if copy in (False, np._CopyMode.IF_NEEDED):
# Note: copy=False is not yet implemented in np.asarray
raise NotImplementedError("copy=False is not yet implemented")
if isinstance(obj, Array):
if dtype is not None and obj.dtype != dtype:
copy = True
if copy in (True, np._CopyMode.ALWAYS):
return Array._new(np.array(obj._array, copy=True, dtype=_np_dtype))
return obj
if np.__version__[0] < '2':
if copy is False:
# Note: copy=False is not yet implemented in np.asarray for
# NumPy 1

# Work around it by creating the new array and seeing if NumPy
# copies it.
new_array = np.array(obj._array, copy=copy, dtype=_np_dtype)
if new_array is not obj._array:
raise ValueError("Unable to avoid copy while creating an array from given array.")
return Array._new(new_array)
if copy is None:
# NumPy 1 treats copy=False the same as the standard copy=None
copy = False
return Array._new(np.array(obj._array, copy=copy, dtype=_np_dtype))
if dtype is None and isinstance(obj, int) and (obj > 2 ** 64 or obj < -(2 ** 63)):
# Give a better error message in this case. NumPy would convert this
# to an object array. TODO: This won't handle large integers in lists.
Expand Down
19 changes: 12 additions & 7 deletions array_api_strict/tests/test_creation_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,19 +50,24 @@ def test_asarray_copy():
a[0] = 0
assert all(b[0] == 1)
assert all(a[0] == 0)

a = asarray([1])
b = asarray(a, copy=np._CopyMode.ALWAYS)
b = asarray(a, copy=False)
a[0] = 0
assert all(b[0] == 1)
assert all(a[0] == 0)
assert all(b[0] == 0)

a = asarray([1])
b = asarray(a, copy=np._CopyMode.NEVER)
assert_raises(ValueError, lambda: asarray(a, copy=False, dtype=float64))

a = asarray([1])
b = asarray(a, copy=None)
a[0] = 0
assert all(b[0] == 0)
assert_raises(NotImplementedError, lambda: asarray(a, copy=False))
assert_raises(NotImplementedError,
lambda: asarray(a, copy=np._CopyMode.IF_NEEDED))

a = asarray([1])
b = asarray(a, dtype=float64, copy=None)
a[0] = 0
assert all(b[0] == 1.0)

def test_arange_errors():
arange(1, device=CPU_DEVICE) # Doesn't error
Expand Down