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

Remove attrs #1660

Merged
merged 31 commits into from
Feb 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
67c64ea
begin removing attrs in favor of frozen dataclasses
d-v-b Feb 4, 2024
d743a6e
remove runtime_configuration from corearraymetadata; rename CodecMeta…
d-v-b Feb 6, 2024
c95ba00
Merge branch 'v3' of github.com:zarr-developers/zarr-python into remo…
d-v-b Feb 6, 2024
5d745eb
add typing_extensions dependency
d-v-b Feb 6, 2024
af92ebf
finalize merge
d-v-b Feb 8, 2024
b398bb8
remove CodecMetadatas and data_types; move basic parsers to common
d-v-b Feb 8, 2024
e756d62
feat: base to_dict method that actually works
d-v-b Feb 9, 2024
0ca3554
Making Codec classes self-contained
normanrz Feb 9, 2024
f163bb4
fixes
normanrz Feb 9, 2024
8c16d7d
chunk_grids
normanrz Feb 9, 2024
ce87d89
chunk key encodings
normanrz Feb 9, 2024
0fb188d
dry-er parse_*
normanrz Feb 9, 2024
249b05a
serialize to enums
normanrz Feb 9, 2024
1e103e3
ruff
normanrz Feb 9, 2024
42e83e8
organize imports
normanrz Feb 9, 2024
5778126
merge upstream
normanrz Feb 9, 2024
29b3d2a
rm src/zarr/v3/codecs/common.py
normanrz Feb 9, 2024
2f535c7
Merge pull request #109 from zarr-developers/single-codec-classes
d-v-b Feb 9, 2024
abea690
resolve merge conflicts
normanrz Feb 9, 2024
45adb5e
cleanup error messages
normanrz Feb 9, 2024
3f9c388
better codec evolve
normanrz Feb 9, 2024
fb18e9d
__init__ types
normanrz Feb 12, 2024
4c448dc
add validation to RuntimeConfiguration.__init__; create a validator f…
d-v-b Feb 12, 2024
132ef97
Merge branch 'v3' of https://github.com/zarr-developers/zarr-python i…
d-v-b Feb 12, 2024
45c14a4
import Dict at runtime
d-v-b Feb 12, 2024
3a22bf2
fix parse_dimension_names
d-v-b Feb 12, 2024
7a10b85
add tests; tweak parse_name function; adjust some exception messages
d-v-b Feb 14, 2024
8b882b7
fix shapelike parser and tests; clean up imports
d-v-b Feb 14, 2024
b7e0d75
typing
normanrz Feb 16, 2024
efd8351
improve typing
normanrz Feb 16, 2024
286deec
blacken test_common
d-v-b Feb 19, 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
6 changes: 5 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,12 @@ extra-dependencies = [
"coverage",
"pytest",
"pytest-cov",
"msgpack",
"lmdb",
"zstandard",
"crc32c",
"pytest-asyncio",
"mypy",
"typing_extensions"
]
features = ["extra"]

Expand Down
22 changes: 14 additions & 8 deletions src/zarr/v3/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
from typing import Union

import zarr.v3.codecs # noqa: F401
from zarr.v3.array import Array # noqa: F401
from zarr.v3.array_v2 import ArrayV2 # noqa: F401
from zarr.v3.group import Group # noqa: F401
from zarr.v3.metadata import RuntimeConfiguration, runtime_configuration # noqa: F401
from zarr.v3.array import Array, AsyncArray # noqa: F401
from zarr.v3.array_v2 import ArrayV2
from zarr.v3.config import RuntimeConfiguration # noqa: F401
from zarr.v3.group import AsyncGroup, Group # noqa: F401
from zarr.v3.metadata import runtime_configuration # noqa: F401
from zarr.v3.store import ( # noqa: F401
StoreLike,
make_store_path,
Expand All @@ -17,19 +18,24 @@
async def open_auto_async(
store: StoreLike,
runtime_configuration_: RuntimeConfiguration = RuntimeConfiguration(),
) -> Union[Array, ArrayV2, Group]:
) -> Union[AsyncArray, AsyncGroup]:
store_path = make_store_path(store)
try:
return await Array.open(store_path, runtime_configuration=runtime_configuration_)
return await AsyncArray.open(store_path, runtime_configuration=runtime_configuration_)
except KeyError:
return await Group.open(store_path, runtime_configuration=runtime_configuration_)
return await AsyncGroup.open(store_path, runtime_configuration=runtime_configuration_)


def open_auto(
store: StoreLike,
runtime_configuration_: RuntimeConfiguration = RuntimeConfiguration(),
) -> Union[Array, ArrayV2, Group]:
return _sync(
object = _sync(
open_auto_async(store, runtime_configuration_),
runtime_configuration_.asyncio_loop,
)
if isinstance(object, AsyncArray):
return Array(object)
if isinstance(object, AsyncGroup):
return Group(object)
raise TypeError(f"Unexpected object type. Got {type(object)}.")
32 changes: 10 additions & 22 deletions src/zarr/v3/abc/codec.py
Original file line number Diff line number Diff line change
@@ -1,45 +1,33 @@
from __future__ import annotations

from abc import abstractmethod, ABC
from typing import TYPE_CHECKING, Optional, Type
from abc import abstractmethod
from typing import TYPE_CHECKING, Optional

import numpy as np
from zarr.v3.abc.metadata import Metadata

from zarr.v3.common import BytesLike, SliceSelection
from zarr.v3.common import ArraySpec
from zarr.v3.store import StorePath


if TYPE_CHECKING:
from zarr.v3.metadata import (
ArraySpec,
ArrayMetadata,
DataType,
CodecMetadata,
RuntimeConfiguration,
)
from typing_extensions import Self
from zarr.v3.common import BytesLike, SliceSelection
from zarr.v3.metadata import ArrayMetadata
from zarr.v3.config import RuntimeConfiguration


class Codec(ABC):
class Codec(Metadata):
is_fixed_size: bool

@classmethod
@abstractmethod
def get_metadata_class(cls) -> Type[CodecMetadata]:
pass

@classmethod
@abstractmethod
def from_metadata(cls, codec_metadata: CodecMetadata) -> Codec:
pass

@abstractmethod
def compute_encoded_size(self, input_byte_length: int, chunk_spec: ArraySpec) -> int:
pass

def resolve_metadata(self, chunk_spec: ArraySpec) -> ArraySpec:
return chunk_spec

def evolve(self, *, ndim: int, data_type: DataType) -> Codec:
def evolve(self, array_spec: ArraySpec) -> Self:
return self

def validate(self, array_metadata: ArrayMetadata) -> None:
Expand Down
44 changes: 44 additions & 0 deletions src/zarr/v3/abc/metadata.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
from __future__ import annotations
from typing import TYPE_CHECKING, Sequence

if TYPE_CHECKING:
from typing import Dict
from typing_extensions import Self

from dataclasses import fields

from zarr.v3.common import JSON


class Metadata:
def to_dict(self) -> JSON:
"""
Recursively serialize this model to a dictionary.
This method inspects the fields of self and calls `x.to_dict()` for any fields that
are instances of `Metadata`. Sequences of `Metadata` are similarly recursed into, and
the output of that recursion is collected in a list.
"""
...
out_dict = {}
for field in fields(self):
key = field.name
value = getattr(self, key)
if isinstance(value, Metadata):
out_dict[field.name] = getattr(self, field.name).to_dict()
elif isinstance(value, str):
out_dict[key] = value
elif isinstance(value, Sequence):
out_dict[key] = [v.to_dict() if isinstance(v, Metadata) else v for v in value]
else:
out_dict[key] = value

return out_dict

@classmethod
def from_dict(cls, data: Dict[str, JSON]) -> Self:
"""
Create an instance of the model from a dictionary
"""
...

return cls(**data)
Loading
Loading