Skip to content

Commit f8a45ba

Browse files
committed
More robust dtype check
main branch of zarr is not a higher version right now.
1 parent 5da6cca commit f8a45ba

File tree

3 files changed

+41
-18
lines changed

3 files changed

+41
-18
lines changed

xarray/backends/zarr.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
import numpy as np
1111
import pandas as pd
12-
from packaging.version import Version
1312

1413
from xarray import coding, conventions
1514
from xarray.backends.chunks import grid_rechunk, validate_grid_chunks_alignment
@@ -819,9 +818,9 @@ def open_store_variable(self, name):
819818
elif "_FillValue" in attributes:
820819
# TODO update version check for the released version with dtypes
821820
# probably be 3.1
822-
import zarr
823821

824-
if Version(zarr.__version__) > Version("3.0.9"):
822+
# if Version(zarr.__version__) > Version("3.0.10"):
823+
if hasattr(zarr_array.metadata.data_type, "to_native_dtype"):
825824
native_dtype = zarr_array.metadata.data_type.to_native_dtype()
826825
attributes["_FillValue"] = (
827826
# Use the new dtype infrastructure instead of doing xarray
@@ -961,12 +960,11 @@ def store(
961960
# To do so, we decode variables directly to access the proper encoding,
962961
# without going via xarray.Dataset to avoid needing to load
963962
# index variables into memory.
964-
store_vars = {
965-
k: self.open_store_variable(name=k) for k in existing_variable_names
966-
}
967963

968964
existing_vars, _, _ = conventions.decode_cf_variables(
969-
variables=store_vars,
965+
variables={
966+
k: self.open_store_variable(name=k) for k in existing_variable_names
967+
},
970968
# attributes = {} since we don't care about parsing the global
971969
# "coordinates" attribute
972970
attributes={},

xarray/tests/__init__.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -130,20 +130,26 @@ def _importorskip(
130130
has_rasterio, requires_rasterio = _importorskip("rasterio")
131131
has_zarr, requires_zarr = _importorskip("zarr")
132132
has_zarr_v3, requires_zarr_v3 = _importorskip("zarr", "3.0.0")
133-
has_zarr_v3_dtypes, requires_zarr_v3_dtypes = _importorskip("zarr", "3.0.9")
134-
135-
# Additional check for zarr dtype support (dev versions > 3.0.9)
136-
if has_zarr:
133+
has_zarr_v3_dtypes, requires_zarr_v3_dtypes = _importorskip("zarr", "3.0.10")
134+
if has_zarr_v3:
137135
import zarr
138136

139-
# Dev versions like "3.0.9.dev47+g9da38f75" are > "3.0.9"
140-
has_zarr_v3_dtypes = has_zarr_v3_dtypes or (
141-
Version(zarr.__version__) > Version("3.0.9")
142-
)
137+
has_zarr_v3_dtypes = hasattr(zarr.core, "dtype")
138+
139+
# has_zarr_v3_dtypes = hasattr(zarr.Array.metadata.data_type, "to_native_dtype")
143140
requires_zarr_v3_dtypes = pytest.mark.skipif(
144-
not has_zarr_v3_dtypes, reason="requires zarr>3.0.9 (including dev versions)"
141+
not has_zarr_v3_dtypes, reason="requires zarr>3.0.10 (including dev versions)"
145142
)
146143

144+
# Additional check for zarr dtype support (dev versions > 3.0.9)
145+
# if has_zarr:
146+
# import zarr
147+
#
148+
# # Dev versions like "3.0.10.dev47+g9da38f75" are > "3.0.10"
149+
# has_zarr_v3_dtypes = has_zarr_v3_dtypes or (
150+
# Version(zarr.__version__) > Version("3.0.10")
151+
# )
152+
147153
has_fsspec, requires_fsspec = _importorskip("fsspec")
148154
has_iris, requires_iris = _importorskip("iris")
149155
has_numbagg, requires_numbagg = _importorskip("numbagg")

xarray/tests/test_backends.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2908,8 +2908,11 @@ def test_append_with_existing_encoding_raises(self) -> None:
29082908

29092909
@pytest.mark.parametrize("dtype", ["U", "S"])
29102910
def test_append_string_length_mismatch_raises(self, dtype) -> None:
2911-
if has_zarr_v3() and not has_zarr_v3_dtypes:
2912-
pytest.skip("This works on pre dtype updated zarr python")
2911+
print("start")
2912+
print(has_zarr_v3)
2913+
print(has_zarr_v3_dtypes)
2914+
if has_zarr_v3 and not has_zarr_v3_dtypes:
2915+
skip_if_zarr_format_3("This actually works fine with Zarr format 3")
29132916

29142917
ds, ds_to_append = create_append_string_length_mismatch_test_data(dtype)
29152918
with self.create_zarr_target() as store_target:
@@ -2919,6 +2922,22 @@ def test_append_string_length_mismatch_raises(self, dtype) -> None:
29192922
store_target, append_dim="time", **self.version_kwargs
29202923
)
29212924

2925+
@pytest.mark.parametrize("dtype", ["U", "S"])
2926+
def test_append_string_length_mismatch_works(self, dtype) -> None:
2927+
skip_if_zarr_format_2("This doesn't work with Zarr format 2")
2928+
# ...but it probably would if we used object dtype
2929+
if has_zarr_v3_dtypes:
2930+
pytest.skip("This works on pre dtype updated zarr python")
2931+
2932+
ds, ds_to_append = create_append_string_length_mismatch_test_data(dtype)
2933+
expected = xr.concat([ds, ds_to_append], dim="time")
2934+
2935+
with self.create_zarr_target() as store_target:
2936+
ds.to_zarr(store_target, mode="w", **self.version_kwargs)
2937+
ds_to_append.to_zarr(store_target, append_dim="time", **self.version_kwargs)
2938+
actual = xr.open_dataset(store_target, engine="zarr")
2939+
xr.testing.assert_identical(expected, actual)
2940+
29222941
def test_check_encoding_is_consistent_after_append(self) -> None:
29232942
ds, ds_to_append, _ = create_append_test_data()
29242943

0 commit comments

Comments
 (0)