Skip to content

Fix item lookups and bookkeeping in ContentSequence - #480

Merged
CPBridge merged 1 commit into
ImagingDataCommons:masterfrom
DG47:bugfix/content-sequence-index
Oct 22, 2026
Merged

CPBridge merged 1 commit into
ImagingDataCommons:masterfrom
DG47:bugfix/content-sequence-index

Conversation

@DG47

@DG47 DG47 commented Oct 21, 2026

Copy link
Copy Markdown
Contributor

Fixes #479

ContentSequence.index() looked the item up in the per-name lookup table and returned its position among the items with the same concept name, not its position in the sequence. remove(), inherited from MutableSequence, calls index() and deletes at that position, so ContentSequence([a, b]).remove(b) deleted a.

The lookup table behind find() also drifted out of sync with the sequence:

  • extend() added each item to the lookup table itself and then called append(), which added it again. find() therefore returned duplicates, and an item that append() rejected for a missing relationship type stayed in the table.
  • += went through pydicom's Sequence.__iadd__, so the lookup table and the relationship type checks were skipped.
  • __setitem__ used isinstance(val, Iterable) to tell a single item from a list. A Dataset is iterable, so seq[0] = item raised TypeError and seq[0] = Dataset() was accepted. Neither form updated the lookup table.

Changes, all in ContentSequence:

  • index() returns the position in the sequence (first item that is, or is equal to, the given item), matching list.index.
  • extend() relies on append() to register each item.
  • __iadd__ delegates to extend().
  • __setitem__ checks the index type to tell a single item from a slice, and updates the lookup table for both the replaced and the new items.

The public API does not change.

Tests: new cases in TestContentSequence in tests/test_sr.py (index, remove, find after extend and +=, item and slice assignment). The 9 new tests fail on master and pass with this change.

pytest tests/test_sr.py -k TestContentSequence   # 29 passed
pytest tests                                     # 2048 passed, 153 skipped
flake8 src tests                                 # no errors

ContentSequence.index() returned the position of the item among the
items sharing its concept name rather than its position in the
sequence. As a consequence, remove() (which relies on index()) could
delete a different item than the one requested.

In addition, the lookup table used by find() was not kept consistent
with the sequence: extend() registered every item twice (and also
registered items that were subsequently rejected), "+=" bypassed both
the lookup table and the relationship checks, and __setitem__ neither
accepted a single content item nor updated the lookup table.
@CPBridge CPBridge added the bug Something isn't working label Oct 22, 2026

@CPBridge CPBridge left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Hi @DG47, looks like you are having a lot of fun with SRs

Again here, I don't think this functionality has been used much at all since it was implemented. Thanks for the fix, it looks good to me

@CPBridge
CPBridge merged commit 8a812e4 into ImagingDataCommons:master Oct 22, 2026
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ContentSequence.index() returns the wrong position, so remove() can delete the wrong item

2 participants