Raise OptionalImportError when user-specified reader is not installed - #9039
Raise OptionalImportError when user-specified reader is not installed#9039vandhanaanatarajan-oss wants to merge 3 commits into
Conversation
…fixes Project-MONAI#7437) Signed-off-by: vandhanaanatarajan-oss <vandhanaanatarajan@gmail.com>
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (2)
🚧 Files skipped from review as they are similar to previous changes (2)
📝 WalkthroughWalkthrough
Estimated code review effort: 2 (Simple) | ~10 minutes 🚥 Pre-merge checks | ✅ 3 | ❌ 2❌ Failed checks (2 warnings)
✅ Passed checks (3 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
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 |
There was a problem hiding this comment.
🧹 Nitpick comments (2)
monai/transforms/io/array.py (1)
212-215: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winDocument the new constructor exception.
LoadImage.__init__now raisesOptionalImportErrorwhen an explicitly selected reader lacks its dependency. Add a Google-styleRaisessection to the constructor docstring.Suggested documentation update
Args: kwargs: additional parameters for reader if providing a reader name. + Raises: + OptionalImportError: If an explicitly selected reader dependency is unavailable. + Note:As per path instructions, Python definitions must use Google-style docstrings that document raised exceptions.
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@monai/transforms/io/array.py` around lines 212 - 215, Update the LoadImage.__init__ docstring to include a Google-style Raises section documenting OptionalImportError when an explicitly selected reader’s required dependency is unavailable or incompatible; leave the constructor behavior unchanged.Source: Path instructions
tests/transforms/test_load_image.py (1)
225-231: 🎯 Functional Correctness | 🔵 Trivial | ⚡ Quick winAssert the preserved exception cause.
Line [230] checks only the exception type. The test would still pass if Line [215] stopped chaining the original error. Store the mocked exception, capture
assertRaises, and assertcontext.exception.__cause__ is original_error.Suggested test update
def test_reader_not_installed_raises(self): + """Verify that an unavailable explicit reader raises OptionalImportError.""" from unittest.mock import patch from monai.utils import OptionalImportError - with patch("monai.data.ITKReader", side_effect=OptionalImportError("itk not installed")): - with self.assertRaises(OptionalImportError): + original_error = OptionalImportError("itk not installed") + with patch("monai.data.ITKReader", side_effect=original_error): + with self.assertRaises(OptionalImportError) as context: LoadImage(reader="ITKReader") + self.assertIs(context.exception.__cause__, original_error)🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@tests/transforms/test_load_image.py` around lines 225 - 231, Update test_reader_not_installed_raises to store the OptionalImportError instance passed as the ITKReader patch side effect, capture the assertRaises context, and assert that context.exception.__cause__ is the same original error. Keep the existing reader setup and exception-type assertion intact.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@monai/transforms/io/array.py`:
- Around line 212-215: Update the LoadImage.__init__ docstring to include a
Google-style Raises section documenting OptionalImportError when an explicitly
selected reader’s required dependency is unavailable or incompatible; leave the
constructor behavior unchanged.
In `@tests/transforms/test_load_image.py`:
- Around line 225-231: Update test_reader_not_installed_raises to store the
OptionalImportError instance passed as the ITKReader patch side effect, capture
the assertRaises context, and assert that context.exception.__cause__ is the
same original error. Keep the existing reader setup and exception-type assertion
intact.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro Plus
Run ID: c2b1874d-064e-4f50-8a7e-c5e6c8552e2e
📒 Files selected for processing (2)
monai/transforms/io/array.pytests/transforms/test_load_image.py
… chain in test Signed-off-by: vandhanaanatarajan-oss <vandhanaanatarajan@gmail.com>
for more information, see https://pre-commit.ci
(fixes #7437)
Fixes
Description
A few sentences describing the changes proposed in this pull request.
Types of changes
./runtests.sh -f -u --net --coverage../runtests.sh --quick --unittests --disttests.make htmlcommand in thedocs/folder.