From 677fb8f7f066dcfd4f41cb97bb6b4634a9786538 Mon Sep 17 00:00:00 2001 From: SID Date: Sat, 27 Jun 2026 15:51:01 -0400 Subject: [PATCH 1/2] test(backed): regress filename=None after read_h5ad --- tests/test_backed_hdf5.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/tests/test_backed_hdf5.py b/tests/test_backed_hdf5.py index 34571793c..fb636677f 100644 --- a/tests/test_backed_hdf5.py +++ b/tests/test_backed_hdf5.py @@ -318,6 +318,32 @@ def test_return_to_memory_mode(adata: ad.AnnData, backing_h5ad: Path): bdata.filename = None +@pytest.mark.parametrize( + ("x", "is_sparse"), + [ + pytest.param(np.arange(12).reshape(3, 4), False, id="dense"), + pytest.param(sparse.csr_matrix(np.arange(12).reshape(3, 4)), True, id="csr"), + pytest.param(sparse.csc_matrix(np.arange(12).reshape(3, 4)), True, id="csc"), + ], +) +def test_return_to_memory_mode_after_read_h5ad( + tmp_path: Path, x: np.ndarray | sparse.spmatrix, is_sparse: bool +): + pth = tmp_path / "tmp.h5ad" + expected = np.arange(12).reshape(3, 4) + ad.AnnData(X=x).write_h5ad(pth) + + backed = ad.read_h5ad(pth, backed="r+") + assert backed.isbacked + + backed.filename = None + assert not backed.isbacked + assert backed.X is not None + + got = backed.X.toarray() if is_sparse else backed.X + np.testing.assert_array_equal(got, expected) + + def test_backed_modification(adata: ad.AnnData, backing_h5ad: Path): adata.X[:, 1] = 0 # Make it a little sparse adata.X = sparse.csr_matrix(adata.X) From 00c4376119fc9b823974316f71c66b4b116d09c2 Mon Sep 17 00:00:00 2001 From: SID Date: Sat, 27 Jun 2026 16:08:19 -0400 Subject: [PATCH 2/2] test(backed): satisfy lint in filename=None regression test Signed-off-by: SID --- tests/test_backed_hdf5.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/test_backed_hdf5.py b/tests/test_backed_hdf5.py index fb636677f..3bf387cbc 100644 --- a/tests/test_backed_hdf5.py +++ b/tests/test_backed_hdf5.py @@ -319,15 +319,15 @@ def test_return_to_memory_mode(adata: ad.AnnData, backing_h5ad: Path): @pytest.mark.parametrize( - ("x", "is_sparse"), + "x", [ - pytest.param(np.arange(12).reshape(3, 4), False, id="dense"), - pytest.param(sparse.csr_matrix(np.arange(12).reshape(3, 4)), True, id="csr"), - pytest.param(sparse.csc_matrix(np.arange(12).reshape(3, 4)), True, id="csc"), + pytest.param(np.arange(12).reshape(3, 4), id="dense"), + pytest.param(sparse.csr_matrix(np.arange(12).reshape(3, 4)), id="csr"), + pytest.param(sparse.csc_matrix(np.arange(12).reshape(3, 4)), id="csc"), ], ) def test_return_to_memory_mode_after_read_h5ad( - tmp_path: Path, x: np.ndarray | sparse.spmatrix, is_sparse: bool + tmp_path: Path, x: np.ndarray | sparse.spmatrix ): pth = tmp_path / "tmp.h5ad" expected = np.arange(12).reshape(3, 4) @@ -340,7 +340,7 @@ def test_return_to_memory_mode_after_read_h5ad( assert not backed.isbacked assert backed.X is not None - got = backed.X.toarray() if is_sparse else backed.X + got = backed.X.toarray() if sparse.issparse(backed.X) else backed.X np.testing.assert_array_equal(got, expected)