Skip to content

WIP: ENH/TST: xp_assert_ enhancements #267

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

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
159 changes: 101 additions & 58 deletions src/array_api_extra/_lib/_testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@
from types import ModuleType
from typing import cast

import numpy as np
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this OK? Sometimes it was imported within test functions below.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Today it is OK, as this module is not imported automatically from the outer scope.
In the long run though, we want to move this module to public at which point it won't be a good design anymore (although it remains to be seen if any Array library in real life can achieve not to have numpy as a hard dependency...)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I think it would be fine to make these public API once they are ready, with the caveat that NumPy is required. We are really striving for minimal runtime dependencies rather than test time dependencies downstream, at least for now.

Copy link
Contributor

@crusaderky crusaderky Apr 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This module heavily relies on np.testing.assert* anyway.
We'll just need to add a test that import array_api_extra doesn't import numpy.

import pytest

from ._utils._compat import (
array_namespace,
is_array_api_strict_namespace,
is_cupy_namespace,
is_dask_namespace,
is_numpy_namespace,
is_pydata_sparse_namespace,
is_torch_namespace,
)
Expand All @@ -25,7 +27,11 @@


def _check_ns_shape_dtype(
actual: Array, desired: Array
actual: Array,
desired: Array,
check_dtype: bool,
check_shape: bool,
check_scalar: bool,
) -> ModuleType: # numpydoc ignore=RT03
"""
Assert that namespace, shape and dtype of the two arrays match.
Expand All @@ -47,43 +53,62 @@
msg = f"namespaces do not match: {actual_xp} != f{desired_xp}"
assert actual_xp == desired_xp, msg

actual_shape = actual.shape
desired_shape = desired.shape
if is_dask_namespace(desired_xp):
# Dask uses nan instead of None for unknown shapes
if any(math.isnan(i) for i in cast(tuple[float, ...], actual_shape)):
actual_shape = actual.compute().shape # type: ignore[attr-defined] # pyright: ignore[reportAttributeAccessIssue]
if any(math.isnan(i) for i in cast(tuple[float, ...], desired_shape)):
desired_shape = desired.compute().shape # type: ignore[attr-defined] # pyright: ignore[reportAttributeAccessIssue]

msg = f"shapes do not match: {actual_shape} != f{desired_shape}"
assert actual_shape == desired_shape, msg

msg = f"dtypes do not match: {actual.dtype} != {desired.dtype}"
assert actual.dtype == desired.dtype, msg
if check_shape:
actual_shape = actual.shape
desired_shape = desired.shape
Comment on lines +57 to +58
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This may fail if we start using it in scipy, because scipy overrides array_namespace to return numpy for scalars and lists. Maybe out of scope for this PR I though?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Huh. Yeah let's do l consider that in a SciPy PR that attempts to use this there. Then we can decide whether/what changes are needed.

if is_dask_namespace(desired_xp):
# Dask uses nan instead of None for unknown shapes
if any(math.isnan(i) for i in cast(tuple[float, ...], actual_shape)):
actual_shape = actual.compute().shape # type: ignore[attr-defined] # pyright: ignore[reportAttributeAccessIssue]
if any(math.isnan(i) for i in cast(tuple[float, ...], desired_shape)):
desired_shape = desired.compute().shape # type: ignore[attr-defined] # pyright: ignore[reportAttributeAccessIssue]

msg = f"shapes do not match: {actual_shape} != f{desired_shape}"
assert actual_shape == desired_shape, msg

if check_dtype:
msg = f"dtypes do not match: {actual.dtype} != {desired.dtype}"
assert actual.dtype == desired.dtype, msg

if is_numpy_namespace(actual_xp) and check_scalar:
Copy link
Contributor Author

@mdhaber mdhaber Apr 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if is_numpy_namespace(actual_xp) and check_scalar:
if is_numpy_namespace(actual_xp) and check_arrayness:

?
I seem to remember some discussion about naming this parameter when adding to SciPy (check_0d). I don't really care what it is called. check_type is also on the table.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

scalar sounds fine to me.

# only NumPy distinguishes between scalars and arrays; we do if check_scalar.
_msg = (
"array-ness does not match:\n Actual: "
f"{type(actual)}\n Desired: {type(desired)}"
)
assert np.isscalar(actual) == np.isscalar(desired), _msg

return desired_xp


def _prepare_for_test(array: Array, xp: ModuleType) -> Array:
"""
Ensure that the array can be compared with xp.testing or np.testing.
Ensure that the array can be compared with np.testing.

This involves transferring it from GPU to CPU memory, densifying it, etc.
"""
if is_torch_namespace(xp):
return array.cpu() # type: ignore[attr-defined] # pyright: ignore[reportAttributeAccessIssue]
return np.asarray(array.cpu()) # type: ignore[attr-defined, return-value] # pyright: ignore[reportAttributeAccessIssue, reportUnknownArgumentType, reportReturnType]
if is_pydata_sparse_namespace(xp):
return array.todense() # type: ignore[attr-defined] # pyright: ignore[reportAttributeAccessIssue]
if is_array_api_strict_namespace(xp):
# Note: we deliberately did not add a `.to_device` method in _typing.pyi
# even if it is required by the standard as many backends don't support it
return array.to_device(xp.Device("CPU_DEVICE")) # type: ignore[attr-defined] # pyright: ignore[reportAttributeAccessIssue]
# Note: nothing to do for CuPy, because it uses a bespoke test function
if is_cupy_namespace(xp):
return xp.asnumpy(array)
return array


def xp_assert_equal(actual: Array, desired: Array, err_msg: str = "") -> None:
def xp_assert_equal(
actual: Array,
desired: Array,
*,
err_msg: str = "",
check_dtype: bool = True,
check_shape: bool = True,
check_scalar: bool = False,
) -> None:
"""
Array-API compatible version of `np.testing.assert_array_equal`.

Expand All @@ -95,34 +120,56 @@
The expected array (typically hardcoded).
err_msg : str, optional
Error message to display on failure.
check_dtype, check_shape : bool, default: True
Whether to check agreement between actual and desired dtypes and shapes
check_scalar : bool, default: False
NumPy only: whether to check agreement between actual and desired types -
0d array vs scalar.
Comment on lines +125 to +127
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The default for this is the opposite as in scipy

Copy link
Contributor Author

@mdhaber mdhaber Apr 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant to mention that, so thanks for bringing it up. I think it should be True, but that would make a lot of tests fail internally. I figured that could be fixed in a follow-up.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

array-api-extra tests? If so, yes, that sounds fine, but we should open an issue for that before merging this

Copy link
Contributor Author

@mdhaber mdhaber Apr 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. Sure, I'll open an issue.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


See Also
--------
xp_assert_close : Similar function for inexact equality checks.
numpy.testing.assert_array_equal : Similar function for NumPy arrays.
"""
xp = _check_ns_shape_dtype(actual, desired)
xp = _check_ns_shape_dtype(actual, desired, check_dtype, check_shape, check_scalar)
actual = _prepare_for_test(actual, xp)
desired = _prepare_for_test(desired, xp)
np.testing.assert_array_equal(actual, desired, err_msg=err_msg)

if is_cupy_namespace(xp):
xp.testing.assert_array_equal(actual, desired, err_msg=err_msg)
elif is_torch_namespace(xp):
# PyTorch recommends using `rtol=0, atol=0` like this
# to test for exact equality
xp.testing.assert_close(
actual,
desired,
rtol=0,
atol=0,
equal_nan=True,
check_dtype=False,
msg=err_msg or None,
)
else:
import numpy as np # pylint: disable=import-outside-toplevel

np.testing.assert_array_equal(actual, desired, err_msg=err_msg)
def xp_assert_less(
x: Array,
y: Array,
*,
err_msg: str = "",
check_dtype: bool = True,
check_shape: bool = True,
check_scalar: bool = False,
) -> None:
"""
Array-API compatible version of `np.testing.assert_array_less`.

Parameters
----------
x, y : Array
The arrays to compare according to ``x < y`` (elementwise).
err_msg : str, optional
Error message to display on failure.
check_dtype, check_shape : bool, default: True
Whether to check agreement between actual and desired dtypes and shapes
check_scalar : bool, default: False
NumPy only: whether to check agreement between actual and desired types -
0d array vs scalar.

See Also
--------
xp_assert_close : Similar function for inexact equality checks.
numpy.testing.assert_array_equal : Similar function for NumPy arrays.
"""
xp = _check_ns_shape_dtype(x, y, check_dtype, check_shape, check_scalar)
x = _prepare_for_test(x, xp)
y = _prepare_for_test(y, xp)
np.testing.assert_array_less(x, y, err_msg=err_msg) # type: ignore[call-overload]

Check failure on line 172 in src/array_api_extra/_lib/_testing.py

View workflow job for this annotation

GitHub Actions / Format

Argument of type "Array" cannot be assigned to parameter "y" of type "_NumericArrayLike" in function "assert_array_less"   Type "Array" is not assignable to type "_NumericArrayLike"     "Array" is incompatible with protocol "_SupportsArray[dtype[numpy.bool[builtins.bool]] | dtype[number[Any, int | float | complex]]]"       "__array__" is not present     "Array" is incompatible with protocol "_NestedSequence[_SupportsArray[dtype[numpy.bool[builtins.bool]] | dtype[number[Any, int | float | complex]]]]"       "__len__" is not present       "__contains__" is not present       "__iter__" is not present       "__reversed__" is not present ... (reportArgumentType)

Check failure on line 172 in src/array_api_extra/_lib/_testing.py

View workflow job for this annotation

GitHub Actions / Format

Argument of type "Array" cannot be assigned to parameter "x" of type "_NumericArrayLike" in function "assert_array_less"   Type "Array" is not assignable to type "_NumericArrayLike"     "Array" is incompatible with protocol "_SupportsArray[dtype[numpy.bool[builtins.bool]] | dtype[number[Any, int | float | complex]]]"       "__array__" is not present     "Array" is incompatible with protocol "_NestedSequence[_SupportsArray[dtype[numpy.bool[builtins.bool]] | dtype[number[Any, int | float | complex]]]]"       "__len__" is not present       "__contains__" is not present       "__iter__" is not present       "__reversed__" is not present ... (reportArgumentType)

Check failure on line 172 in src/array_api_extra/_lib/_testing.py

View workflow job for this annotation

GitHub Actions / Format

No overloads for "assert_array_less" match the provided arguments (reportCallIssue)


def xp_assert_close(
Expand All @@ -132,6 +179,9 @@
rtol: float | None = None,
atol: float = 0,
err_msg: str = "",
check_dtype: bool = True,
check_shape: bool = True,
check_scalar: bool = False,
) -> None:
"""
Array-API compatible version of `np.testing.assert_allclose`.
Expand All @@ -148,6 +198,11 @@
Absolute tolerance. Default: 0.
err_msg : str, optional
Error message to display on failure.
check_dtype, check_shape : bool, default: True
Whether to check agreement between actual and desired dtypes and shapes
check_scalar : bool, default: False
NumPy only: whether to check agreement between actual and desired types -
0d array vs scalar.

See Also
--------
Expand All @@ -159,7 +214,7 @@
-----
The default `atol` and `rtol` differ from `xp.all(xpx.isclose(a, b))`.
"""
xp = _check_ns_shape_dtype(actual, desired)
xp = _check_ns_shape_dtype(actual, desired, check_dtype, check_shape, check_scalar)

floating = xp.isdtype(actual.dtype, ("real floating", "complex floating"))
if rtol is None and floating:
Expand All @@ -173,26 +228,14 @@
actual = _prepare_for_test(actual, xp)
desired = _prepare_for_test(desired, xp)

if is_cupy_namespace(xp):
xp.testing.assert_allclose(
actual, desired, rtol=rtol, atol=atol, err_msg=err_msg
)
elif is_torch_namespace(xp):
xp.testing.assert_close(
actual, desired, rtol=rtol, atol=atol, equal_nan=True, msg=err_msg or None
)
else:
import numpy as np # pylint: disable=import-outside-toplevel

# JAX/Dask arrays work directly with `np.testing`
assert isinstance(rtol, float)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was probably added to avoid the pyright error below? I don't think pyright should make us do this sort of thing.

np.testing.assert_allclose( # type: ignore[call-overload] # pyright: ignore[reportCallIssue]
actual, # pyright: ignore[reportArgumentType]
desired, # pyright: ignore[reportArgumentType]
rtol=rtol,
atol=atol,
err_msg=err_msg,
)
# JAX/Dask arrays work directly with `np.testing`
np.testing.assert_allclose( # type: ignore[call-overload] # pyright: ignore[reportCallIssue]
actual, # pyright: ignore[reportArgumentType]
desired, # pyright: ignore[reportArgumentType]
rtol=rtol,

Check failure on line 235 in src/array_api_extra/_lib/_testing.py

View workflow job for this annotation

GitHub Actions / Format

Argument of type "float | None" cannot be assigned to parameter "rtol" of type "float" in function "assert_allclose"   Type "float | None" is not assignable to type "float"     "None" is not assignable to "float" (reportArgumentType)
atol=atol,
err_msg=err_msg,
)


def xfail(
Expand Down
82 changes: 76 additions & 6 deletions tests/test_testing.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
from collections.abc import Callable
from contextlib import nullcontext
from types import ModuleType
from typing import cast

import numpy as np
import pytest

from array_api_extra._lib._backends import Backend
from array_api_extra._lib._testing import xp_assert_close, xp_assert_equal
from array_api_extra._lib._testing import (
xp_assert_close,
xp_assert_equal,
xp_assert_less,
)
from array_api_extra._lib._utils._compat import (
array_namespace,
is_dask_namespace,
Expand All @@ -22,15 +27,19 @@
"func",
[
xp_assert_equal,
xp_assert_less,
pytest.param(
xp_assert_close,
marks=pytest.mark.xfail_xp_backend(Backend.SPARSE, reason="no isdtype"),
marks=pytest.mark.xfail_xp_backend(
Backend.SPARSE, reason="no isdtype", strict=False
),
Comment on lines +33 to +35
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#269 adds support for strict=False, which was previously quietly ignored.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

now merged

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Used.

),
],
)


@param_assert_equal_close
@pytest.mark.xfail_xp_backend(Backend.SPARSE, reason="no isdtype", strict=False)
@pytest.mark.parametrize("func", [xp_assert_equal, xp_assert_close])
def test_assert_close_equal_basic(xp: ModuleType, func: Callable[..., None]): # type: ignore[explicit-any]
func(xp.asarray(0), xp.asarray(0))
func(xp.asarray([1, 2]), xp.asarray([1, 2]))
Expand All @@ -50,8 +59,8 @@ def test_assert_close_equal_basic(xp: ModuleType, func: Callable[..., None]): #

@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, xp_assert_close])
def test_assert_close_equal_namespace(xp: ModuleType, func: Callable[..., None]): # type: ignore[explicit-any]
@pytest.mark.parametrize("func", [xp_assert_equal, xp_assert_close, xp_assert_less])
def test_assert_close_equal_less_namespace(xp: ModuleType, func: Callable[..., None]): # type: ignore[explicit-any]
with pytest.raises(AssertionError, match="namespaces do not match"):
func(xp.asarray(0), np.asarray(0))
with pytest.raises(TypeError, match="Unrecognized array input"):
Expand All @@ -60,6 +69,58 @@ def test_assert_close_equal_namespace(xp: ModuleType, func: Callable[..., None])
func(xp.asarray([0]), [0])


@param_assert_equal_close
@pytest.mark.parametrize("check_shape", [False, True])
def test_assert_close_equal_less_shape( # type: ignore[explicit-any]
xp: ModuleType,
func: Callable[..., None],
check_shape: bool,
):
context = (
pytest.raises(AssertionError, match="shapes do not match")
if check_shape
else nullcontext()
)
with context:
func(xp.asarray([xp.nan, xp.nan]), xp.asarray(xp.nan), check_shape=check_shape)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NaNs pass NumPy's _close, _equal, and _less checks, so using NaNs here (and below) allows us to parametrize over all three functions.



@param_assert_equal_close
@pytest.mark.parametrize("check_dtype", [False, True])
def test_assert_close_equal_less_dtype( # type: ignore[explicit-any]
xp: ModuleType,
func: Callable[..., None],
check_dtype: bool,
):
context = (
pytest.raises(AssertionError, match="dtypes do not match")
if check_dtype
else nullcontext()
)
with context:
func(
xp.asarray(xp.nan, dtype=xp.float32),
xp.asarray(xp.nan, dtype=xp.float64),
check_dtype=check_dtype,
)


@pytest.mark.parametrize("func", [xp_assert_equal, xp_assert_close, xp_assert_less])
@pytest.mark.parametrize("check_scalar", [False, True])
def test_assert_close_equal_less_scalar( # type: ignore[explicit-any]
xp: ModuleType,
func: Callable[..., None],
check_scalar: bool,
):
context = (
pytest.raises(AssertionError, match="array-ness does not match")
if check_scalar
else nullcontext()
)
with context:
func(np.asarray(xp.nan), np.asarray(xp.nan)[()], check_scalar=check_scalar)


@pytest.mark.xfail_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)
Expand All @@ -71,9 +132,18 @@ def test_assert_close_tolerance(xp: ModuleType):
xp_assert_close(xp.asarray([100.0]), xp.asarray([102.0]), atol=1)


@param_assert_equal_close
def test_assert_less_basic(xp: ModuleType):
xp_assert_less(xp.asarray(-1), xp.asarray(0))
xp_assert_less(xp.asarray([1, 2]), xp.asarray([2, 3]))
with pytest.raises(AssertionError):
xp_assert_less(xp.asarray([1, 1]), xp.asarray([2, 1]))
with pytest.raises(AssertionError, match="hello"):
xp_assert_less(xp.asarray([1, 1]), xp.asarray([2, 1]), err_msg="hello")


@pytest.mark.skip_xp_backend(Backend.SPARSE, reason="index by sparse array")
@pytest.mark.skip_xp_backend(Backend.ARRAY_API_STRICTEST, reason="boolean indexing")
@pytest.mark.parametrize("func", [xp_assert_equal, xp_assert_close])
def test_assert_close_equal_none_shape(xp: ModuleType, func: Callable[..., None]): # type: ignore[explicit-any]
"""On Dask and other lazy backends, test that a shape with NaN's or None's
can be compared to a real shape.
Expand Down
Loading