Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion monai/data/image_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,18 @@ def resolve_writer(ext_name, error_if_not_found=True) -> Sequence:
except Exception: # other writer init errors indicating it exists
avail_writers.append(_writer)
if not avail_writers and error_if_not_found:
raise OptionalImportError(f"No ImageWriter backend found for {fmt}.")
hints = {
"png": "pip install pillow",
"jpg": "pip install pillow",
"jpeg": "pip install pillow",
"nii": "pip install nibabel",
"nii.gz": "pip install nibabel",
"dcm": "pip install itk",
}
hint = hints.get(fmt.lstrip(".").lower(), "pip install pillow or pip install nibabel")
raise OptionalImportError(
f"No ImageWriter backend found for {fmt}. Try: {hint}."
)
writer_tuple = ensure_tuple(avail_writers)
SUPPORTED_WRITERS[fmt] = writer_tuple
return writer_tuple
Expand Down
2 changes: 1 addition & 1 deletion monai/transforms/io/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ def __init__(
try:
self.register(the_reader(*args, **kwargs))
except OptionalImportError:
warnings.warn(
raise ImportError(
f"required package for reader {_r} is not installed, or the version doesn't match requirement."
)
Comment on lines +213 to 215
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.

⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Verify exception chaining at the modified site.
rg -n -A4 -B2 'except OptionalImportError' monai/transforms/io/array.py

Repository: Project-MONAI/MONAI

Length of output: 934


Chain the exception to preserve traceback context.

The OptionalImportError should be chained when raising ImportError; otherwise debugging is hindered.

Suggested patch
                except OptionalImportError:
-                    raise ImportError(
-                        f"required package for reader {_r} is not installed, or the version doesn't match requirement."
-                    )
+                except OptionalImportError as err:
+                    raise ImportError(
+                        f"required package for reader {_r} is not installed, or the version doesn't match requirement."
+                    ) from err
🧰 Tools
🪛 Ruff (0.15.12)

[warning] 213-215: Within an except clause, raise exceptions with raise ... from err or raise ... from None to distinguish them from errors in exception handling

(B904)

🤖 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 213 - 215, The raised ImportError
in the reader-loading block should preserve the original exception context:
replace the bare raise ImportError(...) with a chained raise using the caught
OptionalImportError (e.g., raise ImportError(f"required package for reader {_r}
...") from e). Update the raise in the block that references _r so the
ImportError is raised "from" the caught OptionalImportError to preserve the
traceback.

except TypeError: # the reader doesn't have the corresponding args/kwargs
Expand Down
Loading