Skip to content
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

BUG: astype(..., copy=True) doesn't copy on dask #234

Merged
merged 3 commits into from
Jan 16, 2025
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
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:
Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks,
I just raised Notimplemented for now.

Copy link
Contributor

Choose a reason for hiding this comment

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

Please ignore it. astype(x, dtype, device=device(x) must work.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated, PTAL.

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
Copy link
Contributor

Choose a reason for hiding this comment

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

Please move this test to array-api-tests.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Loading