Skip to content

Commit

Permalink
Minor UX improvement: Add the UnknownCodecError. (zarr-developers#689)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
cwognum and dstansby authored Jan 17, 2025
1 parent bd517ab commit 6d090a3
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 2 deletions.
2 changes: 2 additions & 0 deletions docs/release.rst
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ Improvements
Import errors caused by optional dependencies (ZFPY, MsgPack, CRC32C, and PCodec)
are still silently caught.
By :user:`David Stansby <dstansby>`, :issue:`550`.
* Raise a custom `UnknownCodecError` when trying to retrieve an unavailable codec.
By :user:`Cas Wognum <cwognum>`.

.. _release_0.14.1:

Expand Down
26 changes: 26 additions & 0 deletions numcodecs/errors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
"""
This module defines custom exceptions that are raised in the `numcodecs` codebase.
"""


class UnknownCodecError(ValueError):
"""
An exception that is raised when trying to receive a codec that has not been registered.
Parameters
----------
codec_id : str
Codec identifier.
Examples
----------
>>> import numcodecs
>>> numcodecs.get_codec({"codec_id": "unknown"})
Traceback (most recent call last):
...
UnknownCodecError: codec not available: 'unknown'
"""

def __init__(self, codec_id: str):
self.codec_id = codec_id
super().__init__(f"codec not available: '{codec_id}'")
3 changes: 2 additions & 1 deletion numcodecs/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from importlib.metadata import EntryPoints, entry_points

from numcodecs.abc import Codec
from numcodecs.errors import UnknownCodecError

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


def register_codec(cls, codec_id=None):
Expand Down
3 changes: 2 additions & 1 deletion numcodecs/tests/test_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
import pytest

import numcodecs
from numcodecs.errors import UnknownCodecError
from numcodecs.registry import get_codec


def test_registry_errors():
with pytest.raises(ValueError):
with pytest.raises(UnknownCodecError, match='foo'):
get_codec({'id': 'foo'})


Expand Down

0 comments on commit 6d090a3

Please sign in to comment.