Skip to content

Fix malformed exception messages and trailing context item slicing - #507

Merged
CPBridge merged 1 commit into
ImagingDataCommons:masterfrom
DG47:bugfix/exception_messages
Nov 4, 2026
Merged

CPBridge merged 1 commit into
ImagingDataCommons:masterfrom
DG47:bugfix/exception_messages

Conversation

@DG47

@DG47 DG47 commented Nov 4, 2026

Copy link
Copy Markdown
Contributor

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 by SourceImageForMeasurement.from_source_image, SourceImageForRegion.from_source_image, SourceImageForSegmentation.from_source_image and SourceImageForMeasurementGroup.from_source_image): the second line of the message was missing its f prefix, so an invalid frame number produced 5 is an invalid frame number for the given dataset with {dataset.NumberOfFrames} frames.
  • highdicom.sr.TcoordContentItem: a stray comma in the TypeError introduced in Add check on tcoord input parameters #499 passed the message as two arguments, so str(exc) was the repr of a tuple rather than a sentence. The now unreachable else: raise ValueError(...) branch below it is removed.
  • highdicom.content.IssuerOfIdentifier.from_dataset: both ValueError messages were split by a stray comma into two arguments in the same way.
  • MeasurementReport.get_observer_contexts, MeasurementReport.get_subject_contexts, KeyObjectSelection.get_observer_contexts and SubjectContext.subject_class_specific_context: the "Unexpected observer type"/"Unexpected subject class" messages had no f prefix and referred to a non-existent item.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_contexts and KeyObjectSelection.get_observer_contexts locate each Observer Type/Subject Class item and pass the root items up to the next one to the corresponding from_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 a Device Observer UID, which is required), the attribute was silently lost or the accessor raised TypeError: DeviceObserverIdentifyingAttributes.__init__() missing 1 required positional argument: 'uid'. The slice end is now None.

Tests

  • tests/test_sr.py: the two TCOORD tests now match the full message; test_from_source_image_with_invalid_referenced_frames checks the formatted frame count; new tests for the trailing-attribute case and the unexpected observer type / subject class messages on MeasurementReport, and for SubjectContext.subject_class_specific_context.
  • tests/test_ko.py: new tests for the trailing-attribute case and the unexpected observer type message on KeyObjectSelection.
  • tests/test_content.py: new tests for both IssuerOfIdentifier.from_dataset error messages.

All new tests fail on master and pass with this change. pytest tests/test_sr.py tests/test_ko.py tests/test_content.py (477 passed) and flake8 are 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.

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.

@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.

Looks good, thanks

@CPBridge
CPBridge merged commit 28b6a35 into ImagingDataCommons:master Nov 4, 2026
13 checks passed
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.

Malformed exception messages in SR/KOS modules and last root item excluded by context accessors

2 participants