More slot impl by youknowone · Pull Request #6618 · RustPython/RustPython
Warning
Rate limit exceeded
@youknowone has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 12 minutes and 8 seconds before requesting another review.
⌛ How to resolve this issue?
After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.
We recommend that you space out your commits to avoid hitting the rate limit.
🚦 How do rate limits work?
CodeRabbit enforces hourly rate limits for each developer per organization.
Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.
Please see our FAQ for further information.
📒 Files selected for processing (11)
crates/vm/src/builtins/bool.rscrates/vm/src/builtins/bytearray.rscrates/vm/src/builtins/bytes.rscrates/vm/src/builtins/dict.rscrates/vm/src/builtins/list.rscrates/vm/src/builtins/mappingproxy.rscrates/vm/src/builtins/memory.rscrates/vm/src/builtins/set.rscrates/vm/src/builtins/tuple.rscrates/vm/src/stdlib/collections.rscrates/vm/src/stdlib/ctypes/array.rs
📝 Walkthrough
Walkthrough
This pull request removes Python method exposure for __len__ across multiple builtin classes via #[pymethod] attribute removal, refactors boolean truthiness evaluation to use protocol-based slot lookups instead of method fallbacks, updates binary operations for mixed set/frozenset types with type checking, and improves slot machinery for method resolution based on whether a type defines its own methods.
Changes
| Cohort / File(s) | Summary |
|---|---|
Boolean Truthiness Refactor crates/vm/src/builtins/bool.rs |
Replaced multi-path truthiness evaluation with simplified protocol-based approach: attempt nb_bool slot first, then mp_length, then sq_length, defaulting to truthy. Removed legacy __bool__ and __len__ fallback method lookups. Removed unused malachite_bigint::Sign import. |
len Method Exposure Removal crates/vm/src/builtins/{bytearray,bytes,dict,list,tuple}.rs, crates/vm/src/builtins/memory.rs, crates/vm/src/stdlib/ctypes/array.rs |
Removed #[pymethod] attribute from __len__ methods across eight builtin types, eliminating direct Python method exposure while preserving internal Rust implementations. |
Mapping Proxy Protocol Wiring crates/vm/src/builtins/mappingproxy.rs |
Removed #[pymethod] from __len__ and wired length exposure through sequence protocol via PySequenceMethods.length using sequence_downcast, maintaining external accessibility through protocol machinery instead of direct Python binding. |
Set/Frozenset Binary Operations crates/vm/src/builtins/set.rs |
Reworked binary operators (__sub__/__rsub__, __and__/__rand__, __xor__/__rxor__, __or__/__ror__) to validate operand types using new AnySet::check() helper, return NotImplemented for invalid type combinations, and wrap results to appropriate public types. Introduced unified __contains__(&self, needle: &PyObject, vm) signature for both PySet and PyFrozenSet; updated sequence containment to route through new __contains__ implementation. |
Collections Deque API Changes crates/vm/src/stdlib/collections.rs |
Removed #[pymethod] exposure for __len__ and __add__ methods from PyDeque, eliminating Python-facing concatenation and length methods while retaining internal concat and __iadd__ functionality. |
Windows Registry Type Conversion crates/vm/src/stdlib/winreg.rs |
Removed #[pymethod] attribute from PyHkey::__int__ method, eliminating Python-level integer conversion exposure while preserving internal Rust method for internal protocol hooks. |
Number Protocol Slot Copying crates/vm/src/protocol/number.rs |
Extended PyNumberSlots::copy_from to mirror left-hand operator slots to right-hand counterparts for binary operators (Add, Subtract, Multiply, Remainder, Divmod, Power, Lshift, Rshift, And, Xor, Or, FloorDivide, TrueDivide, MatrixMultiply). |
Slot Update Machinery crates/vm/src/types/slot.rs |
Refactored slot update logic to collect slot definitions into temporary Vec before iteration, added has_own detection to determine if a type defines its own method for a slot, and implemented conditional wrapper/fallback selection based on ownership (ass_item/ass_subscript slots consider paired __setitem__/__delitem__ methods). |
Estimated code review effort
🎯 4 (Complex) | ⏱️ ~45 minutes
Possibly related PRs
- Implement copyslot #6505 — Both PRs modify boolean/truthiness codepaths to use direct protocol slots (nb_bool, mapping, sequence) instead of legacy
__bool__/__len__fallbacks. - PyWrapperDescrObject and rewrite toggle_slot #6536 — Both PRs refactor VM slot/descriptor machinery with changes to slot update logic and wrapper/SlotFunc usage patterns in
crates/vm/src/types/slot.rs. - RichCompare contains pymethod. No AtomicCell for slots #6562 — Both PRs modify slot machinery in
crates/vm/src/types/slot.rsto centralize wrapper functions and change how slots are updated based on type definitions.
Poem
🐰 Protocol paths now guide our way, no more
__len__for the fray,
Sets and slots align just right, truthiness checked in protocol's light,
Simpler flows, fewer lookups dance, RustPython takes a cleaner stance! ✨
Pre-merge checks and finishing touches
❌ Failed checks (1 warning, 1 inconclusive)
| Check name | Status | Explanation | Resolution |
|---|---|---|---|
| Docstring Coverage | Docstring coverage is 48.84% which is insufficient. The required threshold is 80.00%. | You can run @coderabbitai generate docstrings to improve docstring coverage. |
|
| Title check | ❓ Inconclusive | The title 'More slot impl' is vague and does not clearly convey the main changes, which involve refactoring truthiness evaluation, removing len method exposures, and restructuring set operations. | Consider a more descriptive title that captures the primary change, such as 'Refactor protocol-driven truthiness and remove len Python method exposures' or 'Implement slot-based protocols for truthiness and method access'. |
✅ Passed checks (1 passed)
| Check name | Status | Explanation |
|---|---|---|
| Description Check | ✅ Passed | Check skipped - CodeRabbit’s high-level summary is enabled. |
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.