Skip to content

BUG: do not allow asarray of nested sequences of arrays #147

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 1 commit into from
Apr 16, 2025
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
4 changes: 4 additions & 0 deletions array_api_strict/_creation_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ def asarray(

if isinstance(obj, Array):
return Array._new(np.array(obj._array, copy=copy, dtype=_np_dtype), device=device)
elif isinstance(obj, list | tuple):
if any(isinstance(x, Array) for x in obj):
raise TypeError("Nested Arrays are not allowed. Use `stack` instead.")

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
4 changes: 2 additions & 2 deletions array_api_strict/tests/test_array_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -482,12 +482,12 @@ def test_array_conversion():
# __array__, which is only used in asarray when converting lists of
# arrays.
a = ones((2, 3))
asarray([a])
np.asarray(a)

for device in ("device1", "device2"):
a = ones((2, 3), device=array_api_strict.Device(device))
with pytest.raises(RuntimeError, match="Can not convert array"):
asarray([a])
np.asarray(a)

def test__array__():
# __array__ should work for now
Expand Down
26 changes: 14 additions & 12 deletions array_api_strict/tests/test_creation_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
zeros,
zeros_like,
)
from .._dtypes import int16, float32, float64
from .._dtypes import float32, float64
from .._array_object import Array, CPU_DEVICE, Device
from .._flags import set_array_api_strict_flags

Expand Down Expand Up @@ -97,18 +97,20 @@ def test_asarray_copy():
a[0] = 0
assert all(b[0] == 0)


def test_asarray_list_of_lists():
a = asarray(1, dtype=int16)
b = asarray([1], dtype=int16)
res = asarray([a, a])
assert res.shape == (2,)
assert res.dtype == int16
assert all(res == asarray([1, 1]))

res = asarray([b, b])
assert res.shape == (2, 1)
assert res.dtype == int16
assert all(res == asarray([[1], [1]]))
lst = [[1, 2, 3], [4, 5, 6]]
res = asarray(lst)
assert res.shape == (2, 3)


def test_asarray_nested_arrays():
# do not allow arrays in nested sequences
with pytest.raises(TypeError):
asarray([[1, 2, 3], asarray([4, 5, 6])])

with pytest.raises(TypeError):
asarray([1, asarray(1)])


def test_asarray_device_inference():
Expand Down
Loading