-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Fix PyNumber::boolean #6111
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix PyNumber::boolean #6111
Conversation
WalkthroughFlips the boolean truth-value functions in PyNumberMethods for int, float, and complex to return true for non-zero values and false for zero. No other numeric operations or APIs are changed. Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Poem
Tip 🔌 Remote MCP (Model Context Protocol) integration is now available!Pro plan users can now connect to remote MCP servers from the Integrations page. Connect with popular remote MCPs such as Notion and Linear to add more context to your reviews and chats. ✨ Finishing Touches
🧪 Generate unit tests
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. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. CodeRabbit Commands (Invoked using PR/Issue comments)Type Other keywords and placeholders
Status, Documentation and Community
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🧹 Nitpick comments (2)
vm/src/builtins/int.rs (1)
776-776: Add regression tests for numeric truthinessWe’ve now wired up
booleanfor int (int.rs 776), float (float.rs 593), and complex (complex.rs 495), but there aren’t any dedicated tests locking in the exact truthiness semantics. Please add a small parametrized test covering:
bool(0) is False,bool(1) is True,bool(-1) is Truebool(0.0) is False,bool(-0.0) is False,bool(float('nan')) is Truebool(0+0j) is False,bool(1+0j) is True,bool(0+1j) is TrueSuggested placement: your existing builtins test suite (e.g. under
vm/src/builtins/tests/or the Python‐level tests directory). Happy to draft a pytest-style parameterized snippet if that’s helpful.vm/src/builtins/float.rs (1)
593-593: Optional: make style consistent with__bool__.
__bool__usesself.value != 0.0while the slot uses!is_zero(). Consider choosing one form for both (no behavior change).Apply if you prefer to mirror
__bool__:- boolean: Some(|num, _vm| Ok(!PyFloat::number_downcast(num).value.is_zero())), + boolean: Some(|num, _vm| Ok(PyFloat::number_downcast(num).value != 0.0)),
📜 Review details
Configuration used: Path: .coderabbit.yml
Review profile: CHILL
Plan: Pro
💡 Knowledge Base configuration:
- MCP integration is disabled by default for public repositories
- Jira integration is disabled by default for public repositories
- Linear integration is disabled by default for public repositories
You can enable these sources in your CodeRabbit configuration.
📒 Files selected for processing (3)
vm/src/builtins/complex.rs(1 hunks)vm/src/builtins/float.rs(1 hunks)vm/src/builtins/int.rs(1 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
**/*.rs
📄 CodeRabbit inference engine (.github/copilot-instructions.md)
**/*.rs: Follow the default rustfmt code style (cargo fmtto format)
Always run clippy to lint code (cargo clippy) before completing tasks. Fix any warnings or lints that are introduced by your 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:
vm/src/builtins/int.rsvm/src/builtins/float.rsvm/src/builtins/complex.rs
🧬 Code graph analysis (3)
vm/src/builtins/int.rs (1)
vm/src/types/slot.rs (2)
num(1298-1298)number_downcast(1292-1294)
vm/src/builtins/float.rs (1)
vm/src/types/slot.rs (2)
num(1298-1298)number_downcast(1292-1294)
vm/src/builtins/complex.rs (1)
vm/src/types/slot.rs (1)
number_downcast(1292-1294)
⏰ 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). (11)
- GitHub Check: Run snippets and cpython tests on wasm-wasi
- GitHub Check: Run tests under miri
- GitHub Check: Run rust tests (windows-latest)
- GitHub Check: Check the WASM package and demo
- GitHub Check: Run rust tests (macos-latest)
- GitHub Check: Run rust tests (ubuntu-latest)
- GitHub Check: Run snippets and cpython tests (ubuntu-latest)
- GitHub Check: Run snippets and cpython tests (macos-latest)
- GitHub Check: Check Rust code with rustfmt and clippy
- GitHub Check: Run snippets and cpython tests (windows-latest)
- GitHub Check: Ensure compilation on various targets
🔇 Additional comments (3)
vm/src/builtins/int.rs (1)
776-776: Correct nb_bool semantics (non-zero -> true). LGTMFlipping to
Ok(!...is_zero())aligns the PyNumberbooleanslot with Python’s truthiness and with__bool__in this type. No functional risks spotted.vm/src/builtins/float.rs (1)
593-593: Correct truthiness for floats. LGTM
Ok(!...is_zero())yields True for any non-zero (including NaN/±inf) and False for ±0.0, consistent with Python and the existing__bool__.vm/src/builtins/complex.rs (1)
495-495: Complex truthiness fixed and consistent. LGTM
Ok(!value.is_zero())matches Python:0+0jis falsy; any other complex is truthy. This also matches the type’s__bool__.
Summary by CodeRabbit