Skip to content

Commit dcbd543

Browse files
authored
fix typing errors (#1433)
after zarr-developers/zarr-python#3304 we need to be specific about if we have V2 or V3 arrays
1 parent f9e2ea4 commit dcbd543

File tree

4 files changed

+31
-9
lines changed

4 files changed

+31
-9
lines changed

icechunk-python/python/icechunk/dask.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import functools
22
from collections.abc import Callable, Mapping
3-
from typing import Any, ParamSpec, TypeAlias, TypeVar
3+
from typing import TYPE_CHECKING, Any, ParamSpec, TypeAlias, TypeVar
44

55
import numpy as np
66
from packaging.version import Version
@@ -13,6 +13,12 @@
1313
from icechunk.distributed import extract_session, merge_sessions
1414
from icechunk.session import ForkSession
1515

16+
if TYPE_CHECKING:
17+
try:
18+
from zarr.core.metadata import ArrayV3Metadata
19+
except ImportError:
20+
ArrayV3Metadata = Any # type: ignore[misc,assignment]
21+
1622
SimpleGraph: TypeAlias = Mapping[tuple[str, int], tuple[Any, ...]]
1723

1824

@@ -57,7 +63,7 @@ def _assert_correct_dask_version() -> None:
5763
def store_dask(
5864
*,
5965
sources: list[Array],
60-
targets: list[zarr.Array],
66+
targets: "list[zarr.Array[ArrayV3Metadata]]",
6167
regions: list[tuple[slice, ...]] | None = None,
6268
split_every: int | None = None,
6369
**store_kwargs: Any,

icechunk-python/python/icechunk/distributed.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
# distributed utility functions
22
from collections.abc import Generator, Iterable
3-
from typing import Any, cast
3+
from typing import TYPE_CHECKING, Any, cast
44

55
import zarr
66
from icechunk import IcechunkStore
77
from icechunk.session import ForkSession, Session
88

9+
if TYPE_CHECKING:
10+
try:
11+
from zarr.core.metadata import ArrayV3Metadata
12+
except ImportError:
13+
ArrayV3Metadata = Any # type: ignore[misc,assignment]
14+
915
__all__ = [
1016
"extract_session",
1117
"merge_sessions",
@@ -25,7 +31,7 @@ def _flatten(seq: Iterable[Any], container: type = list) -> Generator[Any, None,
2531

2632

2733
def extract_session(
28-
zarray: zarr.Array, axis: Any = None, keepdims: Any = None
34+
zarray: "zarr.Array[ArrayV3Metadata]", axis: Any = None, keepdims: Any = None
2935
) -> Session:
3036
"""
3137
Extract Icechunk Session from a zarr Array, useful for distributed array computing frameworks.

icechunk-python/python/icechunk/testing/strategies.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11
from collections.abc import Iterable
2-
from typing import cast
2+
from typing import TYPE_CHECKING, Any
33

44
import hypothesis.strategies as st
55

66
import icechunk as ic
77
import zarr
8-
from zarr.core.metadata import ArrayV3Metadata
8+
9+
if TYPE_CHECKING:
10+
try:
11+
from zarr.core.metadata import ArrayV3Metadata
12+
except ImportError:
13+
ArrayV3Metadata = Any # type: ignore[misc,assignment]
914

1015

1116
@st.composite
1217
def splitting_configs(
13-
draw: st.DrawFn, *, arrays: Iterable[zarr.Array]
18+
draw: st.DrawFn, *, arrays: "Iterable[zarr.Array[ArrayV3Metadata]]"
1419
) -> ic.ManifestSplittingConfig:
1520
config_dict: dict[
1621
ic.ManifestSplitCondition,
@@ -29,7 +34,7 @@ def splitting_configs(
2934
else:
3035
array_condition = ic.ManifestSplitCondition.path_matches(array.path)
3136
dimnames = (
32-
cast(ArrayV3Metadata, array.metadata).dimension_names or (None,) * array.ndim
37+
getattr(array.metadata, "dimension_names", None) or (None,) * array.ndim
3338
)
3439
dimsize_axis_names = draw(
3540
st.lists(

icechunk-python/python/icechunk/xarray.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@
1515
from xarray.backends.common import ArrayWriter
1616
from xarray.backends.zarr import ZarrStore
1717

18+
try:
19+
from zarr.core.metadata import ArrayV3Metadata
20+
except ImportError:
21+
ArrayV3Metadata = Any # type: ignore[misc,assignment]
22+
1823
__all__ = ["to_icechunk"]
1924

2025
Region = Mapping[str, slice | Literal["auto"]] | Literal["auto"] | None
@@ -57,7 +62,7 @@ def __init__(self) -> None:
5762
super().__init__() # type: ignore[no-untyped-call]
5863

5964
self.eager_sources: list[np.ndarray[Any, Any]] = []
60-
self.eager_targets: list[zarr.Array] = []
65+
self.eager_targets: list[zarr.Array[ArrayV3Metadata]] = []
6166
self.eager_regions: list[tuple[slice, ...]] = []
6267

6368
def add(self, source: Any, target: Any, region: Any = None) -> Any:

0 commit comments

Comments
 (0)