File tree Expand file tree Collapse file tree 4 files changed +31
-3
lines changed Expand file tree Collapse file tree 4 files changed +31
-3
lines changed Original file line number Diff line number Diff line change
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.
Original file line number Diff line number Diff line change 34
34
)
35
35
from zarr .core .config import config
36
36
from zarr .core .metadata .common import parse_attributes
37
- from zarr .errors import MetadataValidationError , NodeTypeValidationError
37
+ from zarr .errors import MetadataValidationError , NodeTypeValidationError , UnknownCodecError
38
38
from zarr .registry import get_codec_class
39
39
40
40
@@ -63,7 +63,11 @@ def parse_codecs(data: object) -> tuple[Codec, ...]:
63
63
out += (c ,)
64
64
else :
65
65
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
67
71
68
72
return out
69
73
Original file line number Diff line number Diff line change @@ -101,6 +101,14 @@ class MetadataValidationError(BaseZarrError):
101
101
_msg = "Invalid value for '{}'. Expected '{}'. Got '{}'."
102
102
103
103
104
+ class UnknownCodecError (BaseZarrError ):
105
+ """
106
+ Raised when a unknown codec was used.
107
+ """
108
+
109
+ _msg = "{}"
110
+
111
+
104
112
class NodeTypeValidationError (MetadataValidationError ):
105
113
"""
106
114
Specialized exception when the node_type of the metadata document is incorrect.
Original file line number Diff line number Diff line change 17
17
from zarr .core .group import GroupMetadata , parse_node_type
18
18
from zarr .core .metadata .v3 import (
19
19
ArrayV3Metadata ,
20
+ parse_codecs ,
20
21
parse_dimension_names ,
21
22
parse_zarr_format ,
22
23
)
23
- from zarr .errors import MetadataValidationError , NodeTypeValidationError
24
+ from zarr .errors import MetadataValidationError , NodeTypeValidationError , UnknownCodecError
24
25
25
26
if TYPE_CHECKING :
26
27
from collections .abc import Sequence
@@ -323,3 +324,17 @@ async def test_special_float_fill_values(fill_value: str) -> None:
323
324
elif fill_value == "-Infinity" :
324
325
assert np .isneginf (m .fill_value )
325
326
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 )
You can’t perform that action at this time.
0 commit comments