Skip to content

Commit d1075de

Browse files
Zarr.save argument check (#2446)
* add argument check * test * test zarr.save with multiple arrays * fix path for zarr.save args * improve test readability * fix typing * support other array types Co-authored-by: Tom Augspurger <[email protected]> * support NDArrayLike * fix typing * fix typing * fix typing * format --------- Co-authored-by: Tom Augspurger <[email protected]>
1 parent f092351 commit d1075de

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

src/zarr/api/asynchronous.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
from zarr.abc.store import Store
1212
from zarr.core.array import Array, AsyncArray, get_array_metadata
13+
from zarr.core.buffer import NDArrayLike
1314
from zarr.core.common import (
1415
JSON,
1516
AccessModeLiteral,
@@ -31,7 +32,6 @@
3132
from collections.abc import Iterable
3233

3334
from zarr.abc.codec import Codec
34-
from zarr.core.buffer import NDArrayLike
3535
from zarr.core.chunk_key_encodings import ChunkKeyEncoding
3636

3737
# TODO: this type could use some more thought
@@ -393,6 +393,8 @@ async def save_array(
393393
_handle_zarr_version_or_format(zarr_version=zarr_version, zarr_format=zarr_format)
394394
or _default_zarr_version()
395395
)
396+
if not isinstance(arr, NDArrayLike):
397+
raise TypeError("arr argument must be numpy or other NDArrayLike array")
396398

397399
mode = kwargs.pop("mode", None)
398400
store_path = await make_store_path(store, path=path, mode=mode, storage_options=storage_options)
@@ -447,16 +449,26 @@ async def save_group(
447449
or _default_zarr_version()
448450
)
449451

452+
for arg in args:
453+
if not isinstance(arg, NDArrayLike):
454+
raise TypeError(
455+
"All arguments must be numpy or other NDArrayLike arrays (except store, path, storage_options, and zarr_format)"
456+
)
457+
for k, v in kwargs.items():
458+
if not isinstance(v, NDArrayLike):
459+
raise TypeError(f"Keyword argument '{k}' must be a numpy or other NDArrayLike array")
460+
450461
if len(args) == 0 and len(kwargs) == 0:
451462
raise ValueError("at least one array must be provided")
452463
aws = []
453464
for i, arr in enumerate(args):
465+
_path = f"{path}/arr_{i}" if path is not None else f"arr_{i}"
454466
aws.append(
455467
save_array(
456468
store,
457469
arr,
458470
zarr_format=zarr_format,
459-
path=f"{path}/arr_{i}",
471+
path=_path,
460472
storage_options=storage_options,
461473
)
462474
)

tests/test_api.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,33 @@ async def test_open_group_unspecified_version(
132132
assert g2.metadata.zarr_format == zarr_format
133133

134134

135+
@pytest.mark.parametrize("store", ["local", "memory", "zip"], indirect=["store"])
136+
@pytest.mark.parametrize("n_args", [10, 1, 0])
137+
@pytest.mark.parametrize("n_kwargs", [10, 1, 0])
138+
def test_save(store: Store, n_args: int, n_kwargs: int) -> None:
139+
data = np.arange(10)
140+
args = [np.arange(10) for _ in range(n_args)]
141+
kwargs = {f"arg_{i}": data for i in range(n_kwargs)}
142+
143+
if n_kwargs == 0 and n_args == 0:
144+
with pytest.raises(ValueError):
145+
save(store)
146+
elif n_args == 1 and n_kwargs == 0:
147+
save(store, *args)
148+
array = open(store)
149+
assert isinstance(array, Array)
150+
assert_array_equal(array[:], data)
151+
else:
152+
save(store, *args, **kwargs) # type: ignore[arg-type]
153+
group = open(store)
154+
assert isinstance(group, Group)
155+
for array in group.array_values():
156+
assert_array_equal(array[:], data)
157+
for k in kwargs.keys():
158+
assert k in group
159+
assert group.nmembers() == n_args + n_kwargs
160+
161+
135162
def test_save_errors() -> None:
136163
with pytest.raises(ValueError):
137164
# no arrays provided
@@ -142,6 +169,10 @@ def test_save_errors() -> None:
142169
with pytest.raises(ValueError):
143170
# no arrays provided
144171
save("data/group.zarr")
172+
with pytest.raises(TypeError):
173+
# mode is no valid argument and would get handled as an array
174+
a = np.arange(10)
175+
zarr.save("data/example.zarr", a, mode="w")
145176

146177

147178
def test_open_with_mode_r(tmp_path: pathlib.Path) -> None:

0 commit comments

Comments
 (0)