Repository navigation
Fix item lookups and bookkeeping in ContentSequence - #480
Merged
CPBridge merged 1 commit intoOct 22, 2026
Merged
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 fromMutableSequence, callsindex()and deletes at that position, soContentSequence([a, b]).remove(b)deleteda.The lookup table behind
find()also drifted out of sync with the sequence:extend()added each item to the lookup table itself and then calledappend(), which added it again.find()therefore returned duplicates, and an item thatappend()rejected for a missing relationship type stayed in the table.+=went through pydicom'sSequence.__iadd__, so the lookup table and the relationship type checks were skipped.__setitem__usedisinstance(val, Iterable)to tell a single item from a list. ADatasetis iterable, soseq[0] = itemraisedTypeErrorandseq[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), matchinglist.index.extend()relies onappend()to register each item.__iadd__delegates toextend().__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
TestContentSequenceintests/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.