Skip to content

Commit fa7fb8f

Browse files
committed
protect imports within TYPE_CHECKING bloc
1 parent 5e1c0b5 commit fa7fb8f

File tree

1 file changed

+23
-8
lines changed
  • virtualizarr/readers/hdf

1 file changed

+23
-8
lines changed

virtualizarr/readers/hdf/hdf.py

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
import math
22
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+
)
413

514
import numpy as np
615
import xarray as xr
@@ -21,12 +30,17 @@
2130
from virtualizarr.utils import _FsspecFSFromFilepath, check_for_collisions, soft_import
2231
from virtualizarr.zarr import ZArray
2332

24-
if TYPE_CHECKING:
25-
import h5py # type: ignore
26-
2733
h5py = soft_import("h5py", "For reading hdf files", strict=False)
2834

2935

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+
3044
class HDFVirtualBackend(VirtualBackend):
3145
@staticmethod
3246
def open_virtual_dataset(
@@ -85,7 +99,8 @@ def open_virtual_dataset(
8599

86100
@staticmethod
87101
def _dataset_chunk_manifest(
88-
path: str, dataset: h5py.Dataset
102+
path: str,
103+
dataset: H5Dataset,
89104
) -> Optional[ChunkManifest]:
90105
"""
91106
Generate ChunkManifest for HDF5 dataset.
@@ -154,7 +169,7 @@ def add_chunk_info(blob):
154169
return chunk_manifest
155170

156171
@staticmethod
157-
def _dataset_dims(dataset: h5py.Dataset) -> Union[List[str], List[None]]:
172+
def _dataset_dims(dataset: H5Dataset) -> Union[List[str], List[None]]:
158173
"""
159174
Get a list of dimension scale names attached to input HDF5 dataset.
160175
@@ -196,7 +211,7 @@ def _dataset_dims(dataset: h5py.Dataset) -> Union[List[str], List[None]]:
196211
return dims
197212

198213
@staticmethod
199-
def _extract_attrs(h5obj: Union[h5py.Dataset, h5py.Group]):
214+
def _extract_attrs(h5obj: Union[H5Dataset, H5Group]):
200215
"""
201216
Extract attributes from an HDF5 group or dataset.
202217
@@ -242,7 +257,7 @@ def _extract_attrs(h5obj: Union[h5py.Dataset, h5py.Group]):
242257
return attrs
243258

244259
@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]:
246261
"""
247262
Extract an xarray Variable with ManifestArray data from an h5py dataset
248263

0 commit comments

Comments
 (0)