Skip to content

Raise OptionalImportError for unavailable explicit image readers#9006

Open
minsuking wants to merge 2 commits into
Project-MONAI:devfrom
minsuking:7437-raise-unavailable-reader
Open

Raise OptionalImportError for unavailable explicit image readers#9006
minsuking wants to merge 2 commits into
Project-MONAI:devfrom
minsuking:7437-raise-unavailable-reader

Conversation

@minsuking

Copy link
Copy Markdown

Description

Fixes #7437.

When LoadImage is given an explicit reader string whose optional dependency is unavailable, the original OptionalImportError is now propagated instead of being converted into a warning and silently falling back to another registered reader.

Automatic reader selection with reader=None remains unchanged.

Implementation

  • Propagate OptionalImportError for explicitly requested unavailable string readers.
  • Add an environment-independent regression test using mock readers.
  • Update reader initialization tests to reflect the new explicit-reader behavior when optional dependencies are unavailable.

Compatibility

This is an intentional behavior change for explicitly requested unavailable readers.

The following behavior remains unchanged:

  • automatic reader selection with reader=None
  • default registration skipping unavailable optional readers
  • runtime fallback when an installed reader cannot read a file
  • public APIs and signatures

This PR does not redesign explicit tuple/list reader semantics.

Validation

Executed locally:

  • python -m tests.transforms.test_load_image
  • python -m tests.transforms.test_load_imaged
  • python -m tests.data.test_init_reader
  • Ruff

All executed tests passed.

Some optional-backend tests were skipped as expected in the current environment.

Signed-off-by: Minsu Kim <minsu.kim08@gmail.com>
@coderabbitai

coderabbitai Bot commented Jul 21, 2026

Copy link
Copy Markdown
Contributor

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 7e61fa24-d9fb-468f-b1e6-7f6a1959f1eb

📥 Commits

Reviewing files that changed from the base of the PR and between 782e9af and eaec3f1.

📒 Files selected for processing (1)
  • tests/transforms/test_load_image.py
🚧 Files skipped from review as they are similar to previous changes (1)
  • tests/transforms/test_load_image.py

📝 Walkthrough

Walkthrough

LoadImage now re-raises OptionalImportError when an explicitly selected reader cannot be constructed. Tests account for optional backend availability and verify that missing dependencies raise without warnings or fallback reader use.

Estimated code review effort: 2 (Simple) | ~10 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly states the main behavior change for explicit readers.
Description check ✅ Passed The description follows the template and covers behavior, implementation, compatibility, and validation.
Linked Issues check ✅ Passed The change matches #7437 by raising on missing explicit readers and adding tests.
Out of Scope Changes check ✅ Passed The changes stay focused on the reported reader error-handling fix and its tests.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 1

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

Inline comments:
In `@tests/transforms/test_load_image.py`:
- Around line 231-233: Update the warnings.catch_warnings context in the
LoadImage missing-reader test to call warnings.simplefilter("always") before
constructing LoadImage, ensuring the emitted warning is captured regardless of
existing filters while preserving the OptionalImportError assertion.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 9f59df5e-f5b9-431f-93ac-2088870e0dfa

📥 Commits

Reviewing files that changed from the base of the PR and between 8885fcb and 782e9af.

📒 Files selected for processing (3)
  • monai/transforms/io/array.py
  • tests/data/test_init_reader.py
  • tests/transforms/test_load_image.py

Comment thread tests/transforms/test_load_image.py
Signed-off-by: Minsu Kim <minsu.kim08@gmail.com>
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.

Raise the exception when LoadImage has a reader specified but it is not installed

1 participant