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] Hierarchy api #1912

Open
wants to merge 22 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
5aa0c17
initial hierarchy API
d-v-b May 24, 2024
94a60ae
start recursive ser/deseriaization tests, fix issues in the Array API
d-v-b May 26, 2024
c3cf284
Merge branch 'v3' of https://github.com/zarr-developers/zarr-python i…
d-v-b May 30, 2024
3bf098a
add ArrayModel.from_array, await unawaited awaitables, add flattening…
d-v-b May 30, 2024
38e003f
make test_asyncgroup_from_dict async
d-v-b May 30, 2024
aeb8e05
Merge branch 'v3' into hierarchy_api
d-v-b Jun 1, 2024
bae6d46
Enable warn_unreachable for mypy (#1937)
dstansby Jun 1, 2024
bcec6d6
Run sphinx directly on readthedocs (#1919)
dstansby Jun 1, 2024
81d6063
Fix list of packages in mypy pre-commit environment (#1907)
dstansby Jun 1, 2024
a72a669
Bump the actions group with 6 updates (#1904)
dependabot[bot] Jun 1, 2024
89c9ae6
Fix final typing errors (#1939)
d-v-b Jun 4, 2024
d59daa9
feature(typing): add py.typed file to package root (#1935)
jhamman Jun 2, 2024
87c97ba
Apply preview ruff rules (#1942)
DimitriPapadopoulos Jun 2, 2024
f01f9bb
Enable and apply ruff rule RUF009 (#1941)
DimitriPapadopoulos Jun 2, 2024
1ea8373
Support all indexing variants (#1917)
normanrz Jun 3, 2024
1cb6179
Feature: group and array name properties (#1940)
jhamman Jun 3, 2024
080bcd5
implement .chunks on v3 arrays (#1929)
rabernat Jun 3, 2024
449cfaa
chore: update pre-commit hooks (#1948)
pre-commit-ci[bot] Jun 3, 2024
24461d5
fixes bug in transpose (#1949)
normanrz Jun 4, 2024
0d208cf
Create issue-metrics.yml
jhamman Jun 4, 2024
b98c06c
Merge branch 'v3' of github.com:zarr-developers/zarr-python into hier…
d-v-b Jun 4, 2024
fff6a2c
Merge branch 'v3' into hierarchy_api
d-v-b Jun 5, 2024
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 @@ -296,13 +296,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 @@ -627,11 +627,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 @@ -83,7 +83,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