Skip to content

Improve missing image writer dependency hints#8989

Open
NiketGirdhar22 wants to merge 1 commit into
Project-MONAI:devfrom
NiketGirdhar22:7980-image-writer-install-hints
Open

Improve missing image writer dependency hints#8989
NiketGirdhar22 wants to merge 1 commit into
Project-MONAI:devfrom
NiketGirdhar22:7980-image-writer-install-hints

Conversation

@NiketGirdhar22

@NiketGirdhar22 NiketGirdhar22 commented Jul 12, 2026

Copy link
Copy Markdown

Fixes #7980.

Description

Improve the error raised when resolve_writer cannot find an available image writer by including actionable installation commands for the actual candidate backends.

Rather than maintaining a separate extension-to-package table, this change records the missing dependency name on OptionalImportError in require_pkg. resolve_writer collects that information from its registered candidate writers and formats one or more installation hints. This keeps the guidance aligned with SUPPORTED_WRITERS, including wildcard formats and custom registrations that use require_pkg.

For example, saving PNG without Pillow now reports:

No ImageWriter backend found for png. Install `pillow` with `pip install pillow`.

This is a non-breaking, error-message-only enhancement.

I am aware of the existing proposals for #7980, particularly #8761, #8625, and #8240. This draft uses the package metadata approach previously suggested in the review of #8240 and is opened for maintainer guidance on whether to proceed independently or coordinate with an existing PR.

Changes

  • Preserve the missing package name through OptionalImportError.name.
  • Generate installation guidance from unavailable registered writers.
  • Translate the PIL import name to the installable pillow distribution.
  • Preserve the generic error when dependency metadata is unavailable.
  • Add tests for single, multiple, and unknown dependency cases.
  • Document the enhanced OptionalImportError behavior.

Types of changes

  • 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).
  • New tests added to cover the changes.
  • Integration tests passed locally by running ./runtests.sh -f -u --net --coverage.
  • Focused unit tests passed locally.
  • Black, isort, Ruff, compilation, and whitespace checks passed.
  • In-line docstrings updated.
  • Documentation pages updated.

Test results

  • tests.utils.test_require_pkg: 7 passed
  • tests.utils.test_optional_import: 12 passed
  • New writer dependency-hint tests: 3 passed

The complete tests.data.test_image_rw module requires optional image backends not installed in the minimal local environment. Its pre-existing NRRD assertion requires itk or nibabel; the new tests do not require those dependencies and pass independently.

Signed-off-by: NiketGirdhar22 <niketgirdhar2004@gmail.com>
@coderabbitai

coderabbitai Bot commented Jul 12, 2026

Copy link
Copy Markdown
Contributor

Important

Review skipped

No new commits to review since the last review.

⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: c1311bab-b1e6-4f72-9c59-39fb89aeb1d0

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
📝 Walkthrough

Walkthrough

OptionalImportError now records the missing package name when raised by require_pkg. Image writer resolution collects unavailable backend dependencies, maps module names to PyPI packages, and includes single or multiple pip install commands in error messages. Tests cover package-name propagation, single and multiple dependency hints, and fallback behavior when no package name is available.

Estimated code review effort: 3 (Moderate) | ~20 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the main change: improved missing image writer dependency hints.
Description check ✅ Passed The description matches the template and includes the issue, summary, change list, and test status.
Linked Issues check ✅ Passed The PR satisfies #7980 by surfacing installable package hints, including Pillow for PNG writer failures.
Out of Scope Changes check ✅ Passed The code and tests stay focused on error-message improvements and dependency hinting, with no clear unrelated changes.
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.

@NiketGirdhar22 NiketGirdhar22 marked this pull request as ready for review July 12, 2026 22:34
@NiketGirdhar22

Copy link
Copy Markdown
Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Jul 12, 2026

Copy link
Copy Markdown
Contributor
✅ Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

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

🧹 Nitpick comments (1)
monai/data/image_writer.py (1)

95-140: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Add Returns section to resolve_writer docstring.

The docstring now has Args and Raises but is missing a Returns section documenting the return type and value. As per coding guidelines, Google-style docstrings should describe each return value.

📝 Proposed docstring addition
         error_if_not_found: whether to raise an error if no suitable image writer is found.
             if True , raise an ``OptionalImportError``, otherwise return an empty tuple. Default is ``True``.

+    Returns:
+        A tuple of available ``ImageWriter`` classes matching the given extension.
+
     Raises:
         OptionalImportError: When no registered writer is available. If candidate writers require missing optional
             dependencies, the error includes commands for installing them.
🤖 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/data/image_writer.py` around lines 95 - 140, The resolve_writer
docstring is missing documentation for its return value. Add a Google-style
Returns section to resolve_writer describing that it returns a tuple of
available ImageWriter classes, or an empty tuple when error_if_not_found is
false and no writer is available.

Source: Path instructions

🤖 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/data/image_writer.py`:
- Around line 95-140: The resolve_writer docstring is missing documentation for
its return value. Add a Google-style Returns section to resolve_writer
describing that it returns a tuple of available ImageWriter classes, or an empty
tuple when error_if_not_found is false and no writer is available.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 28492799-bf75-4726-b58a-c3e68c762102

📥 Commits

Reviewing files that changed from the base of the PR and between fd12fdc and 94f317a.

📒 Files selected for processing (4)
  • monai/data/image_writer.py
  • monai/utils/module.py
  • tests/data/test_image_rw.py
  • tests/utils/test_require_pkg.py

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.

Print what package should be installed when suitable writer is missing

1 participant