Skip to content

Commit 6d090a3

Browse files
cwognumdstansby
andauthored
Minor UX improvement: Add the UnknownCodecError. (#689)
* Add the UnknownCodecError * Remove trailing whitespace * Ruff formatting * Add a release note * Add doctest * Fix failing test case * Formatting again * Update numcodecs/tests/test_registry.py Co-authored-by: David Stansby <[email protected]> * Make UnknownCodecError inherit from ValueError --------- Co-authored-by: David Stansby <[email protected]>
1 parent bd517ab commit 6d090a3

File tree

4 files changed

+32
-2
lines changed

4 files changed

+32
-2
lines changed

docs/release.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ Improvements
7070
Import errors caused by optional dependencies (ZFPY, MsgPack, CRC32C, and PCodec)
7171
are still silently caught.
7272
By :user:`David Stansby <dstansby>`, :issue:`550`.
73+
* Raise a custom `UnknownCodecError` when trying to retrieve an unavailable codec.
74+
By :user:`Cas Wognum <cwognum>`.
7375

7476
.. _release_0.14.1:
7577

numcodecs/errors.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
"""
2+
This module defines custom exceptions that are raised in the `numcodecs` codebase.
3+
"""
4+
5+
6+
class UnknownCodecError(ValueError):
7+
"""
8+
An exception that is raised when trying to receive a codec that has not been registered.
9+
10+
Parameters
11+
----------
12+
codec_id : str
13+
Codec identifier.
14+
15+
Examples
16+
----------
17+
>>> import numcodecs
18+
>>> numcodecs.get_codec({"codec_id": "unknown"})
19+
Traceback (most recent call last):
20+
...
21+
UnknownCodecError: codec not available: 'unknown'
22+
"""
23+
24+
def __init__(self, codec_id: str):
25+
self.codec_id = codec_id
26+
super().__init__(f"codec not available: '{codec_id}'")

numcodecs/registry.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from importlib.metadata import EntryPoints, entry_points
66

77
from numcodecs.abc import Codec
8+
from numcodecs.errors import UnknownCodecError
89

910
logger = logging.getLogger("numcodecs")
1011
codec_registry: dict[str, Codec] = {}
@@ -50,7 +51,7 @@ def get_codec(config):
5051
register_codec(cls, codec_id=codec_id)
5152
if cls:
5253
return cls.from_config(config)
53-
raise ValueError(f'codec not available: {codec_id!r}')
54+
raise UnknownCodecError(f"{codec_id!r}")
5455

5556

5657
def register_codec(cls, codec_id=None):

numcodecs/tests/test_registry.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
import pytest
44

55
import numcodecs
6+
from numcodecs.errors import UnknownCodecError
67
from numcodecs.registry import get_codec
78

89

910
def test_registry_errors():
10-
with pytest.raises(ValueError):
11+
with pytest.raises(UnknownCodecError, match='foo'):
1112
get_codec({'id': 'foo'})
1213

1314

0 commit comments

Comments
 (0)