Adding Temporary Directory Routine - #9029
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughWalkthroughAdds the exported Estimated code review effort: 3 (Moderate) | ~20 minutes 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ 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 |
…ply.github.com> I, Eric Kerfoot <17726042+ericspod@users.noreply.github.com>, hereby add my Signed-off-by to this commit: a95e44e I, Eric Kerfoot <17726042+ericspod@users.noreply.github.com>, hereby add my Signed-off-by to this commit: d9cd2d1 I, Eric Kerfoot <17726042+ericspod@users.noreply.github.com>, hereby add my Signed-off-by to this commit: 5e7e430 Signed-off-by: Eric Kerfoot <17726042+ericspod@users.noreply.github.com>
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (2)
monai/apps/utils.py (2)
46-54: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winSort
__all__to satisfy RUF022.Ruff reports this export list is unsorted.
🤖 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/apps/utils.py` around lines 46 - 54, Sort the entries in the __all__ export list alphabetically to satisfy RUF022, preserving all existing exported symbols and their values.Source: Linters/SAST tools
436-452: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winDocument filesystem exceptions.
Add a Google-style
Raisessection forOSErrorfrom temporary-directory creation oros.makedirs.🤖 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/apps/utils.py` around lines 436 - 452, Update the create_temp_dir docstring to include a Google-style Raises section documenting OSError from temporary-directory creation and os.makedirs, without changing the function’s behavior or existing documentation.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.
Inline comments:
In `@monai/apps/utils.py`:
- Around line 461-462: Update the delete_on_finalise cleanup registration in
create_temp_dir to make shutil.rmtree ignore missing directories, ensuring
repeated callbacks are idempotent and exit cleanup does not raise
FileNotFoundError.
---
Nitpick comments:
In `@monai/apps/utils.py`:
- Around line 46-54: Sort the entries in the __all__ export list alphabetically
to satisfy RUF022, preserving all existing exported symbols and their values.
- Around line 436-452: Update the create_temp_dir docstring to include a
Google-style Raises section documenting OSError from temporary-directory
creation and os.makedirs, without changing the function’s behavior or existing
documentation.
🪄 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 Plus
Run ID: f394f467-bd08-4818-9576-ff8eeb031547
📒 Files selected for processing (3)
monai/apps/__init__.pymonai/apps/utils.pytests/apps/test_create_temp_dir.py
There was a problem hiding this comment.
🧹 Nitpick comments (3)
tests/apps/test_create_temp_dir.py (1)
60-80: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low valueUse a descriptive file-handle name.
Rename
otofile_handleor similar.🤖 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 `@tests/apps/test_create_temp_dir.py` around lines 60 - 80, In test_finalisation, rename the file handle variable o to a descriptive name such as file_handle, and update its write call accordingly.Source: Path instructions
monai/apps/utils.py (2)
46-54: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low valueSort
__all__to satisfy Ruff.
create_temp_dirshould be placed alphabetically with the other exports.🤖 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/apps/utils.py` around lines 46 - 54, Sort the export names in __all__ alphabetically, moving create_temp_dir into its correct position while preserving all existing exports.Source: Linters/SAST tools
437-452: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winDocument propagated filesystem errors.
Add a Google-style
Raisessection forOSErrorfrom directory creation.🤖 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/apps/utils.py` around lines 437 - 452, Update the docstring for the temporary-directory function to add a Google-style Raises section documenting that directory creation can raise OSError. Keep the existing arguments and return documentation unchanged.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/apps/utils.py`:
- Around line 46-54: Sort the export names in __all__ alphabetically, moving
create_temp_dir into its correct position while preserving all existing exports.
- Around line 437-452: Update the docstring for the temporary-directory function
to add a Google-style Raises section documenting that directory creation can
raise OSError. Keep the existing arguments and return documentation unchanged.
In `@tests/apps/test_create_temp_dir.py`:
- Around line 60-80: In test_finalisation, rename the file handle variable o to
a descriptive name such as file_handle, and update its write call accordingly.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro Plus
Run ID: d3aa5766-d56d-47e5-b8be-dea4293006fa
📒 Files selected for processing (3)
monai/apps/__init__.pymonai/apps/utils.pytests/apps/test_create_temp_dir.py
🚧 Files skipped from review as they are similar to previous changes (1)
- monai/apps/init.py
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> Signed-off-by: Eric Kerfoot <17726042+ericspod@users.noreply.github.com>
| self.assertEqual(test_dir, selected_dir) | ||
|
|
||
| def test_finalisation(self): | ||
| """Test the temporary directory is deleted by finalisation using a subprocess.""" |
There was a problem hiding this comment.
This docstring still says "using a subprocess," but the earlier subprocess-based check was replaced with a monkeypatched atexit.register that the test then calls manually (see lines 64-65, 77-78). That proves shutil.rmtree works, but not that create_temp_dir is actually wired up correctly with the real atexit module or that cleanup fires on process exit. Could you fix the docstring to describe what's actually tested, and/or restore a lightweight subprocess smoke test (guarded with skip_if_quick like before) so there's still one real end-to-end check?
|
|
||
|
|
||
| class TestCreateTempDir(unittest.TestCase): | ||
| def test_basic_use(self): |
There was a problem hiding this comment.
test_basic_use exercises the implicit auto-cleanup path (no directory, no MONAI_DATA_DIRECTORY), where delete_on_finalise gets forced to True in monai/apps/utils.py. That's the exact path every notebook migrating to this helper will hit, but this test never asserts atexit.register was called, so a regression here would go unnoticed. Could you patch atexit.register in this test too and assert it gets called when no directory or env var is provided?
| extractall(filepath=filename, output_dir=output_dir, file_type=file_type, has_base=has_base) | ||
|
|
||
|
|
||
| def create_temp_dir(directory: str | None = None, delete_on_finalise: bool = False) -> str: |
There was a problem hiding this comment.
Every other path-like parameter in this file (check_hash, download_and_extract, etc.) is typed PathLike, but this one is str | None. Minor consistency nit: directory: PathLike | None = None.
garciadias
left a comment
There was a problem hiding this comment.
create_temp_dir isn't added to docs/source/apps.rst alongside check_hash, download_url, extractall, and download_and_extract, so it won't show up in the built API docs. Consider adding an autofunction entry for it.
garciadias
left a comment
There was a problem hiding this comment.
Two Major findings around test rigor for the new create_temp_dir utility: the finalisation test's docstring is stale (still claims subprocess-based verification, but now just mocks atexit.register and calls it manually), and the more common implicit auto-cleanup path isn't asserted at all. The implementation itself looks correct and low-risk. Details in the inline comments.
Description
This adds a temporary directory creation routine to replace the following cell in many tutorial notebooks:
This should be functionally equivalent, but can also add a finaliser function to delete the directory when the notebook closes. The notebooks can then be updated to reduce the amount of code.
Types of changes
./runtests.sh -f -u --net --coverage../runtests.sh --quick --unittests --disttests.make htmlcommand in thedocs/folder.