From d1f76fc38754ab51075242d080a3ad70c56ca5fe Mon Sep 17 00:00:00 2001 From: jakirkham Date: Mon, 18 Nov 2024 00:30:18 -0800 Subject: [PATCH 1/5] Disable `PCodec` if dependencies are unavailable --- numcodecs/__init__.py | 5 +++-- numcodecs/pcodec.py | 8 +------- numcodecs/tests/test_pcodec.py | 5 +---- 3 files changed, 5 insertions(+), 13 deletions(-) diff --git a/numcodecs/__init__.py b/numcodecs/__init__.py index 8b949d15..1d1f6d04 100644 --- a/numcodecs/__init__.py +++ b/numcodecs/__init__.py @@ -144,6 +144,7 @@ register_codec(Fletcher32) -from numcodecs.pcodec import PCodec +with suppress(ImportError): + from numcodecs.pcodec import PCodec -register_codec(PCodec) + register_codec(PCodec) diff --git a/numcodecs/pcodec.py b/numcodecs/pcodec.py index ceb012f0..84597479 100644 --- a/numcodecs/pcodec.py +++ b/numcodecs/pcodec.py @@ -3,10 +3,7 @@ from numcodecs.abc import Codec from numcodecs.compat import ensure_contiguous_ndarray -try: - from pcodec import ChunkConfig, ModeSpec, PagingSpec, standalone -except ImportError: # pragma: no cover - standalone = None +from pcodec import ChunkConfig, ModeSpec, PagingSpec, standalone DEFAULT_MAX_PAGE_N = 262144 @@ -49,9 +46,6 @@ def __init__( # TODO one day, add support for the Try* mode specs mode_spec: Literal['auto', 'classic'] = 'auto', ): - if standalone is None: # pragma: no cover - raise ImportError("pcodec must be installed to use the PCodec codec.") - # note that we use `level` instead of `compression_level` to # match other codecs self.level = level diff --git a/numcodecs/tests/test_pcodec.py b/numcodecs/tests/test_pcodec.py index c10549bd..4143cee6 100644 --- a/numcodecs/tests/test_pcodec.py +++ b/numcodecs/tests/test_pcodec.py @@ -1,11 +1,8 @@ import numpy as np import pytest -from numcodecs.pcodec import PCodec - try: - # initializing codec triggers ImportError - PCodec() + from numcodecs.pcodec import PCodec except ImportError: # pragma: no cover pytest.skip("pcodec not available", allow_module_level=True) From acbf6107fe76996957365b19e200a4c625e0b075 Mon Sep 17 00:00:00 2001 From: jakirkham Date: Mon, 18 Nov 2024 00:32:04 -0800 Subject: [PATCH 2/5] Run `isort` on `pcodec` module --- numcodecs/pcodec.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/numcodecs/pcodec.py b/numcodecs/pcodec.py index 84597479..825f2b74 100644 --- a/numcodecs/pcodec.py +++ b/numcodecs/pcodec.py @@ -1,10 +1,9 @@ from typing import Literal, Optional -from numcodecs.abc import Codec -from numcodecs.compat import ensure_contiguous_ndarray - from pcodec import ChunkConfig, ModeSpec, PagingSpec, standalone +from numcodecs.abc import Codec +from numcodecs.compat import ensure_contiguous_ndarray DEFAULT_MAX_PAGE_N = 262144 From 6dd00c067e0eb847d58f762057a7afd5ef87cda5 Mon Sep 17 00:00:00 2001 From: jakirkham Date: Wed, 4 Dec 2024 16:17:03 -0800 Subject: [PATCH 3/5] Add release note --- docs/release.rst | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/release.rst b/docs/release.rst index 68a751d3..e29cd02f 100644 --- a/docs/release.rst +++ b/docs/release.rst @@ -27,6 +27,8 @@ Fixes ~~~~~ * Fixes issue with ``Delta`` Zarr 3 codec not working with ``astype``. By :user:`Norman Rzepka `, :issue:`664` +* Cleanup ``PCodec`` soft dependency. + By :user:`John Kirkham `, :issue:`647` Improvements From a4e1bb6d3c505e264c2f51d65cc2718b00962f2a Mon Sep 17 00:00:00 2001 From: David Stansby Date: Fri, 17 Jan 2025 08:17:51 +0000 Subject: [PATCH 4/5] Update release note --- docs/release.rst | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/release.rst b/docs/release.rst index e29cd02f..5a8f2d42 100644 --- a/docs/release.rst +++ b/docs/release.rst @@ -28,6 +28,8 @@ Fixes * Fixes issue with ``Delta`` Zarr 3 codec not working with ``astype``. By :user:`Norman Rzepka `, :issue:`664` * Cleanup ``PCodec`` soft dependency. + Previously importing ``numcodecs.pcodec`` would work if ``pcodec`` is not installed, + but now it will fail to import. This mirrors the behaviour of other optional dependencies. By :user:`John Kirkham `, :issue:`647` From 36bbed701e7d7556e39ed28dac4623753c53098d Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 17 Jan 2025 08:24:24 +0000 Subject: [PATCH 5/5] style: pre-commit fixes --- numcodecs/pcodec.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/numcodecs/pcodec.py b/numcodecs/pcodec.py index 6e91df20..995a1ce0 100644 --- a/numcodecs/pcodec.py +++ b/numcodecs/pcodec.py @@ -1,9 +1,8 @@ from typing import Literal -from pcodec import ChunkConfig, DeltaSpec, ModeSpec, PagingSpec, standalone - from numcodecs.abc import Codec from numcodecs.compat import ensure_contiguous_ndarray +from pcodec import ChunkConfig, DeltaSpec, ModeSpec, PagingSpec, standalone DEFAULT_MAX_PAGE_N = 262144