Skip to content

Commit

Permalink
BUG: astype(..., copy=True) doesn't copy on dask
Browse files Browse the repository at this point in the history
  • Loading branch information
lithomas1 committed Jan 14, 2025
1 parent e5dd419 commit fef8607
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
9 changes: 8 additions & 1 deletion array_api_compat/dask/array/_aliases.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,14 @@

isdtype = get_xp(np)(_aliases.isdtype)
unstack = get_xp(da)(_aliases.unstack)
astype = _aliases.astype

def astype(x: Array, dtype: Dtype, /, *, copy: bool = True) -> Array:
if not copy and dtype == x.dtype:
return x
# dask astype doesn't respect copy=True so copy
# manually via numpy
x = np.array(x, dtype=dtype, copy=copy)
return da.from_array(x)

# Common aliases

Expand Down
13 changes: 13 additions & 0 deletions tests/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,3 +272,16 @@ def test_asarray_copy(library):
assert all(b[0] == 1.0)
else:
assert all(b[0] == 0.0)

@pytest.mark.parametrize("library", wrapped_libraries)
def test_astype_copy(library):
# array-api-tests currently doesn't check copy=True
# makes a copy when dtypes are the same
# so we check that here
xp = import_(library, wrapper=True)
a = xp.asarray([1])
b = xp.astype(a, a.dtype, copy=True)

a[0] = 10

assert b[0] == 1

0 comments on commit fef8607

Please sign in to comment.