Skip to content

(fix): check xarray installation when using any xarray-dependent code + fix string dtypes with new bound #1991

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

Merged
merged 21 commits into from
Jun 12, 2025
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
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
2 changes: 1 addition & 1 deletion hatch.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ scripts.clean = "git restore --source=HEAD --staged --worktree -- docs/release-n

[envs.hatch-test]
default-args = [ ]
features = [ "dev", "test-min" ]
features = [ "test-min" ]
extra-dependencies = [ "ipykernel" ]
env-vars.UV_CONSTRAINT = "ci/constraints.txt"
overrides.matrix.deps.env-vars = [
Expand Down
14 changes: 14 additions & 0 deletions src/anndata/_core/xarray.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,26 @@
from __future__ import annotations

import warnings
from functools import wraps

import pandas as pd

from ..compat import XDataArray, XDataset, XVariable


def requires_xarray(func):
@wraps(func)
def wrapper(*args, **kwargs):
try:
import xarray # noqa: F401
except ImportError as e:
msg = "xarray is required to read dataframes lazily. Please install xarray."
raise ImportError(msg) from e
return func(*args, **kwargs)

return wrapper


class Dataset2D(XDataset):
"""
A wrapper class meant to enable working with lazy dataframe data.
Expand Down
5 changes: 4 additions & 1 deletion src/anndata/_io/specs/lazy_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

import anndata as ad
from anndata._core.file_backing import filename, get_elem_name
from anndata._core.xarray import Dataset2D
from anndata._core.xarray import Dataset2D, requires_xarray
from anndata.abc import CSCDataset, CSRDataset
from anndata.compat import DaskArray, H5Array, H5Group, XDataArray, ZarrArray, ZarrGroup

Expand Down Expand Up @@ -258,6 +258,7 @@ def _gen_xarray_dict_iterator_from_elems(

@_LAZY_REGISTRY.register_read(ZarrGroup, IOSpec("dataframe", "0.2.0"))
@_LAZY_REGISTRY.register_read(H5Group, IOSpec("dataframe", "0.2.0"))
@requires_xarray
def read_dataframe(
elem: H5Group | ZarrGroup,
*,
Expand Down Expand Up @@ -297,6 +298,7 @@ def read_dataframe(

@_LAZY_REGISTRY.register_read(ZarrGroup, IOSpec("categorical", "0.2.0"))
@_LAZY_REGISTRY.register_read(H5Group, IOSpec("categorical", "0.2.0"))
@requires_xarray
def read_categorical(
elem: H5Group | ZarrGroup,
*,
Expand All @@ -317,6 +319,7 @@ def read_categorical(
)


@requires_xarray
def read_nullable(
elem: H5Group | ZarrGroup,
*,
Expand Down
9 changes: 2 additions & 7 deletions src/anndata/experimental/backed/_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from testing.anndata._doctest import doctest_needs

from ..._core.anndata import AnnData
from ..._core.xarray import requires_xarray
from ..._settings import settings
from ...compat import ZarrGroup, is_zarr_v2
from .. import read_dispatched
Expand All @@ -25,6 +26,7 @@


@doctest_needs("xarray")
@requires_xarray
def read_lazy(
store: PathLike[str] | str | MutableMapping | ZarrGroup | h5py.Dataset,
*,
Expand Down Expand Up @@ -81,13 +83,6 @@ def read_lazy(
AnnData object with n_obs × n_vars = 490 × 33452
obs: 'donor_id', 'self_reported_ethnicity_ontology_term_id', 'organism_ontology_term_id'...
"""
try:
import xarray # noqa: F401
except ImportError as e:
msg = (
"xarray is required to use the `read_lazy` function. Please install xarray."
)
raise ImportError(msg) from e
is_h5_store = isinstance(store, h5py.Dataset | h5py.File | h5py.Group)
is_h5 = (
isinstance(store, PathLike | str) and Path(store).suffix == ".h5ad"
Expand Down
15 changes: 12 additions & 3 deletions tests/test_readwrite.py
Original file line number Diff line number Diff line change
Expand Up @@ -952,8 +952,17 @@ def test_forward_slash_key(elem_key, tmp_path):


@pytest.mark.skipif(
find_spec("xarray"), reason="Xarray is installed so `read_lazy` will not error"
find_spec("xarray"),
reason="Xarray is installed so `read_{elem_}lazy` will not error",
)
def test_read_lazy_import_error():
@pytest.mark.parametrize(
"func", [ad.experimental.read_lazy, ad.experimental.read_elem_lazy]
)
def test_read_lazy_import_error(func, tmp_path):
ad.AnnData(np.ones((10, 10))).write_zarr(tmp_path)
with pytest.raises(ImportError, match="xarray"):
ad.experimental.read_lazy("test.zarr")
func(
zarr.open(
tmp_path if func is ad.experimental.read_lazy else tmp_path / "obs"
)
)
3 changes: 3 additions & 0 deletions tests/test_xarray.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import string
from importlib.util import find_spec

import numpy as np
import pandas as pd
Expand All @@ -10,6 +11,8 @@
from anndata.compat import XDataArray, XDataset, XVariable
from anndata.tests.helpers import gen_typed_df

pytestmark = pytest.mark.skipif(not find_spec("xarray"), reason="xarray not installed")


@pytest.fixture
def df():
Expand Down
Loading