Skip to content

Commit 02fb925

Browse files
committed
Run more tests on array-api-strict and sparse
1 parent 8a79994 commit 02fb925

File tree

6 files changed

+35
-23
lines changed

6 files changed

+35
-23
lines changed

array_api_compat/common/_helpers.py

+3-7
Original file line numberDiff line numberDiff line change
@@ -649,13 +649,9 @@ def device(x: Array, /) -> Device:
649649
return "cpu"
650650
elif is_dask_array(x):
651651
# Peek at the metadata of the jax array to determine type
652-
try:
653-
import numpy as np
654-
if isinstance(x._meta, np.ndarray):
655-
# Must be on CPU since backed by numpy
656-
return "cpu"
657-
except ImportError:
658-
pass
652+
if is_numpy_array(x._meta):
653+
# Must be on CPU since backed by numpy
654+
return "cpu"
659655
return _DASK_DEVICE
660656
elif is_jax_array(x):
661657
# JAX has .device() as a method, but it is being deprecated so that it

docs/supported-array-libraries.md

+5
Original file line numberDiff line numberDiff line change
@@ -137,3 +137,8 @@ The minimum supported Dask version is 2023.12.0.
137137
## [Sparse](https://sparse.pydata.org/en/stable/)
138138

139139
Similar to JAX, `sparse` Array API support is contained directly in `sparse`.
140+
141+
(array-api-strict-support)=
142+
## [array-api-strict](https://data-apis.org/array-api-strict/)
143+
144+
array-api-strict exists only to test support for the Array API, so it does not need any wrappers.

tests/_helpers.py

+2-8
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
from importlib import import_module
2-
import sys
32

43
import pytest
54

65
wrapped_libraries = ["numpy", "cupy", "torch", "dask.array"]
7-
all_libraries = wrapped_libraries + ["jax.numpy"]
6+
all_libraries = wrapped_libraries + ["array_api_strict", "jax.numpy", "sparse"]
87

9-
# `sparse` added array API support as of Python 3.10.
10-
if sys.version_info >= (3, 10):
11-
all_libraries.append('sparse')
128

139
def import_(library, wrapper=False):
1410
if library == 'cupy':
@@ -20,9 +16,7 @@ def import_(library, wrapper=False):
2016
jax_numpy = import_module("jax.numpy")
2117
if not hasattr(jax_numpy, "__array_api_version__"):
2218
library = 'jax.experimental.array_api'
23-
elif library.startswith('sparse'):
24-
library = 'sparse'
25-
else:
19+
elif library in wrapped_libraries:
2620
library = 'array_api_compat.' + library
2721

2822
return import_module(library)

tests/test_all.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@
1818

1919
@pytest.mark.parametrize("library", ["common"] + wrapped_libraries)
2020
def test_all(library):
21-
import_(library, wrapper=True)
21+
if library == "common":
22+
import array_api_compat.common # noqa: F401
23+
else:
24+
import_(library, wrapper=True)
2225

2326
for mod_name in sys.modules:
2427
if not mod_name.startswith('array_api_compat.' + library):

tests/test_array_namespace.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@
1414

1515
@pytest.mark.parametrize("use_compat", [True, False, None])
1616
@pytest.mark.parametrize("api_version", [None, "2021.12", "2022.12", "2023.12"])
17-
@pytest.mark.parametrize("library", all_libraries + ['array_api_strict'])
17+
@pytest.mark.parametrize("library", all_libraries)
1818
def test_array_namespace(library, api_version, use_compat):
1919
xp = import_(library)
2020

2121
array = xp.asarray([1.0, 2.0, 3.0])
22-
if use_compat is True and library in {'array_api_strict', 'jax.numpy', 'sparse'}:
22+
if use_compat and library not in wrapped_libraries:
2323
pytest.raises(ValueError, lambda: array_namespace(array, use_compat=use_compat))
2424
return
2525
namespace = array_api_compat.array_namespace(array, api_version=api_version, use_compat=use_compat)

tests/test_common.py

+19-5
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
is_dask_array, is_jax_array, is_pydata_sparse_array,
1111
is_numpy_namespace, is_cupy_namespace, is_torch_namespace,
1212
is_dask_namespace, is_jax_namespace, is_pydata_sparse_namespace,
13+
is_array_api_strict_namespace,
1314
)
1415

1516
from array_api_compat import (
@@ -33,6 +34,7 @@
3334
'dask.array': 'is_dask_namespace',
3435
'jax.numpy': 'is_jax_namespace',
3536
'sparse': 'is_pydata_sparse_namespace',
37+
'array_api_strict': 'is_array_api_strict_namespace',
3638
}
3739

3840

@@ -74,7 +76,12 @@ def test_xp_is_array_generics(library):
7476
is_func = globals()[func]
7577
if is_func(x0):
7678
matches.append(library2)
77-
assert matches in ([library], ["numpy"])
79+
80+
if library == "array_api_strict":
81+
# There is no is_array_api_strict_array() function
82+
assert matches == []
83+
else:
84+
assert matches in ([library], ["numpy"])
7885

7986

8087
@pytest.mark.parametrize("library", all_libraries)
@@ -213,26 +220,33 @@ def test_to_device_host(library):
213220
@pytest.mark.parametrize("target_library", is_array_functions.keys())
214221
@pytest.mark.parametrize("source_library", is_array_functions.keys())
215222
def test_asarray_cross_library(source_library, target_library, request):
216-
if source_library == "dask.array" and target_library == "torch":
223+
def _xfail(reason: str) -> None:
217224
# Allow rest of test to execute instead of immediately xfailing
218225
# xref https://github.com/pandas-dev/pandas/issues/38902
226+
request.node.add_marker(pytest.mark.xfail(reason=reason))
219227

228+
if source_library == "dask.array" and target_library == "torch":
220229
# TODO: remove xfail once
221230
# https://github.com/dask/dask/issues/8260 is resolved
222-
request.node.add_marker(pytest.mark.xfail(reason="Bug in dask raising error on conversion"))
223-
if source_library == "cupy" and target_library != "cupy":
231+
_xfail(reason="Bug in dask raising error on conversion")
232+
elif source_library == "jax.numpy" and target_library == "torch":
233+
_xfail(reason="casts int to float")
234+
elif source_library == "cupy" and target_library != "cupy":
224235
# cupy explicitly disallows implicit conversions to CPU
225236
pytest.skip(reason="cupy does not support implicit conversion to CPU")
226237
elif source_library == "sparse" and target_library != "sparse":
227238
pytest.skip(reason="`sparse` does not allow implicit densification")
239+
228240
src_lib = import_(source_library, wrapper=True)
229241
tgt_lib = import_(target_library, wrapper=True)
230242
is_tgt_type = globals()[is_array_functions[target_library]]
231243

232-
a = src_lib.asarray([1, 2, 3])
244+
a = src_lib.asarray([1, 2, 3], dtype=src_lib.int32)
233245
b = tgt_lib.asarray(a)
234246

235247
assert is_tgt_type(b), f"Expected {b} to be a {tgt_lib.ndarray}, but was {type(b)}"
248+
assert b.dtype == tgt_lib.int32
249+
236250

237251

238252
@pytest.mark.parametrize("library", wrapped_libraries)

0 commit comments

Comments
 (0)