Skip to content

Commit 9498336

Browse files
authored
reraise the KeyError for unknown codecs with a more specific error class (#3395)
* create a error class for unknown codecs * reraise as a unknown codec error * check that `parse_codecs` raises (v3 only) * monkeypatch to always work with a empty registry * immediatly format the error * changelog
1 parent c019a5f commit 9498336

File tree

4 files changed

+31
-3
lines changed

4 files changed

+31
-3
lines changed

changes/3395.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
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.

src/zarr/core/metadata/v3.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
)
3535
from zarr.core.config import config
3636
from zarr.core.metadata.common import parse_attributes
37-
from zarr.errors import MetadataValidationError, NodeTypeValidationError
37+
from zarr.errors import MetadataValidationError, NodeTypeValidationError, UnknownCodecError
3838
from zarr.registry import get_codec_class
3939

4040

@@ -63,7 +63,11 @@ def parse_codecs(data: object) -> tuple[Codec, ...]:
6363
out += (c,)
6464
else:
6565
name_parsed, _ = parse_named_configuration(c, require_configuration=False)
66-
out += (get_codec_class(name_parsed).from_dict(c),)
66+
67+
try:
68+
out += (get_codec_class(name_parsed).from_dict(c),)
69+
except KeyError as e:
70+
raise UnknownCodecError(f"Unknown codec: {e.args[0]!r}") from e
6771

6872
return out
6973

src/zarr/errors.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,14 @@ class MetadataValidationError(BaseZarrError):
101101
_msg = "Invalid value for '{}'. Expected '{}'. Got '{}'."
102102

103103

104+
class UnknownCodecError(BaseZarrError):
105+
"""
106+
Raised when a unknown codec was used.
107+
"""
108+
109+
_msg = "{}"
110+
111+
104112
class NodeTypeValidationError(MetadataValidationError):
105113
"""
106114
Specialized exception when the node_type of the metadata document is incorrect.

tests/test_metadata/test_v3.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@
1717
from zarr.core.group import GroupMetadata, parse_node_type
1818
from zarr.core.metadata.v3 import (
1919
ArrayV3Metadata,
20+
parse_codecs,
2021
parse_dimension_names,
2122
parse_zarr_format,
2223
)
23-
from zarr.errors import MetadataValidationError, NodeTypeValidationError
24+
from zarr.errors import MetadataValidationError, NodeTypeValidationError, UnknownCodecError
2425

2526
if TYPE_CHECKING:
2627
from collections.abc import Sequence
@@ -323,3 +324,17 @@ async def test_special_float_fill_values(fill_value: str) -> None:
323324
elif fill_value == "-Infinity":
324325
assert np.isneginf(m.fill_value)
325326
assert d["fill_value"] == "-Infinity"
327+
328+
329+
def test_parse_codecs_unknown_codec_raises(monkeypatch: pytest.MonkeyPatch) -> None:
330+
from collections import defaultdict
331+
332+
import zarr.registry
333+
from zarr.registry import Registry
334+
335+
# to make sure the codec is always unknown (not sure if that's necessary)
336+
monkeypatch.setattr(zarr.registry, "__codec_registries", defaultdict(Registry))
337+
338+
codecs = [{"name": "unknown"}]
339+
with pytest.raises(UnknownCodecError):
340+
parse_codecs(codecs)

0 commit comments

Comments
 (0)