Skip to content
14 changes: 7 additions & 7 deletions monai/engines/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,8 +219,8 @@ def __call__(
`kwargs` supports other args for `Tensor.to()` API.
"""
image, label = default_prepare_batch(batchdata, device, non_blocking, **kwargs)
args_ = []
kwargs_ = {}
args_: tuple = ()
kwargs_: dict = {}

def _get_data(key: str) -> torch.Tensor:
data = batchdata[key]
Expand All @@ -231,13 +231,13 @@ def _get_data(key: str) -> torch.Tensor:
return data

if isinstance(self.extra_keys, (str, list, tuple)):
for k in ensure_tuple(self.extra_keys):
args_.append(_get_data(k))
args_ = tuple(_get_data(k) for k in ensure_tuple(self.extra_keys))

elif isinstance(self.extra_keys, dict):
for k, v in self.extra_keys.items():
kwargs_.update({k: _get_data(v)})
kwargs_ = {k: _get_data(v) for k, v in self.extra_keys.items()}


return cast(torch.Tensor, image), cast(torch.Tensor, label), tuple(args_), kwargs_
return cast(torch.Tensor, image), cast(torch.Tensor, label), args_, kwargs_
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 | 🔴 Critical

Broken indentation — file fails to import.

Lines 237 and 240 are over-indented, triggering IndentationError: unindent does not match any outer indentation level (confirmed by Ruff and the premerge/premerge-min pipelines). Beyond the syntax error, the return at line 240 sits at the elif block's indentation, so once parsed it would only execute when extra_keys is a dict — the str/list/tuple branch would fall through with no return.

🔧 Proposed fix
         if isinstance(self.extra_keys, (str, list, tuple)):
             args_ = tuple(_get_data(k) for k in ensure_tuple(self.extra_keys))
-
         elif isinstance(self.extra_keys, dict):
-                        kwargs_ = {k: _get_data(v) for k, v in self.extra_keys.items()}
-
+            kwargs_ = {k: _get_data(v) for k, v in self.extra_keys.items()}

-                return cast(torch.Tensor, image), cast(torch.Tensor, label), args_, kwargs_
+        return cast(torch.Tensor, image), cast(torch.Tensor, label), args_, kwargs_
🧰 Tools
🪛 GitHub Actions: premerge

[error] 240-240: Python import failed with IndentationError: 'unindent does not match any outer indentation level' at line 240.

🪛 GitHub Actions: premerge-min

[error] 240-240: Python failed with IndentationError: "unindent does not match any outer indentation level" during import. Step: "python -c "import monai; monai.config.print_config()"".

🪛 Ruff (0.15.10)

[warning] 240-240: unindent does not match any outer indentation level

(invalid-syntax)

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@monai/engines/utils.py` around lines 236 - 240, The indentation around the
branch that handles self.extra_keys is wrong: fix the over-indented lines so the
elif isinstance(self.extra_keys, dict): and its kwargs_ = {k: _get_data(v) ...}
are aligned with the other conditional branches and move the return statement
(return cast(torch.Tensor, image), cast(torch.Tensor, label), args_, kwargs_)
out of the elif-block to the outer scope of the function so it always executes;
ensure symbols referenced include self.extra_keys, _get_data, kwargs_, args_,
image and label so the dict, list/str/tuple and default branches all return
consistently.



class DiffusionPrepareBatch(PrepareBatch):
Expand Down
Loading