Skip to content

Commit

Permalink
Fix default fill_value
Browse files Browse the repository at this point in the history
  • Loading branch information
dcherian committed Sep 17, 2024
1 parent 33f20d3 commit 566ec13
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 10 deletions.
7 changes: 0 additions & 7 deletions src/zarr/core/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,12 +250,6 @@ async def _create_v3(
shape = parse_shapelike(shape)
codecs = list(codecs) if codecs is not None else [BytesCodec()]

if fill_value is None:
if dtype == np.dtype("bool"):
fill_value = False
else:
fill_value = 0

if chunk_key_encoding is None:
chunk_key_encoding = ("default", "/")
assert chunk_key_encoding is not None
Expand All @@ -279,7 +273,6 @@ async def _create_v3(
)

array = cls(metadata=metadata, store_path=store_path)

await array._save_metadata(metadata)
return array

Expand Down
3 changes: 3 additions & 0 deletions src/zarr/core/buffer/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,9 @@ def __repr__(self) -> str:

def all_equal(self, other: Any, equal_nan: bool = True) -> bool:
"""Compare to `other` using np.array_equal."""
if other is None:
# Handle None fill_value for Zarr V2
return False
# use array_equal to obtain equal_nan=True functionality
data, other = np.broadcast_arrays(self._data, other)
result = np.array_equal(self._data, other, equal_nan=equal_nan)
Expand Down
2 changes: 1 addition & 1 deletion src/zarr/core/metadata/v3.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ def parse_fill_value(
A scalar instance of `dtype`
"""
if fill_value is None:
return dtype.type(0) # type: ignore[call-overload]
fill_value = 0
if isinstance(fill_value, Sequence) and not isinstance(fill_value, str):
if dtype.type in (np.complex64, np.complex128):
dtype = cast(COMPLEX_DTYPE, dtype)
Expand Down
6 changes: 4 additions & 2 deletions src/zarr/testing/strategies.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ def arrays(
path = draw(paths)
name = draw(array_names)
attributes = draw(attrs)
fill_value = draw(npst.from_dtype(nparray.dtype))
# test that None works too.
fill_value = draw(st.one_of([st.none(), npst.from_dtype(nparray.dtype)]))
# compressor = draw(compressors)

expected_attrs = {} if attributes is None else attributes
Expand All @@ -88,11 +89,12 @@ def arrays(
chunks=chunks,
dtype=nparray.dtype,
attributes=attributes,
# compressor=compressor, # TODO: FIXME
# compressor=compressor, # FIXME
fill_value=fill_value,
)

assert isinstance(a, Array)
assert a.fill_value is not None
assert isinstance(root[array_path], Array)
assert nparray.shape == a.shape
assert chunks == a.chunks
Expand Down

0 comments on commit 566ec13

Please sign in to comment.