Skip to content
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/3395.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Raise a Zarr-specific error class when a codec can't be found by name when deserializing the given codecs. This avoids hiding this error behind a "not part of a zarr hierarchy" warning.
8 changes: 6 additions & 2 deletions src/zarr/core/metadata/v3.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
)
from zarr.core.config import config
from zarr.core.metadata.common import parse_attributes
from zarr.errors import MetadataValidationError, NodeTypeValidationError
from zarr.errors import MetadataValidationError, NodeTypeValidationError, UnknownCodecError
from zarr.registry import get_codec_class


Expand Down Expand Up @@ -63,7 +63,11 @@ def parse_codecs(data: object) -> tuple[Codec, ...]:
out += (c,)
else:
name_parsed, _ = parse_named_configuration(c, require_configuration=False)
out += (get_codec_class(name_parsed).from_dict(c),)

try:
out += (get_codec_class(name_parsed).from_dict(c),)
except KeyError as e:
raise UnknownCodecError(f"Unknown codec: {e.args[0]!r}") from e

return out

Expand Down
8 changes: 8 additions & 0 deletions src/zarr/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,14 @@ class MetadataValidationError(BaseZarrError):
_msg = "Invalid value for '{}'. Expected '{}'. Got '{}'."


class UnknownCodecError(BaseZarrError):
"""
Raised when a unknown codec was used.
"""

_msg = "{}"


class NodeTypeValidationError(MetadataValidationError):
"""
Specialized exception when the node_type of the metadata document is incorrect.
Expand Down
17 changes: 16 additions & 1 deletion tests/test_metadata/test_v3.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@
from zarr.core.group import GroupMetadata, parse_node_type
from zarr.core.metadata.v3 import (
ArrayV3Metadata,
parse_codecs,
parse_dimension_names,
parse_zarr_format,
)
from zarr.errors import MetadataValidationError, NodeTypeValidationError
from zarr.errors import MetadataValidationError, NodeTypeValidationError, UnknownCodecError

if TYPE_CHECKING:
from collections.abc import Sequence
Expand Down Expand Up @@ -323,3 +324,17 @@ async def test_special_float_fill_values(fill_value: str) -> None:
elif fill_value == "-Infinity":
assert np.isneginf(m.fill_value)
assert d["fill_value"] == "-Infinity"


def test_parse_codecs_unknown_codec_raises(monkeypatch: pytest.MonkeyPatch) -> None:
from collections import defaultdict

import zarr.registry
from zarr.registry import Registry

# to make sure the codec is always unknown (not sure if that's necessary)
monkeypatch.setattr(zarr.registry, "__codec_registries", defaultdict(Registry))

codecs = [{"name": "unknown"}]
with pytest.raises(UnknownCodecError):
parse_codecs(codecs)
Loading