Skip to content

Import dask and cubed only once #457

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 10 commits into from
Aug 15, 2025
33 changes: 17 additions & 16 deletions flox/xrutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import datetime
import importlib
from collections.abc import Iterable
from types import ModuleType
from typing import Any

import numpy as np
Expand Down Expand Up @@ -45,13 +46,20 @@ def module_available(module: str, minversion: str | None = None) -> bool:
except ImportError:
cftime = None

cubed: ModuleType | None
try:
import cubed # type: ignore[no-redef]
except ImportError:
cubed = None

dask: ModuleType | None
try:
import dask.array

dask_array_type = dask.array.Array
dask_array_type = dask.array.Array # type: ignore[union-attr]
except ImportError:
dask_array_type = () # type: ignore[assignment, misc]
dask = None
dask_array_type = ()


def asarray(data, xp=np):
Expand Down Expand Up @@ -79,26 +87,19 @@ def is_chunked_array(x) -> bool:


def is_dask_collection(x):
try:
import dask

return dask.is_dask_collection(x)

except ImportError:
if dask is None:
return False
return dask.is_dask_collection(x)


def is_duck_dask_array(x):
return is_duck_array(x) and is_dask_collection(x)


def is_duck_cubed_array(x):
try:
import cubed

return is_duck_array(x) and isinstance(x, cubed.Array)
except ImportError:
if cubed is None:
return False
return is_duck_array(x) and isinstance(x, cubed.Array)


class ReprObject:
Expand Down Expand Up @@ -140,7 +141,7 @@ def is_scalar(value: Any, include_0d: bool = True) -> bool:
or isinstance(value, str | bytes | dict)
or not (
isinstance(value, (Iterable,) + NON_NUMPY_SUPPORTED_ARRAY_TYPES)
or hasattr(value, "__array_function__")
or hasattr(value, "__array_function__") # type: ignore[unreachable]
)
)

Expand Down Expand Up @@ -182,13 +183,13 @@ def isnull(data: Any):
else:
# at this point, array should have dtype=object
if isinstance(data, (np.ndarray, dask_array_type)): # noqa
return pd.isnull(data) # type: ignore[arg-type]
return pd.isnull(data)
else:
# Not reachable yet, but intended for use with other duck array
# types. For full consistency with pandas, we should accept None as
# a null value as well as NaN, but it isn't clear how to do this
# with duck typing.
return data != data
return data != data # type: ignore[unreachable]


def datetime_to_numeric(array, offset=None, datetime_unit=None, dtype=float):
Expand Down
Loading