Skip to content
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: 5 additions & 0 deletions src/anndata/tests/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,11 @@ 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"
assert a.indices.dtype == b.indices.dtype, (
f"{elem_name}: indices dtype mismatch"
)
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)


@pytest.mark.parametrize("attr", ["indices", "indptr"])
def test_assert_equal_sparse_index_dtype(attr):
"""assert_equal(exact=True) should detect indptr/indices dtype mismatches."""
a = sparse.csr_matrix(np.eye(3))
b = sparse.csr_matrix(np.eye(3))
setattr(b, attr, getattr(b, attr).astype(np.int64))

# 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=attr):
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