Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changes/4554.misc.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Removed an unreachable scalar-conversion branch from `zarr.save_array`. Scalars are already rejected by the `NDArrayLike` check that precedes it, and mypy began reporting the branch as unreachable once numpy 2.5.4 annotated `numpy.isscalar` with `TypeIs`. A test now pins the `TypeError` that `zarr.save_array` raises for a scalar.
2 changes: 0 additions & 2 deletions src/zarr/api/asynchronous.py
Original file line number Diff line number Diff line change
Expand Up @@ -533,8 +533,6 @@ async def save_array(

mode = kwargs.pop("mode", "a")
store_path = await make_store_path(store, path=path, mode=mode, storage_options=storage_options)
if np.isscalar(arr):
arr = np.array(arr)
shape = arr.shape
chunks = getattr(arr, "chunks", None) # for array-likes with chunks attribute
overwrite = kwargs.pop("overwrite", None) or _infer_overwrite(mode)
Expand Down
6 changes: 6 additions & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -844,6 +844,12 @@ def test_save_errors() -> None:
zarr.save("data/example.zarr", a, mode="w")


def test_save_array_rejects_scalar() -> None:
# numpy scalars lack __len__ and __setitem__, so they fail the NDArrayLike check
with pytest.raises(TypeError, match="arr argument must be numpy or other NDArrayLike array"):
save_array("data/example.zarr", np.float64(1.0)) # type: ignore[arg-type]


def test_save_storage_options_is_not_an_array(tmp_path: Path) -> None:
# `storage_options` is an option of `save`, not one of the arrays to save, so a
# single array still lands as an array and not as a group holding `arr_0`.
Expand Down
Loading