Skip to content

Commit d62b276

Browse files
authored
Fix some typing (#9988)
1 parent 1c7ee65 commit d62b276

File tree

5 files changed

+26
-18
lines changed

5 files changed

+26
-18
lines changed

xarray/backends/api.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
NestedSequence,
6565
ReadBuffer,
6666
T_Chunks,
67+
ZarrStoreLike,
6768
)
6869

6970
T_NetcdfEngine = Literal["netcdf4", "scipy", "h5netcdf"]
@@ -2100,7 +2101,7 @@ def save_mfdataset(
21002101
@overload
21012102
def to_zarr(
21022103
dataset: Dataset,
2103-
store: MutableMapping | str | os.PathLike[str] | None = None,
2104+
store: ZarrStoreLike | None = None,
21042105
chunk_store: MutableMapping | str | os.PathLike | None = None,
21052106
mode: ZarrWriteModes | None = None,
21062107
synchronizer=None,
@@ -2123,7 +2124,7 @@ def to_zarr(
21232124
@overload
21242125
def to_zarr(
21252126
dataset: Dataset,
2126-
store: MutableMapping | str | os.PathLike[str] | None = None,
2127+
store: ZarrStoreLike | None = None,
21272128
chunk_store: MutableMapping | str | os.PathLike | None = None,
21282129
mode: ZarrWriteModes | None = None,
21292130
synchronizer=None,
@@ -2144,7 +2145,7 @@ def to_zarr(
21442145

21452146
def to_zarr(
21462147
dataset: Dataset,
2147-
store: MutableMapping | str | os.PathLike[str] | None = None,
2148+
store: ZarrStoreLike | None = None,
21482149
chunk_store: MutableMapping | str | os.PathLike | None = None,
21492150
mode: ZarrWriteModes | None = None,
21502151
synchronizer=None,

xarray/backends/zarr.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import os
66
import struct
77
from collections.abc import Hashable, Iterable, Mapping
8-
from typing import TYPE_CHECKING, Any, Literal
8+
from typing import TYPE_CHECKING, Any, Literal, cast
99

1010
import numpy as np
1111
import pandas as pd
@@ -38,13 +38,10 @@
3838
from xarray.namedarray.utils import module_available
3939

4040
if TYPE_CHECKING:
41-
from zarr import Array as ZarrArray
42-
from zarr import Group as ZarrGroup
43-
4441
from xarray.backends.common import AbstractDataStore
4542
from xarray.core.dataset import Dataset
4643
from xarray.core.datatree import DataTree
47-
from xarray.core.types import ReadBuffer
44+
from xarray.core.types import ReadBuffer, ZarrArray, ZarrGroup
4845

4946

5047
def _get_mappers(*, storage_options, store, chunk_store):
@@ -108,8 +105,7 @@ def _choose_default_mode(
108105

109106

110107
def _zarr_v3() -> bool:
111-
# TODO: switch to "3" once Zarr V3 is released
112-
return module_available("zarr", minversion="2.99")
108+
return module_available("zarr", minversion="3")
113109

114110

115111
# need some special secret attributes to tell us the dimensions
@@ -768,7 +764,7 @@ def __init__(
768764
self._members = self._fetch_members()
769765

770766
@property
771-
def members(self) -> dict[str, ZarrArray]:
767+
def members(self) -> dict[str, ZarrArray | ZarrGroup]:
772768
"""
773769
Model the arrays and groups contained in self.zarr_group as a dict. If `self._cache_members`
774770
is true, the dict is cached. Otherwise, it is retrieved from storage.
@@ -778,7 +774,7 @@ def members(self) -> dict[str, ZarrArray]:
778774
else:
779775
return self._members
780776

781-
def _fetch_members(self) -> dict[str, ZarrArray]:
777+
def _fetch_members(self) -> dict[str, ZarrArray | ZarrGroup]:
782778
"""
783779
Get the arrays and groups defined in the zarr group modelled by this Store
784780
"""
@@ -1035,6 +1031,7 @@ def sync(self):
10351031

10361032
def _open_existing_array(self, *, name) -> ZarrArray:
10371033
import zarr
1034+
from zarr import Array as ZarrArray
10381035

10391036
# TODO: if mode="a", consider overriding the existing variable
10401037
# metadata. This would need some case work properly with region
@@ -1066,7 +1063,7 @@ def _open_existing_array(self, *, name) -> ZarrArray:
10661063
else:
10671064
zarr_array = self.zarr_group[name]
10681065

1069-
return zarr_array
1066+
return cast(ZarrArray, zarr_array)
10701067

10711068
def _create_new_array(
10721069
self, *, name, shape, dtype, fill_value, encoding, attrs

xarray/core/datatree_io.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
from __future__ import annotations
22

3-
from collections.abc import Mapping, MutableMapping
3+
from collections.abc import Mapping
44
from os import PathLike
5-
from typing import Any, Literal, get_args
5+
from typing import TYPE_CHECKING, Any, Literal, get_args
66

77
from xarray.core.datatree import DataTree
88
from xarray.core.types import NetcdfWriteModes, ZarrWriteModes
99

1010
T_DataTreeNetcdfEngine = Literal["netcdf4", "h5netcdf"]
1111
T_DataTreeNetcdfTypes = Literal["NETCDF4"]
1212

13+
if TYPE_CHECKING:
14+
from xarray.core.types import ZarrStoreLike
15+
1316

1417
def _datatree_to_netcdf(
1518
dt: DataTree,
@@ -78,7 +81,7 @@ def _datatree_to_netcdf(
7881

7982
def _datatree_to_zarr(
8083
dt: DataTree,
81-
store: MutableMapping | str | PathLike[str],
84+
store: ZarrStoreLike,
8285
mode: ZarrWriteModes = "w-",
8386
encoding: Mapping[str, Any] | None = None,
8487
consolidated: bool = True,

xarray/core/types.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,15 @@
7070

7171
try:
7272
from zarr import Array as ZarrArray
73+
from zarr import Group as ZarrGroup
7374
except ImportError:
74-
ZarrArray = np.ndarray
75+
ZarrArray = np.ndarray # type: ignore[misc, assignment, unused-ignore]
76+
ZarrGroup = Any # type: ignore[misc, assignment, unused-ignore]
77+
try:
78+
# this is V3 only
79+
from zarr.storage import StoreLike as ZarrStoreLike
80+
except ImportError:
81+
ZarrStoreLike = Any # type: ignore[misc, assignment, unused-ignore]
7582

7683
# Anything that can be coerced to a shape tuple
7784
_ShapeLike = Union[SupportsIndex, Sequence[SupportsIndex]]

xarray/tests/test_backends_datatree.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ def test_to_zarr_zip_store(self, tmpdir, simple_datatree):
414414
store = ZipStore(filepath)
415415
original_dt.to_zarr(store)
416416

417-
with open_datatree(store, engine="zarr") as roundtrip_dt:
417+
with open_datatree(store, engine="zarr") as roundtrip_dt: # type: ignore[arg-type, unused-ignore]
418418
assert_equal(original_dt, roundtrip_dt)
419419

420420
def test_to_zarr_not_consolidated(self, tmpdir, simple_datatree):

0 commit comments

Comments
 (0)