Skip to content

Commit def6b46

Browse files
committed
Fix up asarray
1 parent 74b7b79 commit def6b46

File tree

1 file changed

+18
-19
lines changed

1 file changed

+18
-19
lines changed

array_api_compat/common/_aliases.py

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -325,31 +325,30 @@ def _asarray(
325325
else:
326326
COPY_FALSE = (False,)
327327
COPY_TRUE = (True,)
328-
if copy in COPY_FALSE:
328+
if copy in COPY_FALSE and namespace != "dask.array":
329329
# copy=False is not yet implemented in xp.asarray
330330
raise NotImplementedError("copy=False is not yet implemented")
331-
if (hasattr(xp, "ndarray") and isinstance(obj, xp.ndarray)) or hasattr(obj, "__array__"):
332-
#print('hit me')
331+
if (hasattr(xp, "ndarray") and isinstance(obj, xp.ndarray)):
333332
if dtype is not None and obj.dtype != dtype:
334333
copy = True
335-
#print(copy)
336334
if copy in COPY_TRUE:
337-
copy_kwargs = {}
338-
if namespace != "dask.array":
339-
copy_kwargs["copy"] = True
340-
else:
341-
# No copy kw in dask.asarray so we go thorugh np.asarray first
342-
# (like dask also does) but copy after
343-
if dtype is None:
344-
# Same dtype copy is no-op in dask
345-
#print("in here?")
346-
return obj.copy()
347-
import numpy as np
348-
#print(obj)
349-
obj = np.asarray(obj).copy()
350-
#print(obj)
351-
return xp.array(obj, dtype=dtype, **copy_kwargs)
335+
return xp.array(obj, copy=True, dtype=dtype)
352336
return obj
337+
elif namespace == "dask.array":
338+
if copy in COPY_TRUE:
339+
if dtype is None:
340+
return obj.copy()
341+
# Go through numpy, since dask copy is no-op by default
342+
import numpy as np
343+
obj = np.array(obj, dtype=dtype, copy=True)
344+
return xp.array(obj, dtype=dtype)
345+
else:
346+
import dask.array as da
347+
import numpy as np
348+
if not isinstance(obj, da.Array):
349+
obj = np.asarray(obj, dtype=dtype)
350+
return da.from_array(obj)
351+
return obj
353352

354353
return xp.asarray(obj, dtype=dtype, **kwargs)
355354

0 commit comments

Comments
 (0)