|
1 | 1 | import math |
2 | 2 | from pathlib import Path |
3 | | -from typing import TYPE_CHECKING, Dict, Iterable, List, Mapping, Optional, Union |
| 3 | +from typing import ( |
| 4 | + TYPE_CHECKING, |
| 5 | + Any, |
| 6 | + Dict, |
| 7 | + Iterable, |
| 8 | + List, |
| 9 | + Mapping, |
| 10 | + Optional, |
| 11 | + Union, |
| 12 | +) |
4 | 13 |
|
5 | 14 | import numpy as np |
6 | 15 | import xarray as xr |
|
21 | 30 | from virtualizarr.utils import _FsspecFSFromFilepath, check_for_collisions, soft_import |
22 | 31 | from virtualizarr.zarr import ZArray |
23 | 32 |
|
24 | | -if TYPE_CHECKING: |
25 | | - import h5py # type: ignore |
26 | | - |
27 | 33 | h5py = soft_import("h5py", "For reading hdf files", strict=False) |
28 | 34 |
|
29 | 35 |
|
| 36 | +if TYPE_CHECKING: |
| 37 | + from h5py import Dataset as H5Dataset # type: ignore[import-untyped] |
| 38 | + from h5py import Group as H5Group # type: ignore[import-untyped] |
| 39 | +else: |
| 40 | + H5Dataset: Any = None |
| 41 | + H5Group: Any = None |
| 42 | + |
| 43 | + |
30 | 44 | class HDFVirtualBackend(VirtualBackend): |
31 | 45 | @staticmethod |
32 | 46 | def open_virtual_dataset( |
@@ -85,7 +99,8 @@ def open_virtual_dataset( |
85 | 99 |
|
86 | 100 | @staticmethod |
87 | 101 | def _dataset_chunk_manifest( |
88 | | - path: str, dataset: h5py.Dataset |
| 102 | + path: str, |
| 103 | + dataset: H5Dataset, |
89 | 104 | ) -> Optional[ChunkManifest]: |
90 | 105 | """ |
91 | 106 | Generate ChunkManifest for HDF5 dataset. |
@@ -154,7 +169,7 @@ def add_chunk_info(blob): |
154 | 169 | return chunk_manifest |
155 | 170 |
|
156 | 171 | @staticmethod |
157 | | - def _dataset_dims(dataset: h5py.Dataset) -> Union[List[str], List[None]]: |
| 172 | + def _dataset_dims(dataset: H5Dataset) -> Union[List[str], List[None]]: |
158 | 173 | """ |
159 | 174 | Get a list of dimension scale names attached to input HDF5 dataset. |
160 | 175 |
|
@@ -196,7 +211,7 @@ def _dataset_dims(dataset: h5py.Dataset) -> Union[List[str], List[None]]: |
196 | 211 | return dims |
197 | 212 |
|
198 | 213 | @staticmethod |
199 | | - def _extract_attrs(h5obj: Union[h5py.Dataset, h5py.Group]): |
| 214 | + def _extract_attrs(h5obj: Union[H5Dataset, H5Group]): |
200 | 215 | """ |
201 | 216 | Extract attributes from an HDF5 group or dataset. |
202 | 217 |
|
@@ -242,7 +257,7 @@ def _extract_attrs(h5obj: Union[h5py.Dataset, h5py.Group]): |
242 | 257 | return attrs |
243 | 258 |
|
244 | 259 | @staticmethod |
245 | | - def _dataset_to_variable(path: str, dataset: h5py.Dataset) -> Optional[xr.Variable]: |
| 260 | + def _dataset_to_variable(path: str, dataset: H5Dataset) -> Optional[xr.Variable]: |
246 | 261 | """ |
247 | 262 | Extract an xarray Variable with ManifestArray data from an h5py dataset |
248 | 263 |
|
|
0 commit comments