test_builtin.test_import by youknowone · Pull Request #6546 · RustPython/RustPython
📝 Walkthrough
Walkthrough
The changes modify warning module behavior in two functions: get_warnings_attr now probes sys.modules for an already-imported warnings module instead of immediately returning None, and call_show_warning now instantiates warning objects by invoking the category constructor with the message before passing them to warnings.WarningMessage.
Changes
| Cohort / File(s) | Summary |
|---|---|
Warning module behavior refinement crates/vm/src/warn.rs |
Modified get_warnings_attr to check sys.modules for pre-imported warnings module when not importing eagerly. Updated call_show_warning to create warning instances by invoking category constructor with message, then pass instance to warning message constructor. |
Estimated code review effort
🎯 3 (Moderate) | ⏱️ ~25 minutes
Poem
🐰 Warnings now probe the module shelf,
Seeking what's already there itself,
Each category spawns its proper kin,
A CPython-like dance begins,
Where instances flow, not strings so thin! 🌟
Pre-merge checks and finishing touches
❌ Failed checks (1 warning, 1 inconclusive)
| Check name | Status | Explanation | Resolution |
|---|---|---|---|
| Docstring Coverage | Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. | You can run @coderabbitai generate docstrings to improve docstring coverage. |
|
| Title check | ❓ Inconclusive | The title 'test_builtin.test_import' is vague and does not clearly convey what the actual changes are about. The PR modifies warning handling logic in warn.rs, but the title suggests it's only about a test file or import test. | Consider a more descriptive title like 'Fix warning message handling to match CPython behavior' or 'Improve warnings module detection and instantiation' to better reflect the actual changes made. |
✅ Passed checks (1 passed)
| Check name | Status | Explanation |
|---|---|---|
| Description Check | ✅ Passed | Check skipped - CodeRabbit’s high-level summary is enabled. |
✨ Finishing touches
- 📝 Generate docstrings
🧪 Generate unit tests (beta)
- Create PR with unit tests
- Post copyable unit tests in a comment
📜 Recent review details
Configuration used: Path: .coderabbit.yml
Review profile: CHILL
Plan: Pro
⛔ Files ignored due to path filters (6)
Lib/test/test_builtin.pyis excluded by!Lib/**Lib/test/test_import/__init__.pyis excluded by!Lib/**Lib/test/test_importlib/import_/test___package__.pyis excluded by!Lib/**Lib/test/test_importlib/import_/test_helpers.pyis excluded by!Lib/**Lib/test/test_importlib/import_/test_meta_path.pyis excluded by!Lib/**Lib/test/test_importlib/import_/test_path.pyis excluded by!Lib/**
📒 Files selected for processing (1)
crates/vm/src/warn.rs
🧰 Additional context used
📓 Path-based instructions (1)
**/*.rs
📄 CodeRabbit inference engine (.github/copilot-instructions.md)
**/*.rs: Follow the default rustfmt code style by runningcargo fmtto format Rust code
Always run clippy to lint Rust code (cargo clippy) before completing tasks and fix any warnings or lints introduced by changes
Follow Rust best practices for error handling and memory management
Use the macro system (pyclass,pymodule,pyfunction, etc.) when implementing Python functionality in Rust
Files:
crates/vm/src/warn.rs
🧠 Learnings (1)
📓 Common learnings
Learnt from: CR
Repo: RustPython/RustPython PR: 0
File: .github/copilot-instructions.md:0-0
Timestamp: 2025-11-29T12:17:28.606Z
Learning: Applies to Lib/test/**/*.py : When modifying test files in `Lib/test/`, use `unittest.skip("TODO: RustPython <reason>")` or `unittest.expectedFailure` with `# TODO: RUSTPYTHON <reason>` comment to mark unsupported features
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (10)
- GitHub Check: Run rust tests (windows-latest)
- GitHub Check: Run snippets and cpython tests (macos-latest)
- GitHub Check: Run snippets and cpython tests (ubuntu-latest)
- GitHub Check: Run snippets and cpython tests (windows-latest)
- GitHub Check: Run rust tests (ubuntu-latest)
- GitHub Check: Run rust tests (macos-latest)
- GitHub Check: Check Rust code with clippy
- GitHub Check: Ensure compilation on various targets
- GitHub Check: Run tests under miri
- GitHub Check: Check the WASM package and demo
🔇 Additional comments (2)
crates/vm/src/warn.rs (2)
70-78: LGTM - Properly mirrors CPython'sPyImport_GetModulebehavior.The change correctly probes
sys.modulesfor an already-imported warnings module when eager import is not desired, rather than immediately returningNone. This is appropriate behavior during VM finalization or whentry_importis false, as it allows use of an already-loaded warnings module without triggering new imports.
330-336: LGTM - Correct instantiation of warning objects.This properly creates a warning instance by invoking the category constructor with the message, matching how CPython and the Python
warningsmodule work.WarningMessageexpects a warning instance (which is aBaseExceptionsubclass), not a raw string message.
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 @coderabbitai help to get the list of available commands and usage tips.