Skip to content

Restore pydicom reading validation mode after file validation - #303

Merged
mrbean-bremen merged 3 commits into
pydicom:mainfrom
jdotc1:fix/restore-pydicom-reading-mode-20261021
Oct 22, 2026
Merged

mrbean-bremen merged 3 commits into
pydicom:mainfrom
jdotc1:fix/restore-pydicom-reading-mode-20261021

Conversation

@jdotc1

@jdotc1 jdotc1 commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

Describe the changes

DicomFileValidator.validate_file permanently assigns pydicom's process-wide reading_validation_mode to IGNORE, affecting subsequent pydicom users in the same process even when validation raises.

Use a small set_validation_mode(mode) context manager to save the incoming reading mode and restore it in finally. Wrap file reading, dataset validation, and result handling in with set_validation_mode(config.IGNORE):, as requested in review. Keep IODValidator construction and validation in the inner try statement's else suite so IGNORE remains active for deferred value conversion and result handling, while preserving the original narrow InvalidDicomError catch around dcmread.

Related historical context: #102 (already closed).

Validation

Tested on Linux with Python 3.12.14 and pydicom 3.0.2.

  • Before the fix: 14 failed, 7 passed, 7 deselected, exit 1 with the final tests on unmodified production code. IGNORE starts pass; WARN/RAISE starts expose leaked IGNORE after each exit path.
  • After the fix: 21 passed, 7 deselected. All IGNORE/WARN/RAISE settings are restored after normal completion, invalid files, read failures, IOD constructor failures, validation failures, and both result-handler failure paths.
  • The patch applied cleanly to the stated base and the focused tests passed again there.
  • All applicable pinned pre-commit hooks passed on the changed files.
  • Sphinx HTML documentation built with warnings treated as errors.
  • Additional macOS ARM64/Python 3.12.14 validation: genuine 2015b and 2026b standard files downloaded successfully; the two upstream command-line tests generated their JSON and passed. All seven previously fixture-blocked file-validator tests passed (21 previously validated mode-restoration cases deselected).
  • Full upstream platform CI remains outstanding.

Context-manager review update

  • Extracted the mode lifetime into set_validation_mode(mode) and adopted the requested with set_validation_mode(config.IGNORE): call site.
  • Added nine cases covering every incoming/temporary IGNORE, WARN, and RAISE pairing, nested exception restoration, and preservation of writing-validation mode.
  • The affected file-validator module passed all 37 tests on macOS ARM64/Python 3.12.14. After the lint-requested syntax adjustment to the new test, its nine parameterized cases passed again; the other 28 results were reused.
  • Applicable pre-commit checks passed. Black and Ruff were checked again after their requested changes; other successful hook results were reused.
  • The previous revision passed the upstream platform matrix; the updated commit requires fresh remote CI. Existing documentation receipts are retained because this update does not change documentation inputs.

Compatibility

pydicom's setting remains process-wide. Restoring it fixes leakage after a call but does not make concurrent pydicom reads or concurrent validations thread-safe. The patch explicitly documents this limitation and does not claim thread isolation.

Tasks

  • Unit tests added that reproduce the issue or prove feature is working
  • Fix added
  • Entry to release notes added
  • Local pre-commit checks pass on changed files
  • Documentation build succeeds
  • Focused regression tests pass
  • Pre-commit CI shows no errors
  • Full upstream unit-test matrix passes

The focused regression, formatting and documentation results are reused from the reviewed Linux preparation at the unchanged base. The standard-dependent checks above were completed separately on macOS; no full platform-matrix result is claimed.

@codecov-commenter

codecov-commenter commented Oct 21, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.42%. Comparing base (e8644e2) to head (c2846ae).
⚠️ Report is 227 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #303      +/-   ##
==========================================
+ Coverage   89.82%   90.42%   +0.59%     
==========================================
  Files          15       21       +6     
  Lines        1632     2130     +498     
==========================================
+ Hits         1466     1926     +460     
- Misses        166      204      +38     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

file_path=file_path,
).validate()
}
finally:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks! I generally prefer to have the logic for restoring the mode in a separate small context manager and use it like:

with set_validation_mode(config.IGNORE):
    # do stuff

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Implemented in daccc30. The restoration logic now lives in set_validation_mode(), and file validation uses with set_validation_mode(config.IGNORE). The tests cover restoration after normal completion, exceptions and nested contexts. Thanks for the suggestion.

"""Temporarily set pydicom's process-wide reading validation mode.

Restore the previous mode on exit, including when the body raises.
This does not change writing validation and is not thread-safe.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Probably better name it set_reading_validation_mode, so there is no need to clarify this in the comment.

@mrbean-bremen
mrbean-bremen merged commit d62a14c into pydicom:main Oct 22, 2026
18 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.

4 participants