Skip to content

Stop patching Dask #78

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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: 4 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ classifiers = [
dynamic = [ "description", "version" ]
dependencies = [ "numpy" ]
optional-dependencies.accel = [ "numba" ]
optional-dependencies.dask = [ "dask>=2025.3" ]
optional-dependencies.doc = [
"furo",
"pytest",
Expand All @@ -29,7 +30,8 @@ optional-dependencies.doc = [
"sphinx-autodoc-typehints",
"sphinx-autofixture",
]
optional-dependencies.full = [ "dask", "fast-array-utils[accel,sparse]", "h5py", "zarr" ]
optional-dependencies.full = [ "fast-array-utils[accel,dask,h5py,sparse,zarr]" ]
optional-dependencies.h5py = [ "h5py" ]
optional-dependencies.sparse = [ "scipy>=1.8" ]
optional-dependencies.test = [
"anndata",
Expand All @@ -44,6 +46,7 @@ optional-dependencies.test-min = [
"pytest-doctestplus",
]
optional-dependencies.testing = [ "packaging" ]
optional-dependencies.zarr = [ "zarr" ]
urls.'Documentation' = "https://icb-fast-array-utils.readthedocs-hosted.com/"
urls.'Issue Tracker' = "https://github.com/scverse/fast-array-utils/issues"
urls.'Source Code' = "https://github.com/scverse/fast-array-utils"
Expand Down
4 changes: 1 addition & 3 deletions src/fast_array_utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@

from __future__ import annotations

from . import _patches, conv, stats, types
from . import conv, stats, types


__all__ = ["conv", "stats", "types"]

_patches.patch_dask()
45 changes: 45 additions & 0 deletions src/fast_array_utils/_checks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
from __future__ import annotations

from functools import cache, wraps
from importlib.metadata import version
from typing import TYPE_CHECKING

from packaging.version import Version

from . import types


if TYPE_CHECKING:
from collections.abc import Callable
from typing import Concatenate, ParamSpec, TypeVar

_DA = TypeVar("_DA", bound=types.DaskArray)
_P = ParamSpec("_P")
_R = TypeVar("_R")


__all__ = ["check_dask_sparray_support"]


@cache
def _dask_supports_sparray() -> bool:
return Version(version("dask")) >= Version("2025.3")


def check_dask_sparray_support(
func: Callable[Concatenate[_DA, _P], _R],
) -> Callable[Concatenate[_DA, _P], _R]:
"""Check that Dask isn’t too old when trying to use it with `scipy.sparse.sparray`s."""

@wraps(func)
def decorated(arr: _DA, *args: _P.args, **kwargs: _P.kwargs) -> _R:
if (
isinstance(arr, types.DaskArray)
and isinstance(arr._meta, types.sparray) # noqa: SLF001
and not _dask_supports_sparray()
):
msg = "dask < 2025.3 does not support `scipy.sparse.sparray`s"
raise RuntimeError(msg)
return func(arr, *args, **kwargs)

return decorated
26 changes: 0 additions & 26 deletions src/fast_array_utils/_patches.py

This file was deleted.

2 changes: 2 additions & 0 deletions src/fast_array_utils/conv/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from typing import TYPE_CHECKING, overload

from .._checks import check_dask_sparray_support
from ..typing import CpuArray, DiskArray, GpuArray # noqa: TC001
from ._to_dense import to_dense_

Expand Down Expand Up @@ -47,6 +48,7 @@ def to_dense(
) -> NDArray[Any]: ...


@check_dask_sparray_support
def to_dense(
x: CpuArray
| GpuArray
Expand Down
5 changes: 5 additions & 0 deletions src/fast_array_utils/stats/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from typing import TYPE_CHECKING, overload

from .._checks import check_dask_sparray_support
from .._validation import validate_axis
from ..typing import CpuArray, DiskArray, GpuArray # noqa: TC001

Expand Down Expand Up @@ -38,6 +39,7 @@ def is_constant(x: types.CupyArray, /, *, axis: Literal[0, 1]) -> types.CupyArra
def is_constant(x: types.DaskArray, /, *, axis: Literal[0, 1, None] = None) -> types.DaskArray: ...


@check_dask_sparray_support
def is_constant(
x: NDArray[Any] | types.CSBase | types.CupyArray | types.DaskArray,
/,
Expand Down Expand Up @@ -103,6 +105,7 @@ def mean(
) -> types.DaskArray: ...


@check_dask_sparray_support
def mean(
x: CpuArray | GpuArray | DiskArray | types.DaskArray,
/,
Expand Down Expand Up @@ -166,6 +169,7 @@ def mean_var(
) -> tuple[types.DaskArray, types.DaskArray]: ...


@check_dask_sparray_support
def mean_var(
x: CpuArray | GpuArray | types.DaskArray,
/,
Expand Down Expand Up @@ -242,6 +246,7 @@ def sum(
) -> types.DaskArray: ...


@check_dask_sparray_support
def sum(
x: CpuArray | GpuArray | DiskArray | types.DaskArray,
/,
Expand Down
Loading