Skip to content

Commit ff530c3

Browse files
committed
restore v2 behavior
1 parent 13be49c commit ff530c3

File tree

3 files changed

+26
-8
lines changed

3 files changed

+26
-8
lines changed

src/zarr/codecs/pipeline.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from zarr.core.common import ChunkCoords, concurrent_map
1818
from zarr.core.config import config
1919
from zarr.core.indexing import SelectorTuple, is_scalar, is_total_slice
20-
from zarr.core.metadata.v2 import default_fill_value
20+
from zarr.core.metadata.v2 import _default_fill_value
2121
from zarr.registry import register_pipeline
2222

2323
if TYPE_CHECKING:
@@ -256,7 +256,7 @@ async def read_batch(
256256
# validated when decoding the metadata, but we support reading
257257
# Zarr V2 data and need to support the case where fill_value
258258
# is None.
259-
fill_value = default_fill_value(dtype=chunk_spec.dtype)
259+
fill_value = _default_fill_value(dtype=chunk_spec.dtype)
260260

261261
out[out_selection] = fill_value
262262
else:
@@ -287,7 +287,7 @@ async def read_batch(
287287
else:
288288
fill_value = chunk_spec.fill_value
289289
if fill_value is None:
290-
fill_value = default_fill_value(dtype=chunk_spec.dtype)
290+
fill_value = _default_fill_value(dtype=chunk_spec.dtype)
291291
out[out_selection] = fill_value
292292

293293
def _merge_chunk_array(

src/zarr/core/metadata/v2.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ def parse_fill_value(fill_value: object, dtype: np.dtype[Any]) -> Any:
302302
return fill_value
303303

304304

305-
def default_fill_value(dtype: np.dtype[Any]) -> Any:
305+
def _default_fill_value(dtype: np.dtype[Any]) -> Any:
306306
"""
307307
Get the default fill value for a type.
308308
@@ -315,4 +315,9 @@ def default_fill_value(dtype: np.dtype[Any]) -> Any:
315315
This is useful for reading Zarr V2 arrays, which allow the fill
316316
value to be unspecified.
317317
"""
318-
return dtype.type(0)
318+
if dtype.kind == "S":
319+
return b""
320+
elif dtype.kind == "U":
321+
return ""
322+
else:
323+
return dtype.type(0)

tests/v3/test_v2.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import json
22
from collections.abc import Iterator
3+
from typing import Any
34

45
import numpy as np
56
import pytest
@@ -35,12 +36,24 @@ def test_simple(store: StorePath) -> None:
3536
assert np.array_equal(data, a[:, :])
3637

3738

38-
def test_implicit_fill_value(store: StorePath) -> None:
39-
arr = zarr.open_array(store=store, shape=(4,), fill_value=None, zarr_format=2)
39+
@pytest.mark.parametrize(
40+
("dtype", "fill_value"),
41+
[
42+
("bool", False),
43+
("int64", 0),
44+
("float64", 0.0),
45+
("|S1", b""),
46+
("|U1", ""),
47+
("object", 0),
48+
(str, ""),
49+
],
50+
)
51+
def test_implicit_fill_value(store: StorePath, dtype: str, fill_value: Any) -> None:
52+
arr = zarr.open_array(store=store, shape=(4,), fill_value=None, zarr_format=2, dtype=dtype)
4053
assert arr.metadata.fill_value is None
4154
assert arr.metadata.to_dict()["fill_value"] is None
4255
result = arr[:]
43-
expected = np.zeros(arr.shape, dtype=arr.dtype)
56+
expected = np.full(arr.shape, fill_value, dtype=dtype)
4457
np.testing.assert_array_equal(result, expected)
4558

4659

0 commit comments

Comments
 (0)