Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Dec 10, 2024
1 parent c18f81b commit 7b13099
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 14 deletions.
14 changes: 9 additions & 5 deletions xarray/backends/zarr.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,14 @@
from xarray.namedarray.utils import module_available

if TYPE_CHECKING:
from zarr import Array as ZarrArray
from zarr import Group as ZarrGroup

from xarray.backends.common import AbstractDataStore
from xarray.core.dataset import Dataset
from xarray.core.datatree import DataTree
from xarray.core.types import ReadBuffer
from zarr import Array as ZarrArray
from zarr import Group as ZarrGroup


def _get_mappers(*, storage_options, store, chunk_store):
# expand str and path-like arguments
Expand Down Expand Up @@ -735,7 +737,6 @@ def __init__(
use_zarr_fill_value_as_mask=None,
cache_members: bool = True,
):

self.zarr_group = zarr_group
self._read_only = self.zarr_group.read_only
self._synchronizer = self.zarr_group.synchronizer
Expand All @@ -753,10 +754,10 @@ def __init__(

if self._cache_members:
# initialize the cache
# this cache is created here and never updated.
# this cache is created here and never updated.
# If the `ZarrStore` instance creates a new zarr array, or if an external process
# removes an existing zarr array, then the cache will be invalid.
# create a new ZarrStore instance if you want to
# create a new ZarrStore instance if you want to
# capture the current state of the zarr group, or create a ZarrStore with
# `cache_members` set to `False` to disable this cache and instead fetch members
# on demand.
Expand All @@ -778,19 +779,22 @@ def _fetch_members(self) -> dict[str, ZarrArray]:
Get the arrays and groups defined in the zarr group modelled by this Store
"""
import zarr

if zarr.__version__ >= "3":
return dict(self.zarr_group.members())
else:
return dict(self.zarr_group.items())

def array_keys(self) -> tuple[str, ...]:
from zarr import Array as ZarrArray

return tuple(
key for (key, node) in self.members.items() if isinstance(node, ZarrArray)
)

def arrays(self) -> tuple[tuple[str, ZarrArray], ...]:
from zarr import Array as ZarrArray

return tuple(
(key, node)
for (key, node) in self.members.items()
Expand Down
17 changes: 8 additions & 9 deletions xarray/tests/test_backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -407,14 +407,14 @@ def test_zero_dimensional_variable(self) -> None:
with self.roundtrip(expected) as actual:
assert_identical(expected, actual)

def test_write_store(self) -> None:
expected = create_test_data()
with self.create_store() as store:
expected.dump_to_store(store)
# we need to cf decode the store because it has time and
# non-dimension coordinates
with xr.decode_cf(store) as actual:
assert_allclose(expected, actual)
def test_write_store(self) -> None:
expected = create_test_data()
with self.create_store() as store:
expected.dump_to_store(store)
# we need to cf decode the store because it has time and
# non-dimension coordinates
with xr.decode_cf(store) as actual:
assert_allclose(expected, actual)

def check_dtypes_roundtripped(self, expected, actual):
for k in expected.variables:
Expand Down Expand Up @@ -3340,7 +3340,6 @@ def create_zarr_target(self):
store = KVStore({}, **kwargs) # type: ignore[arg-type,unused-ignore]
yield store


def make_patches(self, store):
from unittest.mock import MagicMock

Expand Down

0 comments on commit 7b13099

Please sign in to comment.