Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion psm_utils/__init__.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
25 changes: 17 additions & 8 deletions psm_utils/io/cbor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)

Expand All @@ -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):
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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 = []
Loading