Skip to content

[pyOpenMS][TEST] Pin deep-copy independence (mutate copy, original unchanged) (#9460) - #9525

Merged
timosachsenberg merged 1 commit into
OpenMS:developfrom
timosachsenberg:test/9460-pyopenms-copy-independence
Jun 16, 2026
Merged

timosachsenberg merged 1 commit into
OpenMS:developfrom
timosachsenberg:test/9460-pyopenms-copy-independence

Conversation

@timosachsenberg

@timosachsenberg timosachsenberg commented Jun 15, 2026 •

Copy link
Copy Markdown
Contributor

What

Adds test_copy_independence.py: for 17 pyopenms classes it builds a non-trivial instance, copies it three ways (copy.copy, copy.deepcopy, ClassName(obj)), mutates only the copy, and asserts the original is unchanged (and that the mutation took effect on the copy). 17 classes × 3 copy forms = 51 parametrized cases.

Why

This closes a real, still-open part of the §2 (pyOpenMS nanobind rewrite) [AUTO] task in the OpenMS 3.6.0 pre-release testing plan (#9460):

Per copy-ctor class (≥12, incl. AASequence): copy → mutate the copy → assert the original is unchanged. Pass: original untouched; copy reflects the mutation.

The existing test_copy_constructors.py parametrizes over every copyable class, but it default-constructs an empty object, copies it, and only checks type(copy) is type(orig) plus copy == orig. Two freshly-constructed empty objects compare equal whether or not they share underlying C++ storage, and the copy is never mutated — so that sweep cannot catch the exact aliasing / object-slicing regression the nanobind rewrite fixed (an old binding could return a "copy" that aliases the original, so mutating the copy silently corrupts the original).

This test pins that independence contract directly.

Coverage

Scalar setter/getter classes: Peak1D, Peak2D, Residue, PeptideHit, PeptideIdentification, ProteinIdentification, MSSpectrum, MSChromatogram, Precursor, Feature, ConsensusFeature, Param, DateTime; the named example AASequence (mutated via setNTerminalModification); and the MetaInfoInterface container types MSExperiment, FeatureMap, ConsensusMap — where shared internal vectors/maps are exactly where an aliasing bug would hide.

Test plan

Tests only — no production code changed. Verified against the built nanobind bindings:

$ python3 -m pytest test_copy_independence.py -q
51 passed

The assertions are non-vacuous: each case checks the copy starts equal to the original, the original is unchanged after mutating the copy, and the mutation actually changed the copy.

Part of #9460. Complements #9524 (Bruker TDF native-ID parser test).

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Tests
    • Added comprehensive test suite to validate copy independence for multiple pyOpenMS classes, ensuring that shallow copy, deep copy, and copy-constructor operations maintain proper isolation—copies remain independent from originals, and mutations to copies do not affect source objects.

…changed) (OpenMS#9460)

test_copy_constructors.py proves copy.copy()/copy.deepcopy()/ClassName(obj)
return the right type and compare equal to a default-constructed original, but
it never mutates the copy, so it cannot catch the aliasing/object-slicing
regression the nanobind rewrite fixed (a "copy" that shares the original's C++
storage; mutating it corrupts the original). Comparing two empty objects for
equality passes regardless of shared state.

This adds the contract OpenMS OpenMS#9460 §2 asks for:

    copy -> mutate the copy -> assert the original is unchanged

For 17 classes (incl. AASequence, and the MetaInfoInterface containers
MSExperiment/FeatureMap/ConsensusMap where shared vectors/maps would hide an
aliasing bug) it builds a non-trivial instance, copies it three ways, mutates
only the copy, and asserts: the copy starts with the original's value, mutating
the copy leaves the original untouched, and the mutation took effect on the
copy. 17 classes x 3 copy forms = 51 parametrized cases.

Verified against the built nanobind bindings (51 passed).

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@github-actions

Copy link
Copy Markdown
Contributor

@timosachsenberg The following names might be missing from the AUTHORS file:

  • Timo Sachsenberg

Please consider applying the following patch:

--- AUTHORS	2026-06-15 14:47:43.112885708 +0000
+++ AUTHORS.new	2026-06-15 14:47:47.455919509 +0000
@@ -23,8 +23,8 @@
  - Chirag Dave
  - Chris Bauer
  - Chris Bielow
- - Christie Mathews
  - Christian Ehrlich
+ - Christie Mathews
  - Clemens Groepl
  - Cornelia Friedle
  - Daniel Jameson

@coderabbitai

coderabbitai Bot commented Jun 15, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

A new pytest module test_copy_independence.py is added to the pyOpenMS unit test suite. It defines builder functions for pyopenms classes, a CASES registry mapping each class to its builder/mutate/observe lambdas, a shared _assert_independent helper, and three parameterized tests verifying that copy.copy, copy.deepcopy, and the copy-constructor each produce fully independent copies.

Changes

Copy Independence Test Suite

Layer / File(s) Summary
Builder helpers and CASES registry
src/pyOpenMS/tests/unittests/test_copy_independence.py
Module docstring describes the nanobind aliasing/slicing regression being targeted. _b_* builder functions construct non-trivial pyopenms instances with known observable state. CASES enumerates each class with its builder, mutate lambda, and observe lambda; CASE_IDS derives stable pytest parametrization ids from it.
_assert_independent helper and parameterized tests
src/pyOpenMS/tests/unittests/test_copy_independence.py
_assert_independent reads the baseline observable value, makes a copy, asserts the copy matches the baseline, mutates the copy, then asserts the original is unchanged and the copy reflects the mutation. Three @pytest.mark.parametrize test functions invoke this helper for copy.copy, copy.deepcopy, and lambda o: type(o)(o).

Possibly related PRs

  • OpenMS/OpenMS#9098: Adjusts copy-constructor dispatch for AASequence and nullptr-safe copy/assign for BinnedSpectrum — directly related to the same copy-constructor correctness that the new independence tests now verify.

Poem

🐇 A copy's a copy, or ought to be true,
Mutate the clone and the original's new?
No! Builder helpers build objects with care,
And three tests confirm independence is there.
Deep, shallow, or constructor — all must stay fair! ✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. 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 main change: adding a test that verifies deep-copy independence for pyOpenMS classes, ensuring mutating a copy leaves the original unchanged.
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.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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

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

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
src/pyOpenMS/tests/unittests/test_copy_independence.py (1)

39-39: ⚡ Quick win

Refactor builder functions to avoid compound statements.

PEP 8 discourages compound statements (multiple statements on the same line). Split these into separate lines for better readability and compliance.

♻️ Proposed refactor

Example for _b_peak1d:

 def _b_peak1d():
-    o = pyopenms.Peak1D(); o.setMZ(100.0); return o
+    o = pyopenms.Peak1D()
+    o.setMZ(100.0)
+    return o

Apply the same pattern to all other builder functions (lines 42, 45, 48, 51, 54, 57, 60, 63, 66, 69, 75, 78, 81, 84, 87).

Also applies to: 42-42, 45-45, 48-48, 51-51, 54-54, 57-57, 60-60, 63-63, 66-66, 69-69, 75-75, 78-78, 81-81, 84-84, 87-87

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/pyOpenMS/tests/unittests/test_copy_independence.py` at line 39, Multiple
builder functions in this file use compound statements on single lines,
violating PEP 8 conventions. Split each builder function (such as _b_peak1d and
all other similar builder functions throughout the file) so that each statement
appears on its own line instead of using semicolons to chain multiple statements
together. This applies consistently to all builder functions that currently have
multiple statements separated by semicolons on the same line.

Source: Coding guidelines

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@src/pyOpenMS/tests/unittests/test_copy_independence.py`:
- Line 39: Multiple builder functions in this file use compound statements on
single lines, violating PEP 8 conventions. Split each builder function (such as
_b_peak1d and all other similar builder functions throughout the file) so that
each statement appears on its own line instead of using semicolons to chain
multiple statements together. This applies consistently to all builder functions
that currently have multiple statements separated by semicolons on the same
line.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 8487e240-fdb4-4cdf-9936-1f8c3eeef179

📥 Commits

Reviewing files that changed from the base of the PR and between aacd941 and da1d9b8.

📒 Files selected for processing (1)
  • src/pyOpenMS/tests/unittests/test_copy_independence.py

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