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 3434)
3535from zarr .core .config import config
3636from zarr .core .metadata .common import parse_attributes
37- from zarr .errors import MetadataValidationError , NodeTypeValidationError
37+ from zarr .errors import MetadataValidationError , NodeTypeValidationError , UnknownCodecError
3838from 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
Original file line number Diff line number Diff 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+
104112class NodeTypeValidationError (MetadataValidationError ):
105113 """
106114 Specialized exception when the node_type of the metadata document is incorrect.
Original file line number Diff line number Diff line change 1717from zarr .core .group import GroupMetadata , parse_node_type
1818from 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
2526if 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 )
You can’t perform that action at this time.
0 commit comments