Skip to content

fix: correct tuple index in ProForma.__getitem__ slicing#213

Closed
steps-re wants to merge 1 commit into
levitsky:masterfrom
steps-re:fix/proforma-tuple-index
Closed

fix: correct tuple index in ProForma.__getitem__ slicing#213
steps-re wants to merge 1 commit into
levitsky:masterfrom
steps-re:fix/proforma-tuple-index

Conversation

@steps-re

Copy link
Copy Markdown

Bug

Slicing a ProForma sequence that has a grouped modification (e.g. #g1) raises a TypeError.

In ProForma.__getitem__, when handling a slice, the code loops over tag_hits from find_tags_by_id(group_id, include_position=True), which returns a list of (position, tag) tuples. The loop variable i gets reassigned to tag_hits[0], the whole tuple, and is then used directly as subseq[i]. __getitem__ only accepts int or slice, so indexing with a tuple fails.

Repro:

from pyteomics import proforma

seq = "EMEVT[#g1]S[#g1]ES[#g1]PEK"
obj = proforma.ProForma.parse(seq)
obj[2:9]
# TypeError: list indices must be integers or slices, not tuple

Fix

Use tag_hits[0][0] (the position) instead of the full tuple.

Testing

  • Added test_slice_grouped_modification in tests/test_proforma.py, which fails with the TypeError above on the old code and passes with the fix.
  • Ran the full tests/test_proforma.py suite: 62 passed, 0 failed, no regressions.

When slicing a ProForma sequence containing a grouped modification
(e.g. "#g1"), the loop variable i was reassigned to the
(position, tag) tuple returned by find_tags_by_id instead of just
the position, then used directly as subseq[i]. Since __getitem__
only accepts int or slice, this raised a TypeError.

Use tag_hits[0][0] to pass the position instead of the full tuple.

Signed-off-by: Mike German <mike@stepsventures.com>
@mobiusklein

Copy link
Copy Markdown
Contributor

Thank you for reporting this. I believe this is amongst the many historical bugs being fixed in #212. Manually testing your example with that branch worked as expected. Could you please test that branch for your use-case?

mobiusklein added a commit to mobiusklein/pyteomics that referenced this pull request Jul 14, 2026
@steps-re

Copy link
Copy Markdown
Author

Confirmed, thanks @mobiusklein. I checked out fix/proforma-coverage-fixes (#212) and ran my exact repro against it:

from pyteomics import proforma
sub = proforma.ProForma.parse("EMEVT[#g1]S[#g1]ES[#g1]PEK")[2:9]
# -> "EVT[#g1]S[#g1]ES[#g1]P", group_ids == ['#g1']

No more TypeError and the slice is correct, so #212 covers this. Closing this in favor of it.

If it's useful, the regression test I added here (test_slice_grouped_modification) pins this specific grouped-modification slice case, and #212 is all about backfilling tests for historically untested paths, so you're welcome to lift it in. Happy to open a tiny test-only PR against your branch if you'd prefer. Thanks for the fast turnaround.

@steps-re steps-re closed this Jul 14, 2026
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.

2 participants