Skip to content

Commit c552282

Browse files
authored
Fix zarr.save for given path and multiple args arrays (#3127)
* test and fix path argument in save_group * add tests for load_array * Revert "add tests for load_array" partially This reverts commit 2d0f6e9 partially. * document changes
1 parent d53d368 commit c552282

File tree

3 files changed

+12
-10
lines changed

3 files changed

+12
-10
lines changed

changes/3127.bugfix.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
When `zarr.save` has an argument `path=some/path/` and multiple arrays in `args`, the path resulted in `some/path/some/path` due to using the `path`
2+
argument twice while building the array path. This is now fixed.

src/zarr/api/asynchronous.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -505,13 +505,12 @@ async def save_group(
505505
raise ValueError("at least one array must be provided")
506506
aws = []
507507
for i, arr in enumerate(args):
508-
_path = f"{path}/arr_{i}" if path is not None else f"arr_{i}"
509508
aws.append(
510509
save_array(
511510
store_path,
512511
arr,
513512
zarr_format=zarr_format,
514-
path=_path,
513+
path=f"arr_{i}",
515514
storage_options=storage_options,
516515
)
517516
)

tests/test_api.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -229,22 +229,23 @@ async def test_open_group_unspecified_version(
229229
@pytest.mark.parametrize("store", ["local", "memory", "zip"], indirect=["store"])
230230
@pytest.mark.parametrize("n_args", [10, 1, 0])
231231
@pytest.mark.parametrize("n_kwargs", [10, 1, 0])
232-
def test_save(store: Store, n_args: int, n_kwargs: int) -> None:
232+
@pytest.mark.parametrize("path", [None, "some_path"])
233+
def test_save(store: Store, n_args: int, n_kwargs: int, path: None | str) -> None:
233234
data = np.arange(10)
234235
args = [np.arange(10) for _ in range(n_args)]
235236
kwargs = {f"arg_{i}": data for i in range(n_kwargs)}
236237

237238
if n_kwargs == 0 and n_args == 0:
238239
with pytest.raises(ValueError):
239-
save(store)
240+
save(store, path=path)
240241
elif n_args == 1 and n_kwargs == 0:
241-
save(store, *args)
242-
array = zarr.api.synchronous.open(store)
242+
save(store, *args, path=path)
243+
array = zarr.api.synchronous.open(store, path=path)
243244
assert isinstance(array, Array)
244245
assert_array_equal(array[:], data)
245246
else:
246-
save(store, *args, **kwargs) # type: ignore [arg-type]
247-
group = zarr.api.synchronous.open(store)
247+
save(store, *args, path=path, **kwargs) # type: ignore [arg-type]
248+
group = zarr.api.synchronous.open(store, path=path)
248249
assert isinstance(group, Group)
249250
for array in group.array_values():
250251
assert_array_equal(array[:], data)
@@ -384,8 +385,8 @@ def test_array_order_warns(order: MemoryOrder | None, zarr_format: ZarrFormat) -
384385
# assert "LazyLoader: " in repr(loader)
385386

386387

387-
def test_load_array(memory_store: Store) -> None:
388-
store = memory_store
388+
def test_load_array(sync_store: Store) -> None:
389+
store = sync_store
389390
foo = np.arange(100)
390391
bar = np.arange(100, 0, -1)
391392
save(store, foo=foo, bar=bar)

0 commit comments

Comments
 (0)