Skip to content

Commit ba6b5c0

Browse files
authored
add json indentation to config (#1952)
1 parent 143faea commit ba6b5c0

File tree

4 files changed

+40
-11
lines changed

4 files changed

+40
-11
lines changed

src/zarr/config.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"array": {"order": "C"},
1212
"async": {"concurrency": None, "timeout": None},
1313
"codec_pipeline": {"batch_size": 1},
14+
"json_indent": 2,
1415
}
1516
],
1617
)

src/zarr/group.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
ChunkCoords,
2626
ZarrFormat,
2727
)
28+
from zarr.config import config
2829
from zarr.store import StoreLike, StorePath, make_store_path
2930
from zarr.sync import SyncMixin, sync
3031

@@ -79,14 +80,21 @@ class GroupMetadata(Metadata):
7980
node_type: Literal["group"] = field(default="group", init=False)
8081

8182
def to_buffer_dict(self) -> dict[str, Buffer]:
83+
json_indent = config.get("json_indent")
8284
if self.zarr_format == 3:
83-
return {ZARR_JSON: Buffer.from_bytes(json.dumps(self.to_dict()).encode())}
85+
return {
86+
ZARR_JSON: Buffer.from_bytes(
87+
json.dumps(self.to_dict(), indent=json_indent).encode()
88+
)
89+
}
8490
else:
8591
return {
8692
ZGROUP_JSON: Buffer.from_bytes(
87-
json.dumps({"zarr_format": self.zarr_format}).encode()
93+
json.dumps({"zarr_format": self.zarr_format}, indent=json_indent).encode()
94+
),
95+
ZATTRS_JSON: Buffer.from_bytes(
96+
json.dumps(self.attributes, indent=json_indent).encode()
8897
),
89-
ZATTRS_JSON: Buffer.from_bytes(json.dumps(self.attributes).encode()),
9098
}
9199

92100
def __init__(self, attributes: dict[str, Any] | None = None, zarr_format: ZarrFormat = 3):

src/zarr/metadata.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from zarr.chunk_grids import ChunkGrid, RegularChunkGrid
1717
from zarr.chunk_key_encodings import ChunkKeyEncoding, parse_separator
1818
from zarr.codecs._v2 import V2Compressor, V2Filters
19+
from zarr.config import config
1920

2021
if TYPE_CHECKING:
2122
from typing_extensions import Self
@@ -272,8 +273,11 @@ def _json_convert(o: np.dtype[Any] | Enum | Codec) -> str | dict[str, Any]:
272273
return config
273274
raise TypeError
274275

276+
json_indent = config.get("json_indent")
275277
return {
276-
ZARR_JSON: Buffer.from_bytes(json.dumps(self.to_dict(), default=_json_convert).encode())
278+
ZARR_JSON: Buffer.from_bytes(
279+
json.dumps(self.to_dict(), default=_json_convert, indent=json_indent).encode()
280+
)
277281
}
278282

279283
@classmethod
@@ -394,9 +398,12 @@ def _json_convert(
394398
assert isinstance(zarray_dict, dict)
395399
zattrs_dict = zarray_dict.pop("attributes", {})
396400
assert isinstance(zattrs_dict, dict)
401+
json_indent = config.get("json_indent")
397402
return {
398-
ZARRAY_JSON: Buffer.from_bytes(json.dumps(zarray_dict, default=_json_convert).encode()),
399-
ZATTRS_JSON: Buffer.from_bytes(json.dumps(zattrs_dict).encode()),
403+
ZARRAY_JSON: Buffer.from_bytes(
404+
json.dumps(zarray_dict, default=_json_convert, indent=json_indent).encode()
405+
),
406+
ZATTRS_JSON: Buffer.from_bytes(json.dumps(zattrs_dict, indent=json_indent).encode()),
400407
}
401408

402409
@classmethod

tests/v3/test_config.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,32 @@
1+
from typing import Any
2+
3+
import pytest
4+
15
from zarr.config import config
26

37

4-
def test_config_defaults_set():
8+
def test_config_defaults_set() -> None:
59
# regression test for available defaults
610
assert config.defaults == [
711
{
812
"array": {"order": "C"},
913
"async": {"concurrency": None, "timeout": None},
1014
"codec_pipeline": {"batch_size": 1},
15+
"json_indent": 2,
1116
}
1217
]
1318
assert config.get("array.order") == "C"
19+
assert config.get("async.concurrency") is None
20+
assert config.get("async.timeout") is None
21+
assert config.get("codec_pipeline.batch_size") == 1
22+
assert config.get("json_indent") == 2
1423

1524

16-
def test_config_defaults_can_be_overridden():
17-
assert config.get("array.order") == "C"
18-
with config.set({"array.order": "F"}):
19-
assert config.get("array.order") == "F"
25+
@pytest.mark.parametrize(
26+
"key, old_val, new_val",
27+
[("array.order", "C", "F"), ("async.concurrency", None, 10), ("json_indent", 2, 0)],
28+
)
29+
def test_config_defaults_can_be_overridden(key: str, old_val: Any, new_val: Any) -> None:
30+
assert config.get(key) == old_val
31+
with config.set({key: new_val}):
32+
assert config.get(key) == new_val

0 commit comments

Comments
 (0)