Skip to content

Commit 8771582

Browse files
d-v-bclaude
andcommitted
fix(zarr-metadata): refuse a JSONWriterConfig str indent that is not JSON whitespace
`json.dumps` writes a str indent verbatim, so `indent="x"` wrote bytes that no JSON reader parses. The config now raises `ValueError` unless the string holds only spaces, tabs, line feeds and carriage returns. Assisted-by: ClaudeCode:claude-opus-5-5 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 351fdc1 commit 8771582

2 files changed

Lines changed: 13 additions & 2 deletions

File tree

‎packages/zarr-metadata/src/zarr_metadata/model/_shared.py‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,8 @@ class JSONWriterConfig:
316316
equivalent of `JSONWriterConfig(indent=2)`.
317317
318318
`indent` is passed to `json.dumps`: `None` writes one line, an `int`
319-
nests by that many spaces, and a `str` nests by that string. The
319+
nests by that many spaces, and a `str` of JSON whitespace (spaces,
320+
tabs, line feeds and carriage returns) nests by that string. The
320321
separators are those of Python's `json` module: `", "` between items
321322
and `": "` after a key, with the item separator becoming `","` (the
322323
space replaced by the line break) when `indent` is set. `sort_keys`
@@ -336,14 +337,18 @@ class JSONWriterConfig:
336337
def __post_init__(self) -> None:
337338
# Enforce the annotations at runtime: `json.dumps` reads `True` as an
338339
# indent of 1 and a negative indent as 0, so a mistyped config would
339-
# otherwise change the spelling silently.
340+
# otherwise change the spelling silently, and it writes a str indent
341+
# verbatim, so one that is not whitespace writes invalid JSON.
340342
indent = cast("object", self.indent)
341343
if isinstance(indent, bool) or not isinstance(indent, int | str | None):
342344
msg = f"indent must be an int, a str or None, got {type(indent).__name__}"
343345
raise TypeError(msg)
344346
if isinstance(indent, int) and indent < 0:
345347
msg = f"indent must not be negative, got {indent}"
346348
raise ValueError(msg)
349+
if isinstance(indent, str) and indent.strip(" \t\n\r") != "":
350+
msg = f"indent must hold only JSON whitespace, got {indent!r}"
351+
raise ValueError(msg)
347352
for name in ("sort_keys", "ensure_ascii", "trailing_newline"):
348353
flag = cast("object", getattr(self, name))
349354
if not isinstance(flag, bool):

‎packages/zarr-metadata/tests/model/test_writer_config.py‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,12 @@ def test_config_refuses_a_negative_indent() -> None:
125125
JSONWriterConfig(indent=-3)
126126

127127

128+
def test_config_refuses_an_indent_that_is_not_json_whitespace() -> None:
129+
"""A `str` indent must hold only JSON whitespace: `json.dumps` writes it verbatim, so any other character writes bytes no JSON reader parses."""
130+
with pytest.raises(ValueError, match="indent must hold only JSON whitespace, got 'x'"):
131+
JSONWriterConfig(indent="x")
132+
133+
128134
@pytest.mark.parametrize("name", ["sort_keys", "ensure_ascii", "trailing_newline"])
129135
def test_config_refuses_a_flag_that_is_not_a_bool(name: str) -> None:
130136
"""Each flag takes only a `bool`: a truthy value such as `1` is refused."""

0 commit comments

Comments
 (0)