Skip to content

Commit 4aeac75

Browse files
authored
fix(array): reject unknown keys in ArrayConfig.from_dict (#4414)
* fix(array): reject unknown keys in ArrayConfig.from_dict from_dict read only the keys it models and silently ignored the rest, so create_array(config={"nope": 1}) succeeded and a misspelled key such as "write_empty_chunk" had no effect. with_config, which builds the config with ArrayConfig(**...), already raised for the same key. Raise a TypeError naming the unknown keys and the valid ones. Assisted-by: ClaudeCode:claude-opus-5-5 * docs(changes): add fragment for #4414 Assisted-by: ClaudeCode:claude-opus-5-5
1 parent f58644a commit 4aeac75

3 files changed

Lines changed: 54 additions & 2 deletions

File tree

‎changes/4414.bugfix.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
An array `config` with a key that is not a runtime configuration option (for example a misspelled `write_empty_chunk`) now raises a `TypeError` naming the unknown and valid keys. Previously `create_array`, `create`, `open_array` and other functions silently ignored such keys.

‎src/zarr/core/array_spec.py‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,15 @@ def from_dict(cls, data: ArrayConfigParams) -> Self:
9292
"""
9393
Create an ArrayConfig from a dict. The keys of that dict are a subset of the
9494
attributes of the ArrayConfig class. Any keys missing from that dict will be set to the
95-
the values in the ``array`` namespace of ``zarr.config``.
95+
the values in the ``array`` namespace of ``zarr.config``. Keys that are not
96+
attributes of the ArrayConfig class raise a `TypeError`.
9697
"""
98+
unknown = set(data) - {f.name for f in fields(ArrayConfig)}
99+
if unknown:
100+
raise TypeError(
101+
f"Unknown array config keys: {sorted(unknown)}. "
102+
f"Valid keys are {[f.name for f in fields(ArrayConfig)]}."
103+
)
97104
kwargs_out: ArrayConfigParams = {}
98105
for f in fields(ArrayConfig):
99106
field_name = cast(

‎tests/test_array_spec.py‎

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
import numpy as np
66
import pytest
77

8-
from zarr.core.array_spec import ArrayConfig, ArraySpec
8+
import zarr
9+
from zarr.core.array_spec import ArrayConfig, ArrayConfigParams, ArraySpec
910
from zarr.core.buffer import BufferPrototype, default_buffer_prototype
1011
from zarr.core.buffer.cpu import NDBuffer
1112
from zarr.core.dtype import get_data_type_from_native_dtype
@@ -177,3 +178,46 @@ def test_signed_zero_fills_are_distinct(native_dtype: Any, neg_fill: Any, pos_fi
177178
def test_unequal_with_invalid_type(obj: Any) -> None:
178179
assert (_make_spec() == obj) is False
179180
assert _make_spec() != obj
181+
182+
183+
@pytest.mark.parametrize(
184+
"data",
185+
[
186+
{},
187+
{"order": "F"},
188+
{"write_empty_chunks": True, "read_missing_chunks": False},
189+
{
190+
"order": "F",
191+
"write_empty_chunks": True,
192+
"read_missing_chunks": False,
193+
"sharding_coalesce_max_gap_bytes": 1,
194+
"sharding_coalesce_max_bytes": 2,
195+
},
196+
],
197+
)
198+
def test_array_config_from_dict(data: ArrayConfigParams) -> None:
199+
"""
200+
ArrayConfig.from_dict takes the given keys from `data` and the rest from the global
201+
configuration.
202+
"""
203+
observed = ArrayConfig.from_dict(data).to_dict()
204+
for key, value in observed.items():
205+
assert value == data.get(key, zarr.config.get(f"array.{key}"))
206+
207+
208+
@pytest.mark.parametrize(
209+
"build",
210+
[
211+
lambda config: ArrayConfig.from_dict(config),
212+
lambda config: zarr.create_array({}, shape=(4,), dtype="uint8", config=config),
213+
lambda config: zarr.create(shape=(4,), dtype="uint8", config=config),
214+
],
215+
ids=["from_dict", "create_array", "create"],
216+
)
217+
def test_array_config_unknown_key(build: Callable[[Any], object]) -> None:
218+
"""
219+
An array config with a key that is not an ArrayConfig attribute raises TypeError instead of
220+
being ignored.
221+
"""
222+
with pytest.raises(TypeError, match=r"Unknown array config keys: \['nope'\]"):
223+
build({"order": "C", "nope": 1})

0 commit comments

Comments
 (0)