Skip to content

Commit 6bde3f4

Browse files
vup903claude
andcommitted
Migrate Batch 2 codec primitive parsers to parse_json (zarr-developers#3285)
Delegate the int/bool type check of parse_checksum, parse_clevel, parse_blocksize, parse_typesize, parse_gzip_level, and parse_zstd_level to parse_json, keeping each helper's range/bound check and exact error messages. Original exception types are preserved by wrapping parse_json's ValueError/TypeError. Note: parse_json rejects bool where the old isinstance(data, int) accepted it; verified no caller/test passes a bool to these, so this is a deliberate, more-correct strictening. parse_json imported function-locally. Codec suite + full mypy green (794 passed). Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
1 parent 730274a commit 6bde3f4

3 files changed

Lines changed: 51 additions & 27 deletions

File tree

‎src/zarr/codecs/blosc.py‎

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -104,27 +104,39 @@ class BloscCname(metaclass=_DeprecatedStrEnumMeta):
104104

105105

106106
def parse_typesize(data: JSON) -> int:
107-
if isinstance(data, int):
108-
if data > 0:
109-
return data
110-
else:
111-
raise ValueError(
112-
f"Value must be greater than 0. Got {data}, which is less or equal to 0."
113-
)
114-
raise TypeError(f"Value must be an int. Got {type(data)} instead.")
107+
from zarr.core.json_parse import parse_json
108+
109+
try:
110+
parsed: int = parse_json(data, int)
111+
except (ValueError, TypeError) as exc:
112+
raise TypeError(f"Value must be an int. Got {type(data)} instead.") from exc
113+
if parsed > 0:
114+
return parsed
115+
else:
116+
raise ValueError(
117+
f"Value must be greater than 0. Got {parsed}, which is less or equal to 0."
118+
)
115119

116120

117121
# todo: real validation
118122
def parse_clevel(data: JSON) -> int:
119-
if isinstance(data, int):
120-
return data
121-
raise TypeError(f"Value should be an int. Got {type(data)} instead.")
123+
from zarr.core.json_parse import parse_json
124+
125+
try:
126+
parsed: int = parse_json(data, int)
127+
except (ValueError, TypeError) as exc:
128+
raise TypeError(f"Value should be an int. Got {type(data)} instead.") from exc
129+
return parsed
122130

123131

124132
def parse_blocksize(data: JSON) -> int:
125-
if isinstance(data, int):
126-
return data
127-
raise TypeError(f"Value should be an int. Got {type(data)} instead.")
133+
from zarr.core.json_parse import parse_json
134+
135+
try:
136+
parsed: int = parse_json(data, int)
137+
except (ValueError, TypeError) as exc:
138+
raise TypeError(f"Value should be an int. Got {type(data)} instead.") from exc
139+
return parsed
128140

129141

130142
def _parse_cname(data: object) -> BloscCnameLiteral:

‎src/zarr/codecs/gzip.py‎

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

2020

2121
def parse_gzip_level(data: JSON) -> int:
22-
if not isinstance(data, (int)):
23-
raise TypeError(f"Expected int, got {type(data)}")
24-
if data not in range(10):
22+
from zarr.core.json_parse import parse_json
23+
24+
try:
25+
parsed: int = parse_json(data, int)
26+
except (ValueError, TypeError) as exc:
27+
raise TypeError(f"Expected int, got {type(data)}") from exc
28+
if parsed not in range(10):
2529
raise ValueError(
26-
f"Expected an integer from the inclusive range (0, 9). Got {data} instead."
30+
f"Expected an integer from the inclusive range (0, 9). Got {parsed} instead."
2731
)
28-
return data
32+
return parsed
2933

3034

3135
@dataclass(frozen=True)

‎src/zarr/codecs/zstd.py‎

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,25 @@
2121

2222

2323
def parse_zstd_level(data: JSON) -> int:
24-
if isinstance(data, int):
25-
if data >= 23:
26-
raise ValueError(f"Value must be less than or equal to 22. Got {data} instead.")
27-
return data
28-
raise TypeError(f"Got value with type {type(data)}, but expected an int.")
24+
from zarr.core.json_parse import parse_json
25+
26+
try:
27+
parsed: int = parse_json(data, int)
28+
except (ValueError, TypeError) as exc:
29+
raise TypeError(f"Got value with type {type(data)}, but expected an int.") from exc
30+
if parsed >= 23:
31+
raise ValueError(f"Value must be less than or equal to 22. Got {parsed} instead.")
32+
return parsed
2933

3034

3135
def parse_checksum(data: JSON) -> bool:
32-
if isinstance(data, bool):
33-
return data
34-
raise TypeError(f"Expected bool. Got {type(data)}.")
36+
from zarr.core.json_parse import parse_json
37+
38+
try:
39+
parsed: bool = parse_json(data, bool)
40+
except (ValueError, TypeError) as exc:
41+
raise TypeError(f"Expected bool. Got {type(data)}.") from exc
42+
return parsed
3543

3644

3745
@dataclass(frozen=True)

0 commit comments

Comments
 (0)