Skip to content

Remove OpenMS::String wrapper and simplify string handling in pyOpenMS - #10109

Merged
timosachsenberg merged 2 commits into
developfrom
claude/test000-py-state-9ojr4u
Sep 10, 2026
Merged

timosachsenberg merged 2 commits into
developfrom
claude/test000-py-state-9ojr4u

Conversation

@timosachsenberg

@timosachsenberg timosachsenberg commented Sep 10, 2026 •

Copy link
Copy Markdown
Contributor

Description

This PR removes the pure Python String wrapper class and updates the pyOpenMS bindings to work directly with Python str objects. The changes simplify the API by eliminating the need for users to wrap strings in pyopenms.String() objects.

Key Changes

  1. Removed String class: Deleted src/pyOpenMS/pyopenms/addons/string_class.py which provided a wrapper around OpenMS::String. Users no longer need to use pyopenms.String().

  2. Updated bindings to return strings directly:

    • MSNumpressCoder.encodeNP() and encodeNPRaw() now return str/bytes directly instead of requiring an output parameter
    • MzMLFile.storeBuffer() now returns a str buffer instead of modifying an output parameter
    • Base64.encode64(), encode32(), decode64() updated similarly
    • ResidueDB.getResidue(), ProteaseDB.getEnzyme() and similar methods now accept plain str instead of requiring String objects
  3. Simplified test code: Updated all test files to use plain Python strings instead of pyopenms.String() wrappers:

    • test_000.py: Removed from pyopenms import String as s imports and replaced all s("...") calls with plain strings
    • test_tutorial.py: Removed String import
    • test_MSNumpressCoder.py: Updated to work with returned strings instead of output parameters
  4. Updated documentation: Modified doc/pyopenms/docs/source/user_guide/mzml_files.rst to show the new API with direct string returns instead of String wrapper objects.

  5. Fixed exception handling: Updated testAASequence() to catch IndexError instead of RuntimeError for out-of-range access, reflecting improved error handling in the bindings.

  6. Improved Param.__iter__(): Now yields ParamEntry objects instead of just keys, providing more useful iteration semantics.

Benefits

  • Simpler API: Users work with native Python strings, not wrapper objects
  • More Pythonic: Return values instead of output parameters for encoding/decoding operations
  • Better type hints: Clearer what types are expected and returned
  • Reduced maintenance: One less pure Python class to maintain

Checklist

  • Make sure that you are listed in the AUTHORS file
  • Add relevant changes and new features to the CHANGELOG file
  • I have commented my code, particularly in hard-to-understand areas
  • New and existing unit tests pass locally with my changes
  • Updated or added python bindings for changed or new classes (Updated bindings in bind_format.cpp and bind_misc.cpp)

https://claude.ai/code/session_01K8Qvor6Nb2iNJKr1RPt4DU

Summary by CodeRabbit

  • New Features

    • Matrix retrieval now provides an independent, writable copy; zero-copy views remain available separately.
    • Parameter updates accept Boolean values and integer 0/1 flags.
    • Encoding and mzML buffer APIs now return strings or bytes directly, simplifying Python usage.
    • Out-of-range residue access now raises IndexError.
  • Breaking Changes

    • The pyopenms.String compatibility wrapper is no longer available. Use standard Python strings and bytes instead.
  • Documentation

    • Updated mzML user-guide examples to reflect the revised return-value APIs.

timosachsenberg and others added 2 commits September 10, 2026 13:36
pytest is configured with python_files = ["test_*.py"], so
tests/unittests/test000.py was never collected by the pyopenms_unittests
ctest target and had drifted from the nanobind bindings: 15 of its 177
tests failed when run explicitly. Rename it to test_000.py so it is
collected, and fix the failures at their root:

Bindings
- Param.update(): evaluate the flag by Python truthiness. nanobind's bool
  caster accepts only True/False, so passing 0/1 raised
  "RuntimeError: std::bad_cast" (six tests via _testParam).
- MatrixDouble.get_matrix(): return an owned, writable copy. Casting the
  Eigen::Map aliased the C++ storage as a read-only array despite the
  "returns a copy" docstring, and the array outlived the Matrix.
- ParamEntry/ParamNode.__eq__: add nb::is_operator() so comparing with an
  unrelated type returns NotImplemented (False) instead of TypeError.
- std::string caster: accept pyopenms.String. The addon is a mutable box
  whose `_value` payload bindings already use to write back String&
  output parameters (storeBuffer, encodeNP); after the caster
  consolidation it was no longer accepted as an input, so
  ProteaseDB.hasEnzyme(String(...)), ResidueDB.getResidue(String(...)),
  Element(String(...), ...) etc. raised TypeError.

Tests
- AASequence.getResidue() out of range raises IndexError (the binding
  checks the index), not RuntimeError.
- Iterating a Param yields ParamEntry copies; compare their names with
  keys() and check entry == str is False rather than a TypeError.
- Peak1D.__str__ is the compact "(mz, intensity)" form, distinct from
  __repr__.
- Element exposes no mutators (the test already asserts this); exercise
  the constructor's str/bytes/String handling instead of setSymbol().

Verified with a Release build (nanobind 3.0.1): test_000.py 173 passed,
2 skipped; the full unittests directory 3143 passed with test_000.py
collected; top-level tests 80 passed.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_01K8Qvor6Nb2iNJKr1RPt4DU
…value

There is no String class in the nanobind bindings: OpenMS::String
parameters are plain std::string and arrive as Python str. The pure
Python pyopenms.String box (addons/string_class.py) only survived to
emulate Cython-era String& output parameters, with three bindings writing
their result into its `_value` attribute. Remove the shim and make those
bindings return the result instead:

- MzMLFile.storeBuffer(exp) -> str
- MSNumpressCoder.encodeNP(in, zlib_compression, config) -> str
- MSNumpressCoder.encodeNPRaw(in, config) -> bytes

Revert the std::string caster change from the previous commit that
accepted the box as input; tests now pass plain str everywhere.

Tests: test_000.py uses str for Element, ResidueDB, ProteaseDB and
IBSpectraFile arguments, uses the returned values for storeBuffer and
encodeNP, and loses testString (it tested the shim itself).
test_MSNumpressCoder.py uses the returned values; test_tutorial.py drops
an unused String import.

Docs: the mzML user guide examples for Base64 and numpress encoding used
the old output-parameter calls; Base64.encode64/encode32/decode64 already
return their result, so update both examples and their expected output.

Verified with a Release build: the full pyOpenMS suite passes (3222
passed, 85 skipped), and the updated docs snippets reproduce the
documented output.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_01K8Qvor6Nb2iNJKr1RPt4DU
@coderabbitai

coderabbitai Bot commented Sep 10, 2026

Copy link
Copy Markdown
Contributor

Review Change StackReview Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Advanced

Run ID: 56eafb60-68dd-4035-af7c-1da9ac6b47e9

📥 Commits

Reviewing files that changed from the base of the PR and between f97bc89 and 4297280.

📒 Files selected for processing (9)
  • doc/pyopenms/docs/source/user_guide/mzml_files.rst
  • src/pyOpenMS/bindings/bind_datastructures.cpp
  • src/pyOpenMS/bindings/bind_format.cpp
  • src/pyOpenMS/bindings/bind_misc.cpp
  • src/pyOpenMS/pyopenms/addons/__init__.py
  • src/pyOpenMS/pyopenms/addons/string_class.py
  • src/pyOpenMS/tests/unittests/test_000.py
  • src/pyOpenMS/tests/unittests/test_MSNumpressCoder.py
  • src/pyOpenMS/tests/unittests/test_tutorial.py
💤 Files with no reviewable changes (3)
  • src/pyOpenMS/tests/unittests/test_tutorial.py
  • src/pyOpenMS/pyopenms/addons/init.py
  • src/pyOpenMS/pyopenms/addons/string_class.py

Included review availability: Your plan provides up to 4 included reviews per hour; 3 remain after this review.


📝 Walkthrough

Walkthrough

Changes

PyOpenMS API modernization

Layer / File(s) Summary
Return-value binding APIs
src/pyOpenMS/bindings/bind_format.cpp, src/pyOpenMS/bindings/bind_misc.cpp, doc/pyopenms/docs/source/user_guide/mzml_files.rst, src/pyOpenMS/tests/unittests/*
Encoding and mzML buffer methods now return strings or bytes directly. Documentation and tests use the updated APIs.
Binding behavior corrections
src/pyOpenMS/bindings/bind_datastructures.cpp, src/pyOpenMS/tests/unittests/test_000.py
MatrixDouble.get_matrix returns an owned copy. Param.update accepts truthy integer flags. Parameter equality bindings use operator registration.
Native Python string migration
src/pyOpenMS/pyopenms/addons/*, src/pyOpenMS/tests/unittests/*
The Python String wrapper and namespace injection are removed. Affected tests use native Python strings and bytes.

Suggested reviewers: cbielow

Merge Risk: ⚪ Minimal · up to 42972

This change modernizes pyOpenMS bindings to use native Python strings and bytes while updating related APIs and tests. The reported full suite passes, with no concrete merge-blocking risk remaining.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 52.17% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 23 functions across 5 files. (1 skipped: … Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the primary change: removing the OpenMS::String wrapper and simplifying string handling in pyOpenMS.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Full details: Docstring Coverage

Explanation

Docstring coverage is 52.17% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 23 functions across 5 files. (1 skipped: 1 unsupported.)

  • Fix all pre-merge checks with AI
✨ Finishing Touches 💡 1
📝 Generate docstrings 💡
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch claude/test000-py-state-9ojr4u

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

A rabbit hops through bindings bright
Plain strings guide the code tonight
Bytes return with nulls intact
Old wrappers leave the interface stack
Tests follow each changed track

Comment @coderabbitai help to get the list of available commands.

@timosachsenberg
timosachsenberg merged commit 439cc50 into develop Sep 10, 2026
14 checks passed
@timosachsenberg
timosachsenberg deleted the claude/test000-py-state-9ojr4u branch September 10, 2026 14:54
timosachsenberg added a commit that referenced this pull request Sep 12, 2026
…10128)

PR #10109 removed the legacy pyopenms.String shim and changed
storeBuffer()/encodeNP()/encodeNPRaw() to return values directly, but
the PR did not update the CHANGELOG (checklist item left unchecked).


Claude-Session: https://claude.ai/code/session_01BA4gTubWVwKSgYT1ti1YCo

Co-authored-by: Claude <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant