From 4a5a0fdd98c96f9a2ced1a8a1d2d4b9cd98a2537 Mon Sep 17 00:00:00 2001 From: Cas Wognum Date: Tue, 14 Jan 2025 17:26:35 -0500 Subject: [PATCH 1/9] Add the UnknownCodecError --- numcodecs/errors.py | 17 +++++++++++++++++ numcodecs/registry.py | 3 ++- 2 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 numcodecs/errors.py diff --git a/numcodecs/errors.py b/numcodecs/errors.py new file mode 100644 index 00000000..fa85747d --- /dev/null +++ b/numcodecs/errors.py @@ -0,0 +1,17 @@ +""" +This module defines custom exceptions that are raised in the `numcodecs` codebase. +""" + +class UnknownCodecError(Exception): + """ + An exception that is raised when trying to receive a codec that has not been registered. + + Parameters + ---------- + codec_id : str + Codec identifier. + """ + + def __init__(self, codec_id: str): + self.codec_id = codec_id + super().__init__(f"codec not available: '{codec_id}'") \ No newline at end of file diff --git a/numcodecs/registry.py b/numcodecs/registry.py index cc3f3f42..ccb5b26b 100644 --- a/numcodecs/registry.py +++ b/numcodecs/registry.py @@ -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] = {} @@ -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): From 3273e2bf79c0f026fad420355debec279be8a164 Mon Sep 17 00:00:00 2001 From: Cas Wognum Date: Tue, 14 Jan 2025 17:36:03 -0500 Subject: [PATCH 2/9] Remove trailing whitespace --- numcodecs/errors.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/numcodecs/errors.py b/numcodecs/errors.py index fa85747d..39301871 100644 --- a/numcodecs/errors.py +++ b/numcodecs/errors.py @@ -5,7 +5,7 @@ class UnknownCodecError(Exception): """ An exception that is raised when trying to receive a codec that has not been registered. - + Parameters ---------- codec_id : str From 2736b5111abace710409956c6d16b6670c61f586 Mon Sep 17 00:00:00 2001 From: Cas Wognum Date: Tue, 14 Jan 2025 17:36:45 -0500 Subject: [PATCH 3/9] Ruff formatting --- numcodecs/errors.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/numcodecs/errors.py b/numcodecs/errors.py index 39301871..a67deb8a 100644 --- a/numcodecs/errors.py +++ b/numcodecs/errors.py @@ -2,6 +2,7 @@ This module defines custom exceptions that are raised in the `numcodecs` codebase. """ + class UnknownCodecError(Exception): """ An exception that is raised when trying to receive a codec that has not been registered. @@ -14,4 +15,4 @@ class UnknownCodecError(Exception): def __init__(self, codec_id: str): self.codec_id = codec_id - super().__init__(f"codec not available: '{codec_id}'") \ No newline at end of file + super().__init__(f"codec not available: '{codec_id}'") From 4c58f6045fada36d1c763196ed31f515491287a0 Mon Sep 17 00:00:00 2001 From: Cas Wognum Date: Tue, 14 Jan 2025 17:52:11 -0500 Subject: [PATCH 4/9] Add a release note --- docs/release.rst | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/release.rst b/docs/release.rst index 5c8f83b9..fa71c0e8 100644 --- a/docs/release.rst +++ b/docs/release.rst @@ -63,6 +63,8 @@ Improvements Import errors caused by optional dependencies (ZFPY, MsgPack, CRC32C, and PCodec) are still silently caught. By :user:`David Stansby `, :issue:`550`. +* Raise a custom `UnknownCodecError` when trying to retrieve an unavailable codec. + By :user:`Cas Wognum `. 0.14.1 From fc974b9c2a9aa3806b97b5d971f43dcd03cf68f2 Mon Sep 17 00:00:00 2001 From: Cas Wognum Date: Tue, 14 Jan 2025 17:54:24 -0500 Subject: [PATCH 5/9] Add doctest --- numcodecs/errors.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/numcodecs/errors.py b/numcodecs/errors.py index a67deb8a..d1e2f4af 100644 --- a/numcodecs/errors.py +++ b/numcodecs/errors.py @@ -11,6 +11,14 @@ class UnknownCodecError(Exception): ---------- 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): From 03d494e6e5556dca183b379e1e242750d2d3c1f9 Mon Sep 17 00:00:00 2001 From: Cas Wognum Date: Tue, 14 Jan 2025 18:10:25 -0500 Subject: [PATCH 6/9] Fix failing test case --- numcodecs/tests/test_registry.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/numcodecs/tests/test_registry.py b/numcodecs/tests/test_registry.py index 3c779df3..bd665ccf 100644 --- a/numcodecs/tests/test_registry.py +++ b/numcodecs/tests/test_registry.py @@ -4,10 +4,11 @@ import numcodecs from numcodecs.registry import get_codec +from numcodecs.errors import UnknownCodecError def test_registry_errors(): - with pytest.raises(ValueError): + with pytest.raises(UnknownCodecError): get_codec({'id': 'foo'}) From 344fe6e03a0a7d865e6a3287b590e44f8c1a5577 Mon Sep 17 00:00:00 2001 From: Cas Wognum Date: Tue, 14 Jan 2025 18:13:17 -0500 Subject: [PATCH 7/9] Formatting again --- numcodecs/tests/test_registry.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/numcodecs/tests/test_registry.py b/numcodecs/tests/test_registry.py index bd665ccf..ab610e83 100644 --- a/numcodecs/tests/test_registry.py +++ b/numcodecs/tests/test_registry.py @@ -3,8 +3,8 @@ import pytest import numcodecs -from numcodecs.registry import get_codec from numcodecs.errors import UnknownCodecError +from numcodecs.registry import get_codec def test_registry_errors(): From 37ab541520048ed8e81b19b74a786ec849a039f6 Mon Sep 17 00:00:00 2001 From: Cas Wognum Date: Fri, 17 Jan 2025 11:16:46 -0500 Subject: [PATCH 8/9] Update numcodecs/tests/test_registry.py Co-authored-by: David Stansby --- numcodecs/tests/test_registry.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/numcodecs/tests/test_registry.py b/numcodecs/tests/test_registry.py index ab610e83..93ed8f03 100644 --- a/numcodecs/tests/test_registry.py +++ b/numcodecs/tests/test_registry.py @@ -8,7 +8,7 @@ def test_registry_errors(): - with pytest.raises(UnknownCodecError): + with pytest.raises(UnknownCodecError, match='foo'): get_codec({'id': 'foo'}) From e08d0833276aeaa452498da7f7bcb1cdb2a35945 Mon Sep 17 00:00:00 2001 From: Cas Wognum Date: Fri, 17 Jan 2025 11:19:29 -0500 Subject: [PATCH 9/9] Make UnknownCodecError inherit from ValueError --- numcodecs/errors.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/numcodecs/errors.py b/numcodecs/errors.py index d1e2f4af..78e048e6 100644 --- a/numcodecs/errors.py +++ b/numcodecs/errors.py @@ -3,7 +3,7 @@ """ -class UnknownCodecError(Exception): +class UnknownCodecError(ValueError): """ An exception that is raised when trying to receive a codec that has not been registered.