Skip to content

feat: add from_array_metadata_and_store to CodecPipeline #3233

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

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 changes/3233.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add an alternate `from_array_metadata_and_store` constructor to `CodecPipeline`.
22 changes: 21 additions & 1 deletion src/zarr/abc/codec.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@
from collections.abc import Awaitable, Callable, Iterable
from typing import Self

from zarr.abc.store import ByteGetter, ByteSetter
from zarr.abc.store import ByteGetter, ByteSetter, Store
from zarr.core.array_spec import ArraySpec
from zarr.core.chunk_grids import ChunkGrid
from zarr.core.dtype.wrapper import TBaseDType, TBaseScalar, ZDType
from zarr.core.indexing import SelectorTuple
from zarr.core.metadata import ArrayMetadata

__all__ = [
"ArrayArrayCodec",
Expand Down Expand Up @@ -281,6 +282,25 @@ def from_codecs(cls, codecs: Iterable[Codec]) -> Self:
"""
...

@classmethod
def from_array_metadata_and_store(cls, array_metadata: ArrayMetadata, store: Store) -> Self:
"""Creates a codec pipeline from array metadata and a store path.

Raises NotImplementedError by default, indicating the CodecPipeline must be created with from_codecs instead.

Parameters
----------
array_metadata : ArrayMetadata
store : Store

Returns
-------
Self
"""
raise NotImplementedError(
f"'{type(cls).__name__}' does not implement CodecPipeline.from_array_metadata_and_store."
)

@property
@abstractmethod
def supports_partial_decode(self) -> bool: ...
Expand Down
16 changes: 14 additions & 2 deletions src/zarr/core/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,15 @@ def parse_array_metadata(data: Any) -> ArrayMetadata:
raise TypeError # pragma: no cover


def create_codec_pipeline(metadata: ArrayMetadata) -> CodecPipeline:
def create_codec_pipeline(metadata: ArrayMetadata, *, store: Store | None = None) -> CodecPipeline:
if store is not None:
try:
return get_pipeline_class().from_array_metadata_and_store(
array_metadata=metadata, store=store
)
except NotImplementedError:
pass

if isinstance(metadata, ArrayV3Metadata):
return get_pipeline_class().from_codecs(metadata.codecs)
elif isinstance(metadata, ArrayV2Metadata):
Expand Down Expand Up @@ -311,7 +319,11 @@ def __init__(
object.__setattr__(self, "metadata", metadata_parsed)
object.__setattr__(self, "store_path", store_path)
object.__setattr__(self, "_config", config_parsed)
object.__setattr__(self, "codec_pipeline", create_codec_pipeline(metadata=metadata_parsed))
object.__setattr__(
self,
"codec_pipeline",
create_codec_pipeline(metadata=metadata_parsed, store=store_path.store),
)

# this overload defines the function signature when zarr_format is 2
@overload
Expand Down
Loading