Add dependency hints to image writer errors
Fixes #7980 .
Description
Add dependency hints to image writer errors
Types of changes
- [x] Non-breaking change (fix or new feature that would not break existing functionality).
- [ ] Breaking change (fix or new feature that would cause existing functionality to change).
- [x] New tests added to cover the changes.
- [ ] Integration tests passed locally by running
./runtests.sh -f -u --net --coverage. - [x] Quick tests passed locally by running
./runtests.sh --quick --unittests --disttests. - [ ] In-line docstrings updated.
- [ ] Documentation updated, tested
make htmlcommand in thedocs/folder.
Walkthrough
A dependency hints system is introduced to enhance error messages when image writers fail to load due to missing external packages. A new module-level dictionary WRITER_DEPENDENCY_HINTS maps writer classes to package names and installation commands. The resolve_writer function collects these hints when writers fail to import and constructs detailed error messages recommending specific package installations based on the requested image format. A corresponding test verifies error messages properly include the dependency hints.
Estimated code review effort
๐ฏ 2 (Simple) | โฑ๏ธ ~10 minutes
-
Dictionary mappings: Verify
WRITER_DEPENDENCY_HINTScorrectly associates each writer class (ITKWriter, NibabelWriter, PILWriter) with the correct package names and installation commands - Error message construction: Check logic that differentiates between single vs. multiple hints and formats the final error message appropriately
- Test coverage: Confirm the test properly validates that hints appear in error messages and cleanup is handled correctly
Pre-merge checks and finishing touches
โ Passed checks (4 passed)
| Check name | Status | Explanation |
|---|---|---|
| Title check | โ Passed | Title clearly summarizes the main change: adding dependency hints to image writer errors. |
| Description check | โ Passed | Description covers the fix reference, main change, and marks relevant checkbox items, though some optional fields remain unchecked. |
| Linked Issues check | โ Passed | Changes directly address issue #7980: error messages now include actionable dependency hints (package names and install commands) when writers are missing. |
| Out of Scope Changes check | โ Passed | All changes are scoped to the stated objective: adding dependency hints to image writer errors and testing the feature. |
โจ Finishing touches
- [ ] ๐ Generate docstrings
๐งช Generate unit tests (beta)
- [ ] Create PR with unit tests
- [ ] Post copyable unit tests in a comment
๐ Recent review details
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Cache: Disabled due to data retention organization setting
Knowledge base: Disabled due to Reviews -> Disable Knowledge Base setting
๐ฅ Commits
Reviewing files that changed from the base of the PR and between e72145ce852e1b59e2dbf6d57d3360979c2f9407 and 6bbc5348ab7678f9f77ae16b4ecd20b82bcf14e5.
๐ Files selected for processing (2)
-
monai/data/image_writer.py(3 hunks) -
tests/data/test_image_rw.py(2 hunks)
๐งฐ Additional context used
๐ Path-based instructions (1)
**/*.py
โ๏ธ CodeRabbit configuration file
Review the Python code for quality and correctness. Ensure variable names adhere to PEP8 style guides, are sensible and informative in regards to their function, though permitting simple names for loop and comprehension variables. Ensure routine names are meaningful in regards to their function and use verbs, adjectives, and nouns in a semantically appropriate way. Docstrings should be present for all definition which describe each variable, return value, and raised exception in the appropriate section of the Google-style of docstrings. Examine code for logical error or inconsistencies, and suggest what may be changed to addressed these. Suggest any enhancements for code improving efficiency, maintainability, comprehensibility, and correctness. Ensure new or modified definitions will be covered by existing or new unit tests.
Files:
-
tests/data/test_image_rw.py -
monai/data/image_writer.py
๐ช Ruff (0.14.3)
monai/data/image_writer.py
121-121: Do not catch blind exception: Exception
(BLE001)
137-137: Avoid specifying long messages outside the exception class
(TRY003)
โฐ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (19)
- GitHub Check: min-dep-pytorch (2.7.1)
- GitHub Check: min-dep-pytorch (2.8.0)
- GitHub Check: min-dep-pytorch (2.6.0)
- GitHub Check: min-dep-py3 (3.11)
- GitHub Check: min-dep-py3 (3.12)
- GitHub Check: min-dep-os (ubuntu-latest)
- GitHub Check: min-dep-py3 (3.10)
- GitHub Check: min-dep-pytorch (2.5.1)
- GitHub Check: min-dep-os (macOS-latest)
- GitHub Check: min-dep-py3 (3.9)
- GitHub Check: min-dep-os (windows-latest)
- GitHub Check: flake8-py3 (pytype)
- GitHub Check: flake8-py3 (codeformat)
- GitHub Check: quick-py3 (windows-latest)
- GitHub Check: build-docs
- GitHub Check: flake8-py3 (mypy)
- GitHub Check: quick-py3 (ubuntu-latest)
- GitHub Check: quick-py3 (macOS-latest)
- GitHub Check: packaging
๐ Additional comments (2)
tests/data/test_image_rw.py (1)
154-178: Hint coverage looks solid.
Thanks for validating the OptionalImportError surfaces both the package name and install command.monai/data/image_writer.py (1)
110-137: Error hint messaging reads well.
The sorted hint aggregation keeps the OptionalImportError actionable without altering existing flows.
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
Comment @coderabbitai help to get the list of available commands and usage tips.