Skip to content

Commit

Permalink
error if shape / dtype and data are provided
Browse files Browse the repository at this point in the history
  • Loading branch information
d-v-b committed Jan 29, 2025
1 parent b21db55 commit 7b30744
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 14 deletions.
22 changes: 11 additions & 11 deletions src/zarr/core/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -4373,29 +4373,29 @@ def _parse_data_params(
"Either provide a value for data, or specify shape."
)
raise ValueError(msg)
shape_out = shape
if dtype is None:
msg = (
"The data parameter was set to None, but dtype was not specified."
"Either provide an array-like value for data, or specify dtype."
)
raise ValueError(msg)

dtype_out = dtype
else:
if shape is not None:
msg = (
"The data parameter was used, but the shape parameter was also "
"used. The shape parameter will be ignored, and the data.shape "
"attribute will be used instead."
"used. This is an error. Either use the data parameter, or the shape parameter, "
"but not both."
)
warnings.warn(msg, UserWarning, stacklevel=2)
shape = data.shape

raise ValueError(msg)
shape_out = data.shape
if dtype is not None:
msg = (
"The data parameter was used, but the dtype parameter was also "
"used. The dtype parameter will be ignored, and the data.dtype "
"attribute will be used instead."
"used. This is an error. Either use the data parameter, or the dtype parameter, "
"but not both."
)
warnings.warn(msg, UserWarning, stacklevel=2)
dtype = data.dtype
return data, shape, dtype # type: ignore[return-value]
raise ValueError(msg)
dtype_out = data.dtype
return data, shape_out, dtype_out
12 changes: 9 additions & 3 deletions tests/test_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -1306,14 +1306,20 @@ async def test_create_array_data_ignored_params(store: Store) -> None:
Test that specify data AND shape AND dtype results in a warning
"""
data = np.arange(10)
with pytest.warns(UserWarning, match="The shape parameter will be ignored"):
with pytest.raises(
ValueError, match="The data parameter was used, but the shape parameter was also used."
):
await create_array(store, data=data, shape=data.shape, dtype=None, overwrite=True)

# we catch shape first, so specifying a dtype should raise the same warning as before
with pytest.warns(UserWarning, match="The shape parameter will be ignored"):
with pytest.raises(
ValueError, match="The data parameter was used, but the shape parameter was also used."
):
await create_array(store, data=data, shape=data.shape, dtype=data.dtype, overwrite=True)

with pytest.warns(UserWarning, match="The dtype parameter will be ignored"):
with pytest.raises(
ValueError, match="The data parameter was used, but the dtype parameter was also used."
):
await create_array(store, data=data, shape=None, dtype=data.dtype, overwrite=True)


Expand Down

0 comments on commit 7b30744

Please sign in to comment.