Skip to content

Conversation

@youknowone
Copy link
Member

@youknowone youknowone commented Aug 26, 2025

Summary by CodeRabbit

  • Bug Fixes
    • Corrected boolean evaluation for integers, floats, and complex numbers: non‑zero values now evaluate to True and zero to False.
    • Ensures conditionals, loops, and truthiness checks behave consistently with Python’s expectations across numeric types.
    • Prevents unexpected True results for zero values, improving reliability in control flow and filtering.
    • No user-facing API changes; only behavior of numeric truthiness was corrected.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Aug 26, 2025

Walkthrough

Flips 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

Cohort / File(s) Summary
Numeric truthiness in PyNumberMethods
vm/src/builtins/int.rs, vm/src/builtins/float.rs, vm/src/builtins/complex.rs
In nb_bool, switch from value.is_zero() to !value.is_zero(), aligning truthiness with non-zero semantics. No other modifications.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

Thump-thump! I test with twitchy nose—
Zero’s false, and non-zero shows.
Int, float, complex in tidy row,
Truth now matches what we know.
I hop through bytes with carrot cheer—
Boolean breezes, crystal clear! 🥕

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 Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment

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.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR/Issue comments)

Type @coderabbitai help to get the list of available commands.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Status, Documentation and Community

  • Visit our Status Page to check the current availability of CodeRabbit.
  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a 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 truthiness

We’ve now wired up boolean for 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 True
  • bool(0.0) is False, bool(-0.0) is False, bool(float('nan')) is True
  • bool(0+0j) is False, bool(1+0j) is True, bool(0+1j) is True

Suggested 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__ uses self.value != 0.0 while 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.

📥 Commits

Reviewing files that changed from the base of the PR and between 711b1a6 and fffef56.

📒 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 fmt to 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.rs
  • vm/src/builtins/float.rs
  • vm/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). LGTM

Flipping to Ok(!...is_zero()) aligns the PyNumber boolean slot 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+0j is falsy; any other complex is truthy. This also matches the type’s __bool__.

@youknowone youknowone merged commit 16cdcfb into RustPython:main Aug 26, 2025
12 checks passed
@youknowone youknowone deleted the pynumber branch August 26, 2025 06:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant