fix: add missing stacklevel=2 to warnings.warn() in losses/ (Fixes #8929)#8930
fix: add missing stacklevel=2 to warnings.warn() in losses/ (Fixes #8929)#8930rtmalikian wants to merge 2 commits into
Conversation
Add stacklevel=2 to 28 warnings.warn() calls across 9 loss modules so that warnings correctly point to the user's calling code instead of MONAI internal code. Affected files: - dice.py (7 instances) - tversky.py (3 instances) - mcc_loss.py (3 instances) - focal_loss.py (3 instances) - hausdorff_loss.py (3 instances) - unified_focal_loss.py (3 instances) - spatial_mask.py (3 instances) - perceptual.py (2 instances) - adversarial_loss.py (1 instance) Also fixes a typo in perceptual.py: 'supp, ort' -> 'support'. Fixes Project-MONAI#8929 Signed-off-by: Raphael Malikian <rtmalikian@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 Run ID: 📒 Files selected for processing (2)
✅ Files skipped from review due to trivial changes (2)
📝 WalkthroughWalkthroughAll Estimated code review effort🎯 1 (Trivial) | ⏱️ ~3 minutes 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ 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/losses/perceptual.py (1)
129-131: 💤 Low valueConsider moving
stacklevelparameter to previous line for consistency.The
stacklevel=2parameter is on a separate line with a leading comma, inconsistent with line 115 in this file and most other warnings in this PR.Suggested formatting
warnings.warn( - f"Setting cache_dir to {cache_dir}, this may change the default cache dir for all torch.hub calls." - , stacklevel=2) + f"Setting cache_dir to {cache_dir}, this may change the default cache dir for all torch.hub calls.", + stacklevel=2)🤖 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/losses/perceptual.py` around lines 129 - 131, The stacklevel=2 parameter in the warnings.warn() call is on a separate line with a leading comma, which is inconsistent with the formatting used at line 115 and elsewhere in the codebase. Move the stacklevel=2 parameter to the same line as the f-string message by placing the comma at the end of the f-string and positioning stacklevel=2 on the same line with proper indentation to match the consistent formatting style used throughout the file.monai/losses/adversarial_loss.py (1)
130-133: 💤 Low valueConsider moving
stacklevelparameter to previous line for consistency.The
stacklevel=2parameter is on a separate line with a leading comma. Other files in this PR keep the parameter on the same line as the closing quote. Consistent formatting improves readability.Suggested formatting
warnings.warn( "Variable target_is_real has been set to False, but for_discriminator is set" - "to False. To optimise a generator, target_is_real must be set to True." - , stacklevel=2) + "to False. To optimise a generator, target_is_real must be set to True.", + stacklevel=2)🤖 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/losses/adversarial_loss.py` around lines 130 - 133, In the warnings.warn call in the adversarial_loss.py file, the stacklevel parameter is currently on a separate line with a leading comma. Move the stacklevel=2 parameter to the same line as the closing quote of the warning message string and adjust the formatting so the comma follows the string closure rather than appearing on a separate line. This will align with the formatting convention used elsewhere in the PR.
🤖 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/losses/adversarial_loss.py`:
- Around line 130-133: In the warnings.warn call in the adversarial_loss.py
file, the stacklevel parameter is currently on a separate line with a leading
comma. Move the stacklevel=2 parameter to the same line as the closing quote of
the warning message string and adjust the formatting so the comma follows the
string closure rather than appearing on a separate line. This will align with
the formatting convention used elsewhere in the PR.
In `@monai/losses/perceptual.py`:
- Around line 129-131: The stacklevel=2 parameter in the warnings.warn() call is
on a separate line with a leading comma, which is inconsistent with the
formatting used at line 115 and elsewhere in the codebase. Move the stacklevel=2
parameter to the same line as the f-string message by placing the comma at the
end of the f-string and positioning stacklevel=2 on the same line with proper
indentation to match the consistent formatting style used throughout the file.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: 2d1171ec-b7a8-49b8-b1cf-d02878e99e70
📒 Files selected for processing (9)
monai/losses/adversarial_loss.pymonai/losses/dice.pymonai/losses/focal_loss.pymonai/losses/hausdorff_loss.pymonai/losses/mcc_loss.pymonai/losses/perceptual.pymonai/losses/spatial_mask.pymonai/losses/tversky.pymonai/losses/unified_focal_loss.py
…rsarial_loss.py Address CodeRabbit review: move stacklevel parameter to same line as closing quote for consistent formatting across the PR. Signed-off-by: Raphael Malikian <rtmalikian@gmail.com>
Fixes #8929
Problem
28 calls to
warnings.warn()inmonai/losses/are missing thestacklevelparameter. Withoutstacklevel=2, warnings point to the MONAI internal code instead of the user's calling code, making it difficult for users to identify which part of their script triggered the warning.Solution
Added
stacklevel=2to all 28warnings.warn()calls across 9 loss modules:dice.py(7 instances)tversky.py(3 instances)mcc_loss.py(3 instances)focal_loss.py(3 instances)hausdorff_loss.py(3 instances)unified_focal_loss.py(3 instances)spatial_mask.py(3 instances)perceptual.py(2 instances)adversarial_loss.py(1 instance)Also fixes a typo in
perceptual.py:"supp, ort"→"support"(carried from #8924).Verification
Changelog
Files Changed
monai/losses/dice.py— 7 warnings.warn() calls updatedmonai/losses/tversky.py— 3 calls updatedmonai/losses/mcc_loss.py— 3 calls updatedmonai/losses/focal_loss.py— 3 calls updatedmonai/losses/hausdorff_loss.py— 3 calls updatedmonai/losses/unified_focal_loss.py— 3 calls updatedmonai/losses/spatial_mask.py— 3 calls updatedmonai/losses/perceptual.py— 2 calls updated + typo fixmonai/losses/adversarial_loss.py— 1 call updatedVerification
About the Author: Raphael Malikian — Clinical AI Solutions Architect. I specialise in building and fixing AI/ML systems for healthcare, including vector databases, RAG pipelines, and clinical NLP. If you need help with your project or think I can add value to your organisation, feel free to reach out — I'd love to connect.
📧 rtmalikian@gmail.com
🔗 GitHub: https://github.com/rtmalikian
🔗 LinkedIn: http://www.linkedin.com/in/raphael-t-malikian-mbbs-bsc-hons-71075436a
Disclosure: This code was developed with assistance from MiMo-v2.5-Pro (Xiaomi) via Hermes Agent (Nous Research). All changes were reviewed, tested against the actual codebase, and verified for correctness.