Skip to content
Open
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
4 changes: 2 additions & 2 deletions src/zarr/abc/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

@dataclass(frozen=True)
class Metadata:
def to_dict(self) -> JSON:
def to_dict(self) -> dict[str, JSON]:
"""
Recursively serialize this model to a dictionary.
This method inspects the fields of self and calls `x.to_dict()` for any fields that
Expand All @@ -37,7 +37,7 @@ def to_dict(self) -> JSON:
return out_dict

@classmethod
def from_dict(cls, data: dict[str, JSON]) -> Self:
def from_dict(cls: type[Self], data: dict[str, JSON]) -> Self:
"""
Create an instance of the model from a dictionary
"""
Expand Down
18 changes: 8 additions & 10 deletions src/zarr/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,13 +273,13 @@ async def _create_v2(
return array

@classmethod
def from_dict(
cls,
store_path: StorePath,
data: dict[str, JSON],
async def from_dict(
cls, store_path: StorePath, data: dict[str, JSON], order: Literal["C", "F"] | None = None
) -> AsyncArray:
metadata = parse_array_metadata(data)
async_array = cls(metadata=metadata, store_path=store_path)
data_parsed = parse_array_metadata(data)
async_array = cls(metadata=data_parsed, store_path=store_path, order=order)
# weird that this method doesn't use the metadata attribute
await async_array._save_metadata(async_array.metadata)
return async_array

@classmethod
Expand Down Expand Up @@ -535,11 +535,9 @@ def create(

@classmethod
def from_dict(
cls,
store_path: StorePath,
data: dict[str, JSON],
cls, store_path: StorePath, data: dict[str, JSON], order: Literal["C", "F"] | None = None
) -> Array:
async_array = AsyncArray.from_dict(store_path=store_path, data=data)
async_array = sync(AsyncArray.from_dict(store_path=store_path, data=data))
return cls(async_array)

@classmethod
Expand Down
2 changes: 1 addition & 1 deletion src/zarr/chunk_key_encodings.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def parse_separator(data: JSON) -> SeparatorLiteral:
@dataclass(frozen=True)
class ChunkKeyEncoding(Metadata):
name: str
separator: SeparatorLiteral = "."
separator: SeparatorLiteral = "/"

def __init__(self, *, separator: SeparatorLiteral) -> None:
separator_parsed = parse_separator(separator)
Expand Down
2 changes: 1 addition & 1 deletion src/zarr/codecs/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def from_dict(cls, data: Iterable[JSON | Codec], *, batch_size: int | None = Non
out.append(get_codec_class(name_parsed).from_dict(c)) # type: ignore[arg-type]
return cls.from_list(out, batch_size=batch_size)

def to_dict(self) -> JSON:
def to_dict(self) -> list[JSON]:
return [c.to_dict() for c in self]

def evolve_from_array_spec(self, array_spec: ArraySpec) -> Self:
Expand Down
16 changes: 9 additions & 7 deletions src/zarr/group.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
from collections.abc import AsyncGenerator, Iterable
from typing import Any, Literal

from typing_extensions import Self

logger = logging.getLogger("zarr.group")


Expand Down Expand Up @@ -97,7 +99,7 @@ def __init__(self, attributes: dict[str, Any] | None = None, zarr_format: ZarrFo
object.__setattr__(self, "zarr_format", zarr_format_parsed)

@classmethod
def from_dict(cls, data: dict[str, Any]) -> GroupMetadata:
def from_dict(cls, data: dict[str, Any]) -> Self:
assert data.pop("node_type", None) in ("group", None)
return cls(**data)

Expand Down Expand Up @@ -181,10 +183,10 @@ async def open(
assert zarr_json_bytes is not None
group_metadata = json.loads(zarr_json_bytes.to_bytes())

return cls.from_dict(store_path, group_metadata)
return await cls.from_dict(store_path, group_metadata)

@classmethod
def from_dict(
async def from_dict(
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is a notable change that was needed to get the hierarchy API to work. Previously, from_dict was not async, but it should be.

cls,
store_path: StorePath,
data: dict[str, Any],
Expand Down Expand Up @@ -215,9 +217,9 @@ async def getitem(
else:
zarr_json = json.loads(zarr_json_bytes.to_bytes())
if zarr_json["node_type"] == "group":
return type(self).from_dict(store_path, zarr_json)
return await type(self).from_dict(store_path, zarr_json)
elif zarr_json["node_type"] == "array":
return AsyncArray.from_dict(store_path, zarr_json)
return await AsyncArray.from_dict(store_path, zarr_json)
else:
raise ValueError(f"unexpected node_type: {zarr_json['node_type']}")
elif self.metadata.zarr_format == 2:
Expand All @@ -240,15 +242,15 @@ async def getitem(
if zarray is not None:
# TODO: update this once the V2 array support is part of the primary array class
zarr_json = {**zarray, "attributes": zattrs}
return AsyncArray.from_dict(store_path, zarray)
return sync(AsyncArray.from_dict(store_path, zarray))
else:
zgroup = (
json.loads(zgroup_bytes.to_bytes())
if zgroup_bytes is not None
else {"zarr_format": self.metadata.zarr_format}
)
zarr_json = {**zgroup, "attributes": zattrs}
return type(self).from_dict(store_path, zarr_json)
return await type(self).from_dict(store_path, zarr_json)
else:
raise ValueError(f"unexpected zarr_format: {self.metadata.zarr_format}")

Expand Down
Loading