Skip to content

Commit

Permalink
Modify testsuite to add sparse support.
Browse files Browse the repository at this point in the history
  • Loading branch information
hameerabbasi committed May 15, 2024
1 parent d3c6636 commit 855756d
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 5 deletions.
4 changes: 3 additions & 1 deletion tests/_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import pytest

wrapped_libraries = ["cupy", "torch", "dask.array"]
all_libraries = wrapped_libraries + ["numpy", "jax.numpy"]
all_libraries = wrapped_libraries + ["numpy", "jax.numpy", "sparse"]
import numpy as np
if np.__version__[0] == '1':
wrapped_libraries.append("numpy")
Expand All @@ -14,6 +14,8 @@ def import_(library, wrapper=False):
if wrapper:
if 'jax' in library:
library = 'jax.experimental.array_api'
elif library.startswith('sparse'):
library = 'sparse'
else:
library = 'array_api_compat.' + library

Expand Down
2 changes: 1 addition & 1 deletion tests/test_array_namespace.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def test_array_namespace(library, api_version, use_compat):
xp = import_(library)

array = xp.asarray([1.0, 2.0, 3.0])
if use_compat is True and library in ['array_api_strict', 'jax.numpy']:
if use_compat is True and library in {'array_api_strict', 'jax.numpy', 'sparse'}:
pytest.raises(ValueError, lambda: array_namespace(array, use_compat=use_compat))
return
namespace = array_api_compat.array_namespace(array, api_version=api_version, use_compat=use_compat)
Expand Down
5 changes: 4 additions & 1 deletion tests/test_common.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from array_api_compat import (is_numpy_array, is_cupy_array, is_torch_array, # noqa: F401
is_dask_array, is_jax_array)
is_dask_array, is_jax_array, is_pydata_sparse)

from array_api_compat import is_array_api_obj, device, to_device

Expand All @@ -16,6 +16,7 @@
'torch': 'is_torch_array',
'dask.array': 'is_dask_array',
'jax.numpy': 'is_jax_array',
'sparse': 'is_pydata_sparse',
}

@pytest.mark.parametrize('library', is_functions.keys())
Expand Down Expand Up @@ -76,6 +77,8 @@ def test_asarray_cross_library(source_library, target_library, request):
if source_library == "cupy" and target_library != "cupy":
# cupy explicitly disallows implicit conversions to CPU
pytest.skip(reason="cupy does not support implicit conversion to CPU")
elif source_library == "sparse" and target_library != "sparse":
pytest.skip(reason="`sparse` does not allow implicit densification")
src_lib = import_(source_library, wrapper=True)
tgt_lib = import_(target_library, wrapper=True)
is_tgt_type = globals()[is_functions[target_library]]
Expand Down
4 changes: 2 additions & 2 deletions tests/test_no_dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def _test_dependency(mod):

# array-api-strict is an example of an array API library that isn't
# wrapped by array-api-compat.
if "strict" not in mod:
if "strict" not in mod and mod != "sparse":
is_mod_array = getattr(array_api_compat, f"is_{mod.split('.')[0]}_array")
assert not is_mod_array(a)
assert mod not in sys.modules
Expand All @@ -50,7 +50,7 @@ def _test_dependency(mod):
# Y (except most array libraries actually do themselves depend on numpy).

@pytest.mark.parametrize("library", ["cupy", "numpy", "torch", "dask.array",
"jax.numpy", "array_api_strict"])
"jax.numpy", "sparse", "array_api_strict"])
def test_numpy_dependency(library):
# This import is here because it imports numpy
from ._helpers import import_
Expand Down

0 comments on commit 855756d

Please sign in to comment.