Skip to content

Commit ee6fa0b

Browse files
Use soft import strategy for optional dependencies see xarray/issues/9554.
1 parent 83ff577 commit ee6fa0b

3 files changed

Lines changed: 22 additions & 13 deletions

File tree

virtualizarr/readers/hdf/filters.py

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,18 @@
11
import dataclasses
2-
import warnings
32
from typing import List, Tuple, TypedDict, Union
43

5-
import h5py # type: ignore
64
import numcodecs.registry as registry
75
import numpy as np
86
from numcodecs.abc import Codec
97
from numcodecs.fixedscaleoffset import FixedScaleOffset
108
from xarray.coding.variables import _choose_float_dtype
119

12-
try:
13-
import hdf5plugin # type: ignore
14-
except ModuleNotFoundError:
15-
hdf5plugin = None # type: ignore
16-
warnings.warn("hdf5plugin is required for HDF reader")
10+
from virtualizarr.utils import soft_import
11+
12+
h5py = soft_import("h5py", "For reading hdf files")
13+
hdf5plugin = soft_import("hdf5plugin", "For reading hdf files with filters")
14+
imagecodecs = soft_import("imagecodecs", "For reading hdf files with filters")
1715

18-
try:
19-
import imagecodecs # noqa
20-
except ModuleNotFoundError:
21-
warnings.warn("imagecodecs is required for HDF reader")
2216

2317
_non_standard_filters = {
2418
"gzip": "zlib",

virtualizarr/readers/hdf/hdf.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import math
22
from typing import Dict, Iterable, List, Mapping, Optional, Union
33

4-
import h5py # type: ignore
54
import numpy as np
65
from xarray import Dataset, Index, Variable
76

@@ -13,9 +12,11 @@
1312
)
1413
from virtualizarr.readers.hdf.filters import cfcodec_from_dataset, codecs_from_dataset
1514
from virtualizarr.types import ChunkKey
16-
from virtualizarr.utils import _FsspecFSFromFilepath, check_for_collisions
15+
from virtualizarr.utils import _FsspecFSFromFilepath, check_for_collisions, soft_import
1716
from virtualizarr.zarr import ZArray
1817

18+
h5py = soft_import("h5py", "For reading hdf files")
19+
1920

2021
class HDFVirtualBackend(VirtualBackend):
2122
@staticmethod

virtualizarr/utils.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import annotations
22

3+
import importlib
34
import io
45
from typing import TYPE_CHECKING, Iterable, Optional, Union
56

@@ -86,3 +87,16 @@ def check_for_collisions(
8687
raise ValueError(f"Cannot both load and drop variables {common}")
8788

8889
return drop_variables, loadable_variables
90+
91+
92+
def soft_import(name: str, reason: str, strict: Optional[bool] = True):
93+
try:
94+
return importlib.import_module(name)
95+
except (ImportError, ModuleNotFoundError):
96+
if strict:
97+
raise ImportError(
98+
f"for {reason}, the {name} package is required. "
99+
f"Please install it via pip or conda."
100+
)
101+
else:
102+
return None

0 commit comments

Comments
 (0)