diff --git a/anndata/_core/sparse_dataset.py b/anndata/_core/sparse_dataset.py index 3041c5dee..360872bff 100644 --- a/anndata/_core/sparse_dataset.py +++ b/anndata/_core/sparse_dataset.py @@ -508,3 +508,30 @@ def sparse_dataset(group: ZarrGroup | H5Group) -> CSRDataset | CSCDataset: @_subset.register(BaseCompressedSparseDataset) def subset_sparsedataset(d, subset_idx): return d[subset_idx] + + +## Backwards compat + +_sparsedataset_depr_msg = """\ +SparseDataset is deprecated and will be removed in late 2024. It has been replaced by the public classes CSRDataset and CSCDataset. + +For instance checks, use `isinstance(X, (anndata.experimental.CSRDataset, anndata.experimental.CSCDataset))` instead. + +For creation, use `anndata.experimental.sparse_dataset(X)` instead. +""" + + +class SparseDataset(ABC): + """DEPRECATED. + + Use CSRDataset, CSCDataset, and sparse_dataset from anndata.experimental instead. + """ + + def __new__(cls, group): + warnings.warn(FutureWarning(_sparsedataset_depr_msg), stacklevel=2) + return sparse_dataset(group) + + @classmethod + def __subclasshook__(cls, C): + warnings.warn(FutureWarning(_sparsedataset_depr_msg), stacklevel=3) + return issubclass(C, (CSRDataset, CSCDataset)) diff --git a/anndata/tests/test_deprecations.py b/anndata/tests/test_deprecations.py index 01f202aec..a6bcfd478 100644 --- a/anndata/tests/test_deprecations.py +++ b/anndata/tests/test_deprecations.py @@ -10,10 +10,12 @@ import h5py import numpy as np import pytest +import zarr from scipy import sparse import anndata as ad from anndata import AnnData +from anndata.experimental import CSRDataset, write_elem from anndata.tests.helpers import assert_equal @@ -144,3 +146,18 @@ def test_deprecated_sparse_dataset_values(): with pytest.warns(FutureWarning, match="Please use .format"): mtx_backed.format_str + + +def test_deprecated_sparse_dataset(): + from anndata._core.sparse_dataset import SparseDataset + + mem_X = sparse.random(50, 50, format="csr") + g = zarr.group() + write_elem(g, "X", mem_X) + with pytest.warns(FutureWarning, match="SparseDataset is deprecated"): + X = SparseDataset(g["X"]) + + assert isinstance(X, CSRDataset) + + with pytest.warns(FutureWarning, match="SparseDataset is deprecated"): + assert isinstance(X, SparseDataset) diff --git a/docs/release-notes/0.10.2.md b/docs/release-notes/0.10.2.md index 512a90436..2bc379b3e 100644 --- a/docs/release-notes/0.10.2.md +++ b/docs/release-notes/0.10.2.md @@ -3,6 +3,10 @@ ```{rubric} Bugfix ``` +* Added compatibility layer for packages relying on `anndata._core.sparse_dataset.SparseDataset`. + Note that this API is *deprecated* and new code should use {class}`~anndata.experimental.CSRDataset`, {class}`~anndata.experimental.CSCDataset`, and {func}`~anndata.experimental.sparse_dataset` instead. + {pr}`1185` {user}`ivirshup` + ```{rubric} Documentation ```