From 17b03a7b12667bb49ebfca49c58de8d663818461 Mon Sep 17 00:00:00 2001 From: Ilan Gold Date: Mon, 11 Nov 2024 16:55:02 +0100 Subject: [PATCH] (fix): resolve cupy import error on `cupy-cuda12x<0.13` (#1754) * (fix): resolve cupy import error on `cupy-cuda12x<0.13` * (chore): release note --- docs/release-notes/1754.bugfix.md | 1 + src/anndata/compat/__init__.py | 11 ++++++++++- 2 files changed, 11 insertions(+), 1 deletion(-) create mode 100644 docs/release-notes/1754.bugfix.md diff --git a/docs/release-notes/1754.bugfix.md b/docs/release-notes/1754.bugfix.md new file mode 100644 index 000000000..492553ef1 --- /dev/null +++ b/docs/release-notes/1754.bugfix.md @@ -0,0 +1 @@ +Fix `cupy<0.13` imports in non-gpu environments {user}`ilan-gold` diff --git a/src/anndata/compat/__init__.py b/src/anndata/compat/__init__.py index b5ec3d415..255ffa548 100644 --- a/src/anndata/compat/__init__.py +++ b/src/anndata/compat/__init__.py @@ -140,7 +140,16 @@ def __repr__(): return "mock dask.array.core.Array" -if find_spec("cupy") or TYPE_CHECKING: +# https://github.com/scverse/anndata/issues/1749 +def is_cupy_importable() -> bool: + try: + import cupy # noqa: F401 + except ImportError: + return False + return True + + +if is_cupy_importable() or TYPE_CHECKING: from cupy import ndarray as CupyArray from cupyx.scipy.sparse import csc_matrix as CupyCSCMatrix from cupyx.scipy.sparse import csr_matrix as CupyCSRMatrix