Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[v3] add json indentation to config #1952

Merged
merged 1 commit into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/zarr/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"array": {"order": "C"},
"async": {"concurrency": None, "timeout": None},
"codec_pipeline": {"batch_size": 1},
"json_indent": 2,
}
],
)
Expand Down
14 changes: 11 additions & 3 deletions src/zarr/group.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
ChunkCoords,
ZarrFormat,
)
from zarr.config import config
from zarr.store import StoreLike, StorePath, make_store_path
from zarr.sync import SyncMixin, sync

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

def to_buffer_dict(self) -> dict[str, Buffer]:
json_indent = config.get("json_indent")
if self.zarr_format == 3:
return {ZARR_JSON: Buffer.from_bytes(json.dumps(self.to_dict()).encode())}
return {
ZARR_JSON: Buffer.from_bytes(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should add a Buffer.from_json method

json.dumps(self.to_dict(), indent=json_indent).encode()
)
}
else:
return {
ZGROUP_JSON: Buffer.from_bytes(
json.dumps({"zarr_format": self.zarr_format}).encode()
json.dumps({"zarr_format": self.zarr_format}, indent=json_indent).encode()
),
ZATTRS_JSON: Buffer.from_bytes(
json.dumps(self.attributes, indent=json_indent).encode()
),
ZATTRS_JSON: Buffer.from_bytes(json.dumps(self.attributes).encode()),
}

def __init__(self, attributes: dict[str, Any] | None = None, zarr_format: ZarrFormat = 3):
Expand Down
13 changes: 10 additions & 3 deletions src/zarr/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from zarr.chunk_grids import ChunkGrid, RegularChunkGrid
from zarr.chunk_key_encodings import ChunkKeyEncoding, parse_separator
from zarr.codecs._v2 import V2Compressor, V2Filters
from zarr.config import config

if TYPE_CHECKING:
from typing_extensions import Self
Expand Down Expand Up @@ -270,8 +271,11 @@ def _json_convert(o: np.dtype[Any] | Enum | Codec) -> str | dict[str, Any]:
return config
raise TypeError

json_indent = config.get("json_indent")
return {
ZARR_JSON: Buffer.from_bytes(json.dumps(self.to_dict(), default=_json_convert).encode())
ZARR_JSON: Buffer.from_bytes(
json.dumps(self.to_dict(), default=_json_convert, indent=json_indent).encode()
)
}

@classmethod
Expand Down Expand Up @@ -392,9 +396,12 @@ def _json_convert(
assert isinstance(zarray_dict, dict)
zattrs_dict = zarray_dict.pop("attributes", {})
assert isinstance(zattrs_dict, dict)
json_indent = config.get("json_indent")
return {
ZARRAY_JSON: Buffer.from_bytes(json.dumps(zarray_dict, default=_json_convert).encode()),
ZATTRS_JSON: Buffer.from_bytes(json.dumps(zattrs_dict).encode()),
ZARRAY_JSON: Buffer.from_bytes(
json.dumps(zarray_dict, default=_json_convert, indent=json_indent).encode()
),
ZATTRS_JSON: Buffer.from_bytes(json.dumps(zattrs_dict, indent=json_indent).encode()),
}

@classmethod
Expand Down
23 changes: 18 additions & 5 deletions tests/v3/test_config.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,32 @@
from typing import Any

import pytest

from zarr.config import config


def test_config_defaults_set():
def test_config_defaults_set() -> None:
# regression test for available defaults
assert config.defaults == [
{
"array": {"order": "C"},
"async": {"concurrency": None, "timeout": None},
"codec_pipeline": {"batch_size": 1},
"json_indent": 2,
}
]
assert config.get("array.order") == "C"
assert config.get("async.concurrency") is None
assert config.get("async.timeout") is None
assert config.get("codec_pipeline.batch_size") == 1
assert config.get("json_indent") == 2


def test_config_defaults_can_be_overridden():
assert config.get("array.order") == "C"
with config.set({"array.order": "F"}):
assert config.get("array.order") == "F"
@pytest.mark.parametrize(
"key, old_val, new_val",
[("array.order", "C", "F"), ("async.concurrency", None, 10), ("json_indent", 2, 0)],
)
def test_config_defaults_can_be_overridden(key: str, old_val: Any, new_val: Any) -> None:
assert config.get(key) == old_val
with config.set({key: new_val}):
assert config.get(key) == new_val