Remove duplicated richcompare pymethods and add missing rops' wrapper, __getattr__, proper __bool__ impl by youknowone · Pull Request #6579 · RustPython/RustPython
📝 Walkthrough
Walkthrough
This PR refactors Python object truthiness evaluation by systematically migrating __bool__ methods to the AsNumber numeric protocol across multiple builtin types, removes Python-level comparison and representation operators from PyBaseObject in favor of slot-based mechanisms, and enhances the slot system with recursive subclass updates and improved __getattr__ resolution logic.
Changes
| Cohort / File(s) | Summary |
|---|---|
Comparison/representation removal from base object crates/vm/src/builtins/object.rs |
Removed 8 public PyMethods: __eq__, __ne__, __lt__, __le__, __ge__, __gt__, __repr__, __hash__. Comparison and hashing now rely on slot-based mechanisms rather than explicit Python-level methods. |
Migration of bool to AsNumber protocol crates/vm/src/builtins/range.rs, crates/vm/src/builtins/singletons.rs, crates/vm/src/builtins/tuple.rs, crates/vm/src/stdlib/collections.rs, crates/vm/src/stdlib/ctypes/function.rs, crates/vm/src/stdlib/ctypes/pointer.rs, crates/vm/src/stdlib/ctypes/simple.rs, crates/vm/src/stdlib/winreg.rs |
Consistent pattern across 8 files: added AsNumber to pyclass traits, implemented AsNumber with boolean handlers (truthiness checks), and removed explicit __bool__ methods. Each type's boolean behavior now routes through the numeric protocol. |
Weak proxy numeric protocol crates/vm/src/builtins/weakproxy.rs |
Added AsNumber to PyWeakProxy class declaration and implemented AsNumber::as_number() with a boolean handler that delegates to the proxied object's is_true(). Removed __bool__ method. |
Slot system enhancements crates/vm/src/types/slot.rs |
Added update_subclasses<const ADD: bool>() to propagate slot updates recursively through subclass hierarchies. Enhanced TpGetattro slot resolution to check for __getattr__ presence in MRO and use wrapper when available, falling back to local/MRO slot resolution otherwise. |
Slot definitions expansion crates/vm/src/types/slot_defs.rs |
Added two new slot entries: __getattr__ (mapped to TpGetattro) and __rmul__ (mapped to SqRepeat). |
Operator wrapping logic crates/vm/src/class.rs |
Modified add_operators() to skip creating wrapper descriptors for __getattr__ slot, aligning with Python's semantics where __getattr__ is not present on object by default. |
Estimated code review effort
🎯 4 (Complex) | ⏱️ ~60 minutes
Possibly related PRs
- RustPython/RustPython#6505: Modifies slot inheritance machinery and introduces AsNumber/PyNumber protocol changes that directly parallel the protocol migrations in this PR.
- RustPython/RustPython#6562: Moves comparison dunders (
__eq__,__ne__,__lt__, etc.) into slot/Comparable machinery, directly complementing the removal of these methods from PyBaseObject. - RustPython/RustPython#6536: Implements the foundational slot/descriptor refactor (PyWrapper, SLOT_DEFS reorganization) that this PR builds upon for recursive slot updates and new slot definitions.
Suggested reviewers
- arihant2math
Poem
🐰 No more bools scattered about,
AsNumber makes truthiness devout,
Slots cascade through families deep,
Where__getattr__secrets keep,
A cleaner class hierarchy we reap! ✨
Pre-merge checks and finishing touches
❌ Failed checks (1 warning)
| Check name | Status | Explanation | Resolution |
|---|---|---|---|
| Docstring Coverage | Docstring coverage is 74.36% which is insufficient. The required threshold is 80.00%. | You can run @coderabbitai generate docstrings to improve docstring coverage. |
✅ Passed checks (2 passed)
| Check name | Status | Explanation |
|---|---|---|
| Description Check | ✅ Passed | Check skipped - CodeRabbit’s high-level summary is enabled. |
| Title check | ✅ Passed | The title accurately describes the main changes: removal of duplicated richcompare pymethods from object, addition of missing right-hand operation wrappers (rmul), migration of bool to numeric protocol, and getattr handling. |
✨ Finishing touches
- 📝 Generate docstrings
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.