Repository navigation
Fix malformed exception messages and trailing context item slicing - #507
Merged
CPBridge merged 1 commit intoNov 4, 2026
Merged
Conversation
Several exceptions in the SR and KOS modules were raised with messages
that were never formatted: a missing f-prefix left literal placeholders
such as "{dataset.NumberOfFrames}" in the text, a stray comma turned the
TCOORD parameter check message into a tuple, and the context accessors
referenced an undefined "{item.meaning}" placeholder.
The observer and subject context accessors of MeasurementReport and
KeyObjectSelection also used -1 as the end of the slice for the last
matching context, which silently excluded the final item of the root
content sequence from the identifying attributes.
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 #506
Four exceptions in the SR and KOS modules raise with a message that was never formatted (a literal
{dataset.NumberOfFrames}, or a tuple because of a stray comma), and the context accessors that share those code paths drop the last root item. Details and reproductions are in the issue; the changes are:Malformed exception messages
highdicom.sr.content._check_frame_numbers_valid_for_dataset(used bySourceImageForMeasurement.from_source_image,SourceImageForRegion.from_source_image,SourceImageForSegmentation.from_source_imageandSourceImageForMeasurementGroup.from_source_image): the second line of the message was missing itsfprefix, so an invalid frame number produced5 is an invalid frame number for the given dataset with {dataset.NumberOfFrames} frames.highdicom.sr.TcoordContentItem: a stray comma in theTypeErrorintroduced in Add check on tcoord input parameters #499 passed the message as two arguments, sostr(exc)was the repr of a tuple rather than a sentence. The now unreachableelse: raise ValueError(...)branch below it is removed.highdicom.content.IssuerOfIdentifier.from_dataset: bothValueErrormessages were split by a stray comma into two arguments in the same way.MeasurementReport.get_observer_contexts,MeasurementReport.get_subject_contexts,KeyObjectSelection.get_observer_contextsandSubjectContext.subject_class_specific_context: the "Unexpected observer type"/"Unexpected subject class" messages had nofprefix and referred to a non-existentitem.meaning; they now report the meaning of the offending code (item.value.meaning/self.subject_class.meaning).Trailing context item slicing
MeasurementReport.get_observer_contexts,MeasurementReport.get_subject_contextsandKeyObjectSelection.get_observer_contextslocate eachObserver Type/Subject Classitem and pass the root items up to the next one to the correspondingfrom_sequence. For the last match, the slice end was-1, which excludes the final item of the root content sequence. If the last root item belongs to that context (for example aDevice Observer UID, which is required), the attribute was silently lost or the accessor raisedTypeError: DeviceObserverIdentifyingAttributes.__init__() missing 1 required positional argument: 'uid'. The slice end is nowNone.Tests
tests/test_sr.py: the two TCOORD tests now match the full message;test_from_source_image_with_invalid_referenced_frameschecks the formatted frame count; new tests for the trailing-attribute case and the unexpected observer type / subject class messages onMeasurementReport, and forSubjectContext.subject_class_specific_context.tests/test_ko.py: new tests for the trailing-attribute case and the unexpected observer type message onKeyObjectSelection.tests/test_content.py: new tests for bothIssuerOfIdentifier.from_dataseterror messages.All new tests fail on
masterand pass with this change.pytest tests/test_sr.py tests/test_ko.py tests/test_content.py(477 passed) andflake8are clean.Note: this touches
src/highdicom/sr/templates.py, which #407 proposes to split into a package, so one of the two will need a small rebase.