Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 7 additions & 0 deletions src/anndata/tests/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,13 @@ def assert_equal_sparse(
exact: bool = False,
elem_name: str | None = None,
):
if exact and sparse.issparse(b) and hasattr(a, "indptr") and hasattr(b, "indptr"):
assert a.indptr.dtype == b.indptr.dtype, (
f"{elem_name}: indptr dtype mismatch: {a.indptr.dtype} vs {b.indptr.dtype}"
)
assert a.indices.dtype == b.indices.dtype, (
f"{elem_name}: indices dtype mismatch: {a.indices.dtype} vs {b.indices.dtype}"
)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
assert a.indptr.dtype == b.indptr.dtype, (
f"{elem_name}: indptr dtype mismatch: {a.indptr.dtype} vs {b.indptr.dtype}"
)
assert a.indices.dtype == b.indices.dtype, (
f"{elem_name}: indices dtype mismatch: {a.indices.dtype} vs {b.indices.dtype}"
)
assert a.indptr.dtype == b.indptr.dtype, (
f"{elem_name}: indptr dtype mismatch"
)
assert a.indices.dtype == b.indices.dtype, (
f"{elem_name}: indices dtype mismatch"
)

I think the mismatch's contents are already reported by assert

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done — simplified the assert messages and included elem_name for context. Updated in 6cb2b35.

a = asarray(a)
assert_equal(b, a, exact=exact, elem_name=elem_name)

Expand Down
15 changes: 15 additions & 0 deletions tests/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,21 @@ def test_assert_equal_dask_arrays():
assert_equal(c, d)


def test_assert_equal_sparse_index_dtype():
"""assert_equal(exact=True) should detect indptr/indices dtype mismatches."""
a = sparse.random(10, 10, format="csr", density=0.3)
b = a.copy()
b.indptr = b.indptr.astype(np.int64)
b.indices = b.indices.astype(np.int64)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Please parametrize this test to change each of these one-at-a-time and then have the match check that indices or indptr is in the message accordingly

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done — parametrized with @pytest.mark.parametrize("attr", ["indices", "indptr"]), each tested one-at-a-time with match=attr. Updated in 6cb2b35.


# Non-exact comparison should pass (values are identical)
assert_equal(a, b, exact=False)

# Exact comparison should catch the dtype mismatch
with pytest.raises(AssertionError, match="indptr dtype mismatch"):
assert_equal(a, b, exact=True)


def test_assert_equal_dask_sparse_arrays():
import dask.array as da
from scipy import sparse
Expand Down
Loading