Skip to content

Commit 7077fae

Browse files
committed
Fix zarr upstream tests
1 parent 251329e commit 7077fae

File tree

2 files changed

+35
-49
lines changed

2 files changed

+35
-49
lines changed

xarray/backends/zarr.py

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -447,10 +447,11 @@ def extract_zarr_variable_encoding(
447447

448448
safe_to_drop = {"source", "original_shape", "preferred_chunks"}
449449
valid_encodings = {
450-
"codecs",
451450
"chunks",
452-
"compressor",
451+
"compressor", # TODO: delete when min zarr >=3
452+
"compressors",
453453
"filters",
454+
"serializer",
454455
"cache_metadata",
455456
"write_empty_chunks",
456457
}
@@ -480,7 +481,7 @@ def extract_zarr_variable_encoding(
480481
mode=mode,
481482
shape=shape,
482483
)
483-
encoding["chunks"] = chunks
484+
encoding["chunks"] = chunks or "auto"
484485
return encoding
485486

486487

@@ -816,22 +817,17 @@ def open_store_variable(self, name):
816817
)
817818
attributes = dict(attributes)
818819

819-
# TODO: this should not be needed once
820-
# https://github.com/zarr-developers/zarr-python/issues/1269 is resolved.
821-
attributes.pop("filters", None)
822-
823820
encoding = {
824821
"chunks": zarr_array.chunks,
825822
"preferred_chunks": dict(zip(dimensions, zarr_array.chunks, strict=True)),
826823
}
827824

828-
if _zarr_v3() and zarr_array.metadata.zarr_format == 3:
829-
encoding["codecs"] = [x.to_dict() for x in zarr_array.metadata.codecs]
830-
elif _zarr_v3():
825+
if _zarr_v3():
831826
encoding.update(
832827
{
833-
"compressor": zarr_array.metadata.compressor,
834-
"filters": zarr_array.metadata.filters,
828+
"compressors": zarr_array.compressors,
829+
"filters": zarr_array.filters,
830+
# "serializer": zarr_array.serializer,
835831
}
836832
)
837833
else:

xarray/tests/test_backends.py

Lines changed: 27 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -134,21 +134,21 @@
134134

135135

136136
@pytest.fixture(scope="module", params=ZARR_FORMATS)
137-
def default_zarr_version(request) -> Generator[None, None]:
137+
def default_zarr_format(request) -> Generator[None, None]:
138138
if has_zarr_v3:
139-
with zarr.config.set(default_zarr_version=request.param):
139+
with zarr.config.set(default_zarr_format=request.param):
140140
yield
141141
else:
142142
yield
143143

144144

145145
def skip_if_zarr_format_3(reason: str):
146-
if has_zarr_v3 and zarr.config["default_zarr_version"] == 3:
146+
if has_zarr_v3 and zarr.config["default_zarr_format"] == 3:
147147
pytest.skip(reason=f"Unsupported with zarr_format=3: {reason}")
148148

149149

150150
def skip_if_zarr_format_2(reason: str):
151-
if not has_zarr_v3 or (zarr.config["default_zarr_version"] == 2):
151+
if not has_zarr_v3 or (zarr.config["default_zarr_format"] == 2):
152152
pytest.skip(reason=f"Unsupported with zarr_format=2: {reason}")
153153

154154

@@ -2270,7 +2270,7 @@ def test_roundtrip_coordinates(self) -> None:
22702270

22712271

22722272
@requires_zarr
2273-
@pytest.mark.usefixtures("default_zarr_version")
2273+
@pytest.mark.usefixtures("default_zarr_format")
22742274
class ZarrBase(CFEncodedBase):
22752275
DIMENSION_KEY = "_ARRAY_DIMENSIONS"
22762276
zarr_version = 2
@@ -2675,40 +2675,35 @@ def test_write_persistence_modes(self, group) -> None:
26752675
assert_identical(original, actual)
26762676

26772677
def test_compressor_encoding(self) -> None:
2678+
from numcodecs.blosc import Blosc
2679+
26782680
original = create_test_data()
26792681
# specify a custom compressor
26802682

2681-
if has_zarr_v3 and zarr.config.config["default_zarr_version"] == 3:
2682-
encoding_key = "codecs"
2683+
if has_zarr_v3 and zarr.config.config["default_zarr_format"] == 3:
2684+
encoding_key = "compressors"
26832685
# all parameters need to be explicitly specified in order for the comparison to pass below
26842686
encoding = {
2687+
"serializer": zarr.codecs.BytesCodec(endian="little"),
26852688
encoding_key: (
2686-
zarr.codecs.BytesCodec(endian="little"),
2687-
zarr.codecs.BloscCodec(
2689+
Blosc(
26882690
cname="zstd",
26892691
clevel=3,
26902692
shuffle="shuffle",
26912693
typesize=8,
26922694
blocksize=0,
26932695
),
2694-
)
2696+
),
26952697
}
26962698
else:
2697-
from numcodecs.blosc import Blosc
2698-
2699-
encoding_key = "compressor"
2700-
encoding = {encoding_key: Blosc(cname="zstd", clevel=3, shuffle=2)}
2699+
encoding_key = "compressors" if has_zarr_v3 else "compressor"
2700+
encoding = {encoding_key: (Blosc(cname="zstd", clevel=3, shuffle=2),)}
27012701

27022702
save_kwargs = dict(encoding={"var1": encoding})
27032703

27042704
with self.roundtrip(original, save_kwargs=save_kwargs) as ds:
27052705
enc = ds["var1"].encoding[encoding_key]
2706-
if has_zarr_v3 and zarr.config.config["default_zarr_version"] == 3:
2707-
# TODO: figure out a cleaner way to do this comparison
2708-
codecs = zarr.core.metadata.v3.parse_codecs(enc)
2709-
assert codecs == encoding[encoding_key]
2710-
else:
2711-
assert enc == encoding[encoding_key]
2706+
assert enc == encoding[encoding_key]
27122707

27132708
def test_group(self) -> None:
27142709
original = create_test_data()
@@ -2846,14 +2841,9 @@ def test_check_encoding_is_consistent_after_append(self) -> None:
28462841
import numcodecs
28472842

28482843
encoding_value: Any
2849-
if has_zarr_v3 and zarr.config.config["default_zarr_version"] == 3:
2850-
compressor = zarr.codecs.BloscCodec()
2851-
encoding_key = "codecs"
2852-
encoding_value = [zarr.codecs.BytesCodec(), compressor]
2853-
else:
2854-
compressor = numcodecs.Blosc()
2855-
encoding_key = "compressor"
2856-
encoding_value = compressor
2844+
compressor = numcodecs.Blosc()
2845+
encoding_key = "compressors" if has_zarr_v3 else "compressor"
2846+
encoding_value = compressor
28572847

28582848
encoding = {"da": {encoding_key: encoding_value}}
28592849
ds.to_zarr(store_target, mode="w", encoding=encoding, **self.version_kwargs)
@@ -6092,14 +6082,14 @@ def test_raise_writing_to_nczarr(self, mode) -> None:
60926082

60936083
@requires_netCDF4
60946084
@requires_dask
6095-
@pytest.mark.usefixtures("default_zarr_version")
6085+
@pytest.mark.usefixtures("default_zarr_format")
60966086
def test_pickle_open_mfdataset_dataset():
60976087
with open_example_mfdataset(["bears.nc"]) as ds:
60986088
assert_identical(ds, pickle.loads(pickle.dumps(ds)))
60996089

61006090

61016091
@requires_zarr
6102-
@pytest.mark.usefixtures("default_zarr_version")
6092+
@pytest.mark.usefixtures("default_zarr_format")
61036093
def test_zarr_closing_internal_zip_store():
61046094
store_name = "tmp.zarr.zip"
61056095
original_da = DataArray(np.arange(12).reshape((3, 4)))
@@ -6110,7 +6100,7 @@ def test_zarr_closing_internal_zip_store():
61106100

61116101

61126102
@requires_zarr
6113-
@pytest.mark.usefixtures("default_zarr_version")
6103+
@pytest.mark.usefixtures("default_zarr_format")
61146104
class TestZarrRegionAuto:
61156105
def test_zarr_region_auto_all(self, tmp_path):
61166106
x = np.arange(0, 50, 10)
@@ -6286,7 +6276,7 @@ def test_zarr_region_append(self, tmp_path):
62866276

62876277

62886278
@requires_zarr
6289-
@pytest.mark.usefixtures("default_zarr_version")
6279+
@pytest.mark.usefixtures("default_zarr_format")
62906280
def test_zarr_region(tmp_path):
62916281
x = np.arange(0, 50, 10)
62926282
y = np.arange(0, 20, 2)
@@ -6315,7 +6305,7 @@ def test_zarr_region(tmp_path):
63156305

63166306
@requires_zarr
63176307
@requires_dask
6318-
@pytest.mark.usefixtures("default_zarr_version")
6308+
@pytest.mark.usefixtures("default_zarr_format")
63196309
def test_zarr_region_chunk_partial(tmp_path):
63206310
"""
63216311
Check that writing to partial chunks with `region` fails, assuming `safe_chunks=False`.
@@ -6336,7 +6326,7 @@ def test_zarr_region_chunk_partial(tmp_path):
63366326

63376327
@requires_zarr
63386328
@requires_dask
6339-
@pytest.mark.usefixtures("default_zarr_version")
6329+
@pytest.mark.usefixtures("default_zarr_format")
63406330
def test_zarr_append_chunk_partial(tmp_path):
63416331
t_coords = np.array([np.datetime64("2020-01-01").astype("datetime64[ns]")])
63426332
data = np.ones((10, 10))
@@ -6374,7 +6364,7 @@ def test_zarr_append_chunk_partial(tmp_path):
63746364

63756365
@requires_zarr
63766366
@requires_dask
6377-
@pytest.mark.usefixtures("default_zarr_version")
6367+
@pytest.mark.usefixtures("default_zarr_format")
63786368
def test_zarr_region_chunk_partial_offset(tmp_path):
63796369
# https://github.com/pydata/xarray/pull/8459#issuecomment-1819417545
63806370
store = tmp_path / "foo.zarr"
@@ -6394,7 +6384,7 @@ def test_zarr_region_chunk_partial_offset(tmp_path):
63946384

63956385
@requires_zarr
63966386
@requires_dask
6397-
@pytest.mark.usefixtures("default_zarr_version")
6387+
@pytest.mark.usefixtures("default_zarr_format")
63986388
def test_zarr_safe_chunk_append_dim(tmp_path):
63996389
store = tmp_path / "foo.zarr"
64006390
data = np.ones((20,))
@@ -6445,7 +6435,7 @@ def test_zarr_safe_chunk_append_dim(tmp_path):
64456435

64466436
@requires_zarr
64476437
@requires_dask
6448-
@pytest.mark.usefixtures("default_zarr_version")
6438+
@pytest.mark.usefixtures("default_zarr_format")
64496439
def test_zarr_safe_chunk_region(tmp_path):
64506440
store = tmp_path / "foo.zarr"
64516441

0 commit comments

Comments
 (0)