Skip to content

TST: Less redundant skip_xp_backends #106

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
Jan 20, 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
5 changes: 4 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Pytest fixtures."""

from collections.abc import Callable
from contextlib import suppress
from functools import wraps
from types import ModuleType
from typing import ParamSpec, TypeVar, cast
Expand Down Expand Up @@ -36,7 +37,9 @@ def library(request: pytest.FixtureRequest) -> Backend: # numpydoc ignore=PR01,
msg = "argument of skip_xp_backend must be a Backend enum"
raise TypeError(msg)
if skip_library == elem:
reason = cast(str, marker.kwargs.get("reason", "skip_xp_backend"))
reason = skip_library.value
with suppress(KeyError):
reason += ":" + cast(str, marker.kwargs["reason"])
pytest.skip(reason=reason)

return elem
Expand Down
2 changes: 1 addition & 1 deletion tests/test_at.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def assert_copy(array: Array, copy: bool | None) -> Generator[None, None, None]:


@pytest.mark.skip_xp_backend(
Backend.SPARSE, reason="sparse:read-only backend without .at support"
Backend.SPARSE, reason="read-only backend without .at support"
)
@pytest.mark.parametrize(
("kwargs", "expect_copy"),
Expand Down
32 changes: 13 additions & 19 deletions tests/test_funcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
# mypy: disable-error-code=no-untyped-usage


@pytest.mark.skip_xp_backend(Backend.SPARSE, reason="sparse:no expand_dims")
@pytest.mark.skip_xp_backend(Backend.SPARSE, reason="no expand_dims")
class TestAtLeastND:
def test_0D(self, xp: ModuleType):
x = xp.asarray(1.0)
Expand Down Expand Up @@ -98,7 +98,7 @@ def test_xp(self, xp: ModuleType):
xp_assert_equal(y, x)


@pytest.mark.skip_xp_backend(Backend.SPARSE, reason="sparse:no isdtype")
@pytest.mark.skip_xp_backend(Backend.SPARSE, reason="no isdtype")
class TestCov:
def test_basic(self, xp: ModuleType):
xp_assert_close(
Expand Down Expand Up @@ -137,17 +137,15 @@ def test_device(self, xp: ModuleType, device: Device):
x = xp.asarray([1, 2, 3], device=device)
assert get_device(cov(x)) == device

@pytest.mark.skip_xp_backend(
Backend.NUMPY_READONLY, reason="numpy_readonly:explicit xp"
)
@pytest.mark.skip_xp_backend(Backend.NUMPY_READONLY, reason="explicit xp")
def test_xp(self, xp: ModuleType):
xp_assert_close(
cov(xp.asarray([[0.0, 2.0], [1.0, 1.0], [2.0, 0.0]]).T, xp=xp),
xp.asarray([[1.0, -1.0], [-1.0, 1.0]], dtype=xp.float64),
)


@pytest.mark.skip_xp_backend(Backend.SPARSE, reason="sparse:no device kwarg in asarray")
@pytest.mark.skip_xp_backend(Backend.SPARSE, reason="no device")
class TestCreateDiagonal:
def test_1d(self, xp: ModuleType):
# from np.diag tests
Expand Down Expand Up @@ -193,10 +191,10 @@ def test_xp(self, xp: ModuleType):
xp_assert_equal(y, xp.asarray([[1, 0], [0, 2]]))


@pytest.mark.skip_xp_backend(Backend.SPARSE, reason="sparse:no expand_dims")
@pytest.mark.skip_xp_backend(Backend.SPARSE, reason="no expand_dims")
class TestExpandDims:
@pytest.mark.skip_xp_backend(Backend.DASK, reason="dask:tuple index out of range")
@pytest.mark.skip_xp_backend(Backend.TORCH, reason="torch:tuple index out of range")
@pytest.mark.skip_xp_backend(Backend.DASK, reason="tuple index out of range")
@pytest.mark.skip_xp_backend(Backend.TORCH, reason="tuple index out of range")
def test_functionality(self, xp: ModuleType):
def _squeeze_all(b: Array) -> Array:
"""Mimics `np.squeeze(b)`. `xpx.squeeze`?"""
Expand Down Expand Up @@ -254,7 +252,7 @@ def test_xp(self, xp: ModuleType):
assert y.shape == (1, 1, 1, 3)


@pytest.mark.skip_xp_backend(Backend.SPARSE, reason="sparse:no expand_dims")
@pytest.mark.skip_xp_backend(Backend.SPARSE, reason="no expand_dims")
class TestKron:
def test_basic(self, xp: ModuleType):
# Using 0-dimensional array
Expand Down Expand Up @@ -351,9 +349,7 @@ def test_xp(self, xp: ModuleType):
xp_assert_equal(nunique(a, xp=xp), xp.asarray(3))


@pytest.mark.skip_xp_backend(
Backend.SPARSE, reason="sparse:no arange, no device kwarg in asarray"
)
@pytest.mark.skip_xp_backend(Backend.SPARSE, reason="no arange, no device")
class TestPad:
def test_simple(self, xp: ModuleType):
a = xp.arange(1, 4)
Expand Down Expand Up @@ -403,8 +399,8 @@ def test_list_of_tuples_width(self, xp: ModuleType):
assert padded.shape == (4, 4)


@pytest.mark.skip_xp_backend(Backend.DASK, reason="dask:no argsort")
@pytest.mark.skip_xp_backend(Backend.SPARSE, reason="sparse:no device kwarg in asarray")
@pytest.mark.skip_xp_backend(Backend.DASK, reason="no argsort")
@pytest.mark.skip_xp_backend(Backend.SPARSE, reason="no device kwarg in asarray")
class TestSetDiff1D:
@pytest.mark.skip_xp_backend(
Backend.TORCH, reason="index_select not implemented for uint32"
Expand Down Expand Up @@ -440,9 +436,7 @@ def test_device(self, xp: ModuleType, device: Device):
x2 = xp.asarray([2, 3, 4], device=device)
assert get_device(setdiff1d(x1, x2)) == device

@pytest.mark.skip_xp_backend(
Backend.NUMPY_READONLY, reason="numpy_readonly:explicit xp"
)
@pytest.mark.skip_xp_backend(Backend.NUMPY_READONLY, reason="explicit xp")
def test_xp(self, xp: ModuleType):
x1 = xp.asarray([3, 8, 20])
x2 = xp.asarray([2, 3, 4])
Expand All @@ -451,7 +445,7 @@ def test_xp(self, xp: ModuleType):
xp_assert_equal(actual, expected)


@pytest.mark.skip_xp_backend(Backend.SPARSE, reason="sparse:no isdtype")
@pytest.mark.skip_xp_backend(Backend.SPARSE, reason="no isdtype")
class TestSinc:
def test_simple(self, xp: ModuleType):
xp_assert_equal(sinc(xp.asarray(0.0)), xp.asarray(1.0))
Expand Down
16 changes: 5 additions & 11 deletions tests/test_testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@
xp_assert_equal,
pytest.param(
xp_assert_close,
marks=pytest.mark.skip_xp_backend(
Backend.SPARSE, reason="sparse:no isdtype"
),
marks=pytest.mark.skip_xp_backend(Backend.SPARSE, reason="no isdtype"),
),
],
)
Expand All @@ -40,19 +38,15 @@ def test_assert_close_equal_basic(xp: ModuleType, func: Callable[..., None]): #
func(xp.asarray([1, 2]), xp.asarray([1, 3]), err_msg="hello")


@pytest.mark.skip_xp_backend(Backend.NUMPY, reason="numpy:test other ns vs. numpy")
@pytest.mark.skip_xp_backend(
Backend.NUMPY_READONLY, reason="numpy_readonly:test other ns vs. numpy"
)
@pytest.mark.skip_xp_backend(Backend.NUMPY, reason="test other ns vs. numpy")
@pytest.mark.skip_xp_backend(Backend.NUMPY_READONLY, reason="test other ns vs. numpy")
@pytest.mark.parametrize(
"func",
[
xp_assert_equal,
pytest.param(
xp_assert_close,
marks=pytest.mark.skip_xp_backend(
Backend.SPARSE, reason="sparse:no isdtype"
),
marks=pytest.mark.skip_xp_backend(Backend.SPARSE, reason="no isdtype"),
),
],
)
Expand All @@ -65,7 +59,7 @@ def test_assert_close_equal_namespace(xp: ModuleType, func: Callable[..., None])
func(xp.asarray([0]), [0])


@pytest.mark.skip_xp_backend(Backend.SPARSE, reason="sparse:no isdtype")
@pytest.mark.skip_xp_backend(Backend.SPARSE, reason="no isdtype")
def test_assert_close_tolerance(xp: ModuleType):
xp_assert_close(xp.asarray([100.0]), xp.asarray([102.0]), rtol=0.03)
with pytest.raises(AssertionError):
Expand Down
14 changes: 5 additions & 9 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@


class TestIn1D:
@pytest.mark.skip_xp_backend(Backend.DASK, reason="dask:no argsort")
@pytest.mark.skip_xp_backend(Backend.DASK, reason="no argsort")
@pytest.mark.skip_xp_backend(
Backend.SPARSE, reason="sparse:no unique_inverse, no device kwarg in asarray"
Backend.SPARSE, reason="no unique_inverse, no device kwarg in asarray"
)
# cover both code paths
@pytest.mark.parametrize("n", [9, 15])
Expand All @@ -25,19 +25,15 @@ def test_no_invert_assume_unique(self, xp: ModuleType, n: int):
actual = in1d(x1, x2)
xp_assert_equal(actual, expected)

@pytest.mark.skip_xp_backend(
Backend.SPARSE, reason="sparse: no device kwarg in asarray"
)
@pytest.mark.skip_xp_backend(Backend.SPARSE, reason="no device kwarg in asarray")
def test_device(self, xp: ModuleType, device: Device):
x1 = xp.asarray([3, 8, 20], device=device)
x2 = xp.asarray([2, 3, 4], device=device)
assert get_device(in1d(x1, x2)) == device

@pytest.mark.skip_xp_backend(Backend.NUMPY_READONLY, reason="explicit xp")
@pytest.mark.skip_xp_backend(
Backend.NUMPY_READONLY, reason="numpy_readonly:explicit xp"
)
@pytest.mark.skip_xp_backend(
Backend.SPARSE, reason="sparse:no arange, no device kwarg in asarray"
Backend.SPARSE, reason="no arange, no device kwarg in asarray"
)
def test_xp(self, xp: ModuleType):
x1 = xp.asarray([1, 6])
Expand Down
Loading