Skip to content

Commit 041bdf4

Browse files
vup903claude
andcommitted
Encapsulate convert + field-context error into parse_field (zarr-developers#3285)
Address review feedback: factor the repeated convert-then-re-raise pattern out of each per-field parser. convert now raises a field-agnostic ValueError("Expected instance of TYPE, got DATA"); the new parse_field wraps it and re-raises with field context ("Failed to parse input for FIELD") using the caller's chosen exception type, chaining the original error. Every per-field parser collapses to a one-liner. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
1 parent e29d0a0 commit 041bdf4

13 files changed

Lines changed: 91 additions & 95 deletions

File tree

‎src/zarr/codecs/blosc.py‎

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,9 @@ class BloscCname(metaclass=_DeprecatedStrEnumMeta):
104104

105105

106106
def parse_typesize(data: JSON) -> int:
107-
from zarr.core.json_parse import convert
107+
from zarr.core.json_parse import parse_field
108108

109-
try:
110-
parsed: int = convert(data, int)
111-
except (ValueError, TypeError) as exc:
112-
raise TypeError(f"Value must be an int. Got {type(data)} instead.") from exc
109+
parsed: int = parse_field(data, int, "typesize", error=TypeError)
113110
if parsed > 0:
114111
return parsed
115112
else:
@@ -120,22 +117,16 @@ def parse_typesize(data: JSON) -> int:
120117

121118
# todo: real validation
122119
def parse_clevel(data: JSON) -> int:
123-
from zarr.core.json_parse import convert
120+
from zarr.core.json_parse import parse_field
124121

125-
try:
126-
parsed: int = convert(data, int)
127-
except (ValueError, TypeError) as exc:
128-
raise TypeError(f"Value should be an int. Got {type(data)} instead.") from exc
122+
parsed: int = parse_field(data, int, "clevel", error=TypeError)
129123
return parsed
130124

131125

132126
def parse_blocksize(data: JSON) -> int:
133-
from zarr.core.json_parse import convert
127+
from zarr.core.json_parse import parse_field
134128

135-
try:
136-
parsed: int = convert(data, int)
137-
except (ValueError, TypeError) as exc:
138-
raise TypeError(f"Value should be an int. Got {type(data)} instead.") from exc
129+
parsed: int = parse_field(data, int, "blocksize", error=TypeError)
139130
return parsed
140131

141132

‎src/zarr/codecs/gzip.py‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,9 @@
1919

2020

2121
def parse_gzip_level(data: JSON) -> int:
22-
from zarr.core.json_parse import convert
22+
from zarr.core.json_parse import parse_field
2323

24-
try:
25-
parsed: int = convert(data, int)
26-
except (ValueError, TypeError) as exc:
27-
raise TypeError(f"Expected int, got {type(data)}") from exc
24+
parsed: int = parse_field(data, int, "level", error=TypeError)
2825
if parsed not in range(10):
2926
raise ValueError(
3027
f"Expected an integer from the inclusive range (0, 9). Got {parsed} instead."

‎src/zarr/codecs/zstd.py‎

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,24 +21,18 @@
2121

2222

2323
def parse_zstd_level(data: JSON) -> int:
24-
from zarr.core.json_parse import convert
24+
from zarr.core.json_parse import parse_field
2525

26-
try:
27-
parsed: int = convert(data, int)
28-
except (ValueError, TypeError) as exc:
29-
raise TypeError(f"Got value with type {type(data)}, but expected an int.") from exc
26+
parsed: int = parse_field(data, int, "level", error=TypeError)
3027
if parsed >= 23:
3128
raise ValueError(f"Value must be less than or equal to 22. Got {parsed} instead.")
3229
return parsed
3330

3431

3532
def parse_checksum(data: JSON) -> bool:
36-
from zarr.core.json_parse import convert
33+
from zarr.core.json_parse import parse_field
3734

38-
try:
39-
parsed: bool = convert(data, bool)
40-
except (ValueError, TypeError) as exc:
41-
raise TypeError(f"Expected bool. Got {type(data)}.") from exc
35+
parsed: bool = parse_field(data, bool, "checksum", error=TypeError)
4236
return parsed
4337

4438

‎src/zarr/core/chunk_key_encodings.py‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,9 @@
1919

2020

2121
def parse_separator(data: JSON) -> SeparatorLiteral:
22-
from zarr.core.json_parse import convert
22+
from zarr.core.json_parse import parse_field
2323

24-
try:
25-
return cast("SeparatorLiteral", convert(data, Literal[".", "/"]))
26-
except (ValueError, TypeError) as exc:
27-
raise ValueError(f"Expected an '.' or '/' separator. Got {data} instead.") from exc
24+
return cast("SeparatorLiteral", parse_field(data, Literal[".", "/"], "separator"))
2825

2926

3027
class ChunkKeyEncodingParams(TypedDict):

‎src/zarr/core/common.py‎

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -207,21 +207,15 @@ def parse_fill_value(data: Any) -> Any:
207207

208208

209209
def parse_order(data: Any) -> Literal["C", "F"]:
210-
from zarr.core.json_parse import convert
210+
from zarr.core.json_parse import parse_field
211211

212-
try:
213-
return cast("Literal['C', 'F']", convert(data, Literal["C", "F"]))
214-
except TypeError as exc:
215-
raise ValueError(f"Expected one of ('C', 'F'), got {data!r} instead.") from exc
212+
return cast("Literal['C', 'F']", parse_field(data, Literal["C", "F"], "order"))
216213

217214

218215
def parse_bool(data: Any) -> bool:
219216
from zarr.core.json_parse import convert
220217

221-
try:
222-
return cast("bool", convert(data, bool))
223-
except TypeError as exc:
224-
raise ValueError(f"Expected bool, got {data!r} instead.") from exc
218+
return cast("bool", convert(data, bool))
225219

226220

227221
def parse_int(data: Any) -> int:

‎src/zarr/core/config.py‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,6 @@ def enable_gpu(self) -> ConfigSet:
151151

152152

153153
def parse_indexing_order(data: Any) -> Literal["C", "F"]:
154-
from zarr.core.json_parse import convert
154+
from zarr.core.json_parse import parse_field
155155

156-
try:
157-
return cast("Literal['C', 'F']", convert(data, Literal["C", "F"]))
158-
except TypeError as exc:
159-
raise ValueError(f"Expected one of ('C', 'F'), got {data!r} instead.") from exc
156+
return cast("Literal['C', 'F']", parse_field(data, Literal["C", "F"], "order"))

‎src/zarr/core/group.py‎

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -85,24 +85,19 @@
8585

8686
def parse_zarr_format(data: Any) -> ZarrFormat:
8787
"""Parse the zarr_format field from metadata."""
88-
from zarr.core.json_parse import convert
88+
from zarr.core.json_parse import parse_field
8989

90-
try:
91-
return cast("ZarrFormat", convert(data, Literal[2, 3]))
92-
except (ValueError, TypeError) as exc:
93-
msg = f"Invalid zarr_format. Expected one of 2 or 3. Got {data}."
94-
raise ValueError(msg) from exc
90+
return cast("ZarrFormat", parse_field(data, Literal[2, 3], "zarr_format"))
9591

9692

9793
def parse_node_type(data: Any) -> NodeType:
9894
"""Parse the node_type field from metadata."""
99-
from zarr.core.json_parse import convert
95+
from zarr.core.json_parse import parse_field
10096

101-
try:
102-
return cast("Literal['array', 'group']", convert(data, Literal["array", "group"]))
103-
except (ValueError, TypeError) as exc:
104-
msg = f"Invalid value for 'node_type'. Expected 'array' or 'group'. Got '{data}'."
105-
raise MetadataValidationError(msg) from exc
97+
return cast(
98+
"Literal['array', 'group']",
99+
parse_field(data, Literal["array", "group"], "node_type", error=MetadataValidationError),
100+
)
106101

107102

108103
# todo: convert None to empty dict

‎src/zarr/core/json_parse.py‎

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,24 +26,43 @@
2626
if TYPE_CHECKING:
2727
from zarr.core.common import JSON
2828

29-
__all__ = ["MAX_JSON_DEPTH", "convert", "validate_json_value"]
29+
__all__ = ["MAX_JSON_DEPTH", "convert", "parse_field", "validate_json_value"]
3030

3131
MAX_JSON_DEPTH: Final = 64
3232
"""Maximum nesting depth accepted by :func:`validate_json_value`."""
3333

3434

35+
def _type_name(type_: Any) -> str:
36+
return getattr(type_, "__name__", None) or str(type_).replace("typing.", "")
37+
38+
3539
def convert(value: object, type_: Any, *, strict: bool = True) -> Any:
3640
"""Validate and coerce ``value`` against ``type_`` via :func:`msgspec.convert`.
3741
38-
msgspec handles the JSON-shaped coercions Zarr needs but raises
39-
:class:`msgspec.ValidationError` on a mismatch. We translate that to
40-
``TypeError`` so callers can keep raising the exception types the rest of
41-
Zarr already expects.
42+
On a mismatch msgspec raises :class:`msgspec.ValidationError`; this re-raises
43+
a plain, field-agnostic ``ValueError`` naming the expected type, so callers
44+
can add their own field context (see :func:`parse_field`).
4245
"""
4346
try:
4447
return msgspec.convert(value, type_, strict=strict)
4548
except msgspec.ValidationError as exc:
46-
raise TypeError(str(exc)) from exc
49+
raise ValueError(f"Expected instance of {_type_name(type_)}, got {value!r}.") from exc
50+
51+
52+
def parse_field(
53+
data: object, type_: Any, field: str, *, error: type[Exception] = ValueError
54+
) -> Any:
55+
"""Validate ``data`` for metadata field ``field`` against ``type_``.
56+
57+
Wraps :func:`convert` and, on failure, re-raises ``error`` with field
58+
context, chaining the underlying type error. This keeps the
59+
``convert``-then-re-raise pattern in one place rather than repeating it in
60+
every per-field parser.
61+
"""
62+
try:
63+
return convert(data, type_)
64+
except ValueError as exc:
65+
raise error(f"Failed to parse input for {field!r}.") from exc
4766

4867

4968
def validate_json_value(value: object, *, max_depth: int = MAX_JSON_DEPTH, _depth: int = 0) -> JSON:

‎src/zarr/core/metadata/v2.py‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -280,12 +280,9 @@ def parse_dtype(data: npt.DTypeLike) -> np.dtype[Any]:
280280
def parse_zarr_format(data: object) -> Literal[2]:
281281
from typing import Literal
282282

283-
from zarr.core.json_parse import convert
283+
from zarr.core.json_parse import parse_field
284284

285-
try:
286-
return cast("Literal[2]", convert(data, Literal[2]))
287-
except (ValueError, TypeError) as exc:
288-
raise ValueError(f"Invalid value. Expected 2. Got {data}.") from exc
285+
return cast("Literal[2]", parse_field(data, Literal[2], "zarr_format"))
289286

290287

291288
def parse_filters(data: object) -> tuple[Numcodec, ...] | None:

‎src/zarr/core/metadata/v3.py‎

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -47,23 +47,20 @@
4747

4848

4949
def parse_zarr_format(data: object) -> Literal[3]:
50-
from zarr.core.json_parse import convert
50+
from zarr.core.json_parse import parse_field
5151

52-
try:
53-
return cast("Literal[3]", convert(data, Literal[3]))
54-
except (ValueError, TypeError) as exc:
55-
msg = f"Invalid value for 'zarr_format'. Expected '3'. Got '{data}'."
56-
raise MetadataValidationError(msg) from exc
52+
return cast(
53+
"Literal[3]", parse_field(data, Literal[3], "zarr_format", error=MetadataValidationError)
54+
)
5755

5856

5957
def parse_node_type_array(data: object) -> Literal["array"]:
60-
from zarr.core.json_parse import convert
58+
from zarr.core.json_parse import parse_field
6159

62-
try:
63-
return cast('Literal["array"]', convert(data, Literal["array"]))
64-
except (ValueError, TypeError) as exc:
65-
msg = f"Invalid value for 'node_type'. Expected 'array'. Got '{data}'."
66-
raise NodeTypeValidationError(msg) from exc
60+
return cast(
61+
'Literal["array"]',
62+
parse_field(data, Literal["array"], "node_type", error=NodeTypeValidationError),
63+
)
6764

6865

6966
def parse_codecs(data: object) -> tuple[Codec, ...]:

0 commit comments

Comments
 (0)