Skip to content

Commit d2c7838

Browse files
committed
add warnings when write_empty_chunks is used as a kwarg
1 parent 3cfface commit d2c7838

File tree

3 files changed

+37
-14
lines changed

3 files changed

+37
-14
lines changed

src/zarr/api/asynchronous.py

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
ChunkCoords,
1717
MemoryOrder,
1818
ZarrFormat,
19+
_warn_write_empty_chunks_kwarg,
1920
)
2021
from zarr.core.config import config
2122
from zarr.core.group import AsyncGroup, ConsolidatedMetadata, GroupMetadata
@@ -724,7 +725,6 @@ async def create(
724725
read_only: bool | None = None,
725726
object_codec: Codec | None = None, # TODO: type has changed
726727
dimension_separator: Literal[".", "/"] | None = None,
727-
write_empty_chunks: bool = False, # TODO: default has changed
728728
zarr_version: ZarrFormat | None = None, # deprecated
729729
zarr_format: ZarrFormat | None = None,
730730
meta_array: Any | None = None, # TODO: need type
@@ -794,17 +794,6 @@ async def create(
794794
795795
.. versionadded:: 2.8
796796
797-
write_empty_chunks : bool, optional
798-
If True (default), all chunks will be stored regardless of their
799-
contents. If False, each chunk is compared to the array's fill value
800-
prior to storing. If a chunk is uniformly equal to the fill value, then
801-
that chunk is not be stored, and the store entry for that chunk's key
802-
is deleted. This setting enables sparser storage, as only chunks with
803-
non-fill-value data are stored, at the expense of overhead associated
804-
with checking the data of each chunk.
805-
806-
.. versionadded:: 2.11
807-
808797
zarr_format : {2, 3, None}, optional
809798
The zarr format to use when saving.
810799
meta_array : array-like, optional
@@ -856,8 +845,12 @@ async def create(
856845
RuntimeWarning,
857846
stacklevel=2,
858847
)
859-
if write_empty_chunks:
860-
warnings.warn("write_empty_chunks is not yet implemented", RuntimeWarning, stacklevel=2)
848+
849+
if "write_empty_chunks" in kwargs:
850+
# warn users if the write_empty_chunks kwarg was used
851+
write_empty_chunks = kwargs.pop("write_empty_chunks")
852+
_warn_write_empty_chunks_kwarg(write_empty_chunks)
853+
861854
if meta_array is not None:
862855
warnings.warn("meta_array is not yet implemented", RuntimeWarning, stacklevel=2)
863856

@@ -1058,6 +1051,11 @@ async def open_array(
10581051

10591052
zarr_format = _handle_zarr_version_or_format(zarr_version=zarr_version, zarr_format=zarr_format)
10601053

1054+
if "write_empty_chunks" in kwargs:
1055+
# warn users if the write_empty_chunks kwarg was used
1056+
write_empty_chunks = kwargs.pop("write_empty_chunks")
1057+
_warn_write_empty_chunks_kwarg(write_empty_chunks)
1058+
10611059
try:
10621060
return await AsyncArray.open(store_path, zarr_format=zarr_format)
10631061
except FileNotFoundError:

src/zarr/core/common.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import asyncio
44
import functools
55
import operator
6+
import warnings
67
from collections.abc import Iterable, Mapping
78
from enum import Enum
89
from itertools import starmap
@@ -166,3 +167,12 @@ def parse_dtype(dtype: Any, zarr_format: ZarrFormat) -> np.dtype[Any]:
166167
else:
167168
return _STRING_DTYPE
168169
return np.dtype(dtype)
170+
171+
172+
def _warn_write_empty_chunks_kwarg(write_empty_chunks: bool) -> None:
173+
msg = (
174+
f"The `write_empty_chunks` keyword argument was provided to this function with a value of {write_empty_chunks}."
175+
"This keyword argument has no effect. To control whether empty chunks are written to"
176+
" storage, change the 'array.write_empty_chunks' configuration variable."
177+
)
178+
warnings.warn(msg, RuntimeWarning, stacklevel=2)

tests/test_api.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,21 @@ def test_create_array(memory_store: Store) -> None:
4848
assert z.chunks == (40,)
4949

5050

51+
@pytest.mark.parametrize("write_empty_chunks", [True, False])
52+
def test_write_empty_chunks_warns(write_empty_chunks: bool) -> None:
53+
"""
54+
Test that using the `write_empty_chunks` kwarg on array access will raise a warning.
55+
"""
56+
match = f"The `write_empty_chunks` keyword argument was provided to this function with a value of {write_empty_chunks}."
57+
with pytest.warns(RuntimeWarning, match=match):
58+
_ = zarr.array(
59+
data=np.arange(10), shape=(10,), dtype="uint8", write_empty_chunks=write_empty_chunks
60+
)
61+
62+
with pytest.warns(RuntimeWarning, match=match):
63+
_ = zarr.create(shape=(10,), dtype="uint8", write_empty_chunks=write_empty_chunks)
64+
65+
5166
@pytest.mark.parametrize("path", ["foo", "/", "/foo", "///foo/bar"])
5267
@pytest.mark.parametrize("node_type", ["array", "group"])
5368
def test_open_normalized_path(

0 commit comments

Comments
 (0)