diff --git a/changes/4554.misc.md b/changes/4554.misc.md new file mode 100644 index 0000000000..a5a961b1c1 --- /dev/null +++ b/changes/4554.misc.md @@ -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. diff --git a/src/zarr/api/asynchronous.py b/src/zarr/api/asynchronous.py index 476276fd82..8be7d32f7d 100644 --- a/src/zarr/api/asynchronous.py +++ b/src/zarr/api/asynchronous.py @@ -548,8 +548,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) diff --git a/tests/test_api.py b/tests/test_api.py index 1d0c8f6498..e162ddfeea 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -958,6 +958,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`.