From af68ba762b2658c8a6742c36ae7019878cbbaabe Mon Sep 17 00:00:00 2001 From: RalfG Date: Tue, 28 Oct 2025 21:47:42 +0100 Subject: [PATCH] Fix lazy import of optional cbor2 package --- CHANGELOG.md | 6 ++++++ psm_utils/__init__.py | 2 +- psm_utils/io/cbor.py | 25 +++++++++++++++++-------- 3 files changed, 24 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 263e411..d919602 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [1.5.0.post1] - 2025-10-28 + +### Fixed + +- 🐛 `io.cbor`: Fixed lazy import of optional cbor2 package. + ## [1.5.0] - 2025-10-27 ### Added diff --git a/psm_utils/__init__.py b/psm_utils/__init__.py index d8a2705..c759a5f 100644 --- a/psm_utils/__init__.py +++ b/psm_utils/__init__.py @@ -1,6 +1,6 @@ """Common utilities for parsing and handling PSMs, and search engine results.""" -__version__ = "1.5.0" +__version__ = "1.5.0.post1" __all__ = ["Peptidoform", "PSM", "PSMList"] from warnings import filterwarnings diff --git a/psm_utils/io/cbor.py b/psm_utils/io/cbor.py index e36fdf9..3aa1979 100644 --- a/psm_utils/io/cbor.py +++ b/psm_utils/io/cbor.py @@ -29,12 +29,11 @@ from psm_utils.psm_list import PSMList try: - import cbor2 + import cbor2 # type: ignore[import] + + _has_cbor2 = True except ImportError: - raise ImportError( - "The cbor2 package is required to use the CBOR reader/writer. " - "Install it with: pip install cbor2" - ) + _has_cbor2 = False logger = logging.getLogger(__name__) @@ -57,13 +56,18 @@ def __init__(self, filename: str | Path, *args, **kwargs): """ super().__init__(filename, *args, **kwargs) + if not _has_cbor2: + raise ImportError( + "The cbor2 package is required to use the CBOR reader/writer. " + "Install it with: pip install cbor2" + ) def __iter__(self): """Iterate over file and return PSMs one-by-one.""" with open(self.filename, "rb") as open_file: try: - data = cbor2.load(open_file) - except cbor2.CBORDecodeError as e: + data = cbor2.load(open_file) # type: ignore[attr-defined] + except cbor2.CBORDecodeError as e: # type: ignore[attr-defined] raise PSMUtilsIOException(f"Could not decode CBOR file: {e}") from e if not isinstance(data, list): @@ -122,6 +126,11 @@ def __init__( """ super().__init__(filename, *args, **kwargs) + if not _has_cbor2: + raise ImportError( + "The cbor2 package is required to use the CBOR reader/writer. " + "Install it with: pip install cbor2" + ) self._psm_cache: list[dict[str, Any]] = [] def __enter__(self) -> CBORWriter: @@ -155,6 +164,6 @@ def _psm_to_entry(psm: PSM) -> dict: def _flush(self): """Write the cached PSMs to the CBOR file.""" with open(self.filename, "wb") as open_file: - cbor2.dump(self._psm_cache, open_file) + cbor2.dump(self._psm_cache, open_file) # type: ignore[attr-defined] self._psm_cache = []