slot for numeric ops by youknowone · Pull Request #6619 · RustPython/RustPython
📝 Walkthrough
Walkthrough
This PR systematically removes Python method exposure (via #[pymethod] attributes) from multiple operator methods including __add__, __iadd__, __mul__, __imul__, __rmul__, and OR-family operators across builtin and stdlib types, while preserving their Rust implementations. It also refactors sequence repeat slot handling and strengthens MRO-based compatibility checking in slot lookup.
Changes
| Cohort / File(s) | Change Summary |
|---|---|
Sequence types (array, list, tuple) crates/stdlib/src/array.rs, crates/vm/src/builtins/list.rs, crates/vm/src/builtins/tuple.rs |
Removed #[pymethod] attributes from __add__, __iadd__, __mul__, __imul__, and __rmul__ exposure, hiding these operator methods from Python API. |
String and binary types crates/vm/src/builtins/str.rs, crates/vm/src/builtins/bytes.rs, crates/vm/src/builtins/bytearray.rs |
Removed #[pymethod] from __add__, __rmul__, __rmod__ bindings; renamed/reorganized __mod__ exposure; updated internal calls to use __mod__ instead of legacy method names. |
Type and union operators crates/vm/src/builtins/type.rs, crates/vm/src/builtins/union.rs, crates/vm/src/builtins/genericalias.rs |
Removed #[pymethod] attributes from __or__ and __ror__ (including reverse-OR bindings), eliminating Python-level OR operator exposure. |
Mapping and view operators crates/vm/src/builtins/dict.rs, crates/vm/src/builtins/mappingproxy.rs |
Removed #[pymethod] from __or__, __ior__, __ror__ and set-operation methods (__rxor__, __rand__, __rsub__), reducing operator availability on dict/view types. |
Collections and ctypes crates/vm/src/stdlib/collections.rs, crates/vm/src/stdlib/ctypes/pointer.rs, crates/vm/src/stdlib/ctypes/simple.rs, crates/vm/src/stdlib/ctypes/structure.rs |
Removed #[pymethod] attributes from __mul__, __imul__, __rmul__, __iadd__ in PyDeque and ctypes multiplication methods, hiding repeat/multiplication operators. |
Descriptor and slot infrastructure crates/vm/src/builtins/descriptor.rs |
Added ArgSize to public imports; updated SeqRepeat handling to use ArgSize with into() conversion; introduced type-compatibility runtime check in PyMethodWrapper.call to verify bound object type. |
Slot wiring refactoring crates/vm/src/types/slot.rs |
Added private sequence_repeat_wrapper and sequence_inplace_repeat_wrapper functions; refactored SqRepeat/SqInplaceRepeat slot handling; modified lookup_slot_in_mro signature to accept MRO slice parameter for compatibility verification; introduced is_subclass_of helper and cached mro variable for MRO-based extraction; tightened wrapper compatibility checks during MRO lookup with per-class compatibility validation. |
Estimated code review effort
🎯 3 (Moderate) | ⏱️ ~25 minutes
Possibly related PRs
- RichCompare contains pymethod. No AtomicCell for slots #6562 — Modifies slot wiring and sequence/mapping wrapper functions in
slot.rs, directly intersecting with the PR's sequence repeat refactoring andSqRepeat/SqInplaceRepeathandling. - Ctypes __mul__ #6305 — Adds/implements numeric
__mul__exposure and AsNumber integration for ctypes types, directly modifying the same ctypes multiplication methods now being hidden. - PyWrapperDescrObject and rewrite toggle_slot #6536 — Rewrites slot/descriptor infrastructure (SlotDef, SlotAccessor, SLOT_DEFS, MRO-driven slot wiring), closely related to the PR's
lookup_slot_in_mrosignature changes and MRO-aware extraction logic.
Suggested reviewers
- arihant2math
Poem
🐰 ✨ Methods now hide from Python's eager sight,
Yet Rust keeps them ready, tucked out of light.
Slots rewire through MRO's careful chain,
Operators vanish—infrastructure's refrain!
Cleanup and care, with a rabbit's delight.
Pre-merge checks and finishing touches
❌ Failed checks (1 warning, 1 inconclusive)
| Check name | Status | Explanation | Resolution |
|---|---|---|---|
| Docstring Coverage | Docstring coverage is 56.25% which is insufficient. The required threshold is 80.00%. | You can run @coderabbitai generate docstrings to improve docstring coverage. |
|
| Title check | ❓ Inconclusive | The title 'slot for numeric ops' is vague and doesn't clearly convey the main purpose of the changes. While the PR does modify numeric operations, the term 'slot' is a technical implementation detail rather than a description of what the change achieves. | Consider a more descriptive title that explains the primary intent, such as 'Remove operator method exposure from Python API' or 'Refactor numeric operation slots for internal use'. |
✅ Passed checks (1 passed)
| Check name | Status | Explanation |
|---|---|---|
| Description Check | ✅ Passed | Check skipped - CodeRabbit’s high-level summary is enabled. |
✨ Finishing touches
- 📝 Generate docstrings
📜 Recent review details
Configuration used: Path: .coderabbit.yml
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (17)
crates/stdlib/src/array.rscrates/vm/src/builtins/bytearray.rscrates/vm/src/builtins/bytes.rscrates/vm/src/builtins/descriptor.rscrates/vm/src/builtins/dict.rscrates/vm/src/builtins/genericalias.rscrates/vm/src/builtins/list.rscrates/vm/src/builtins/mappingproxy.rscrates/vm/src/builtins/str.rscrates/vm/src/builtins/tuple.rscrates/vm/src/builtins/type.rscrates/vm/src/builtins/union.rscrates/vm/src/stdlib/collections.rscrates/vm/src/stdlib/ctypes/pointer.rscrates/vm/src/stdlib/ctypes/simple.rscrates/vm/src/stdlib/ctypes/structure.rscrates/vm/src/types/slot.rs
💤 Files with no reviewable changes (12)
- crates/vm/src/builtins/genericalias.rs
- crates/vm/src/stdlib/ctypes/structure.rs
- crates/vm/src/stdlib/ctypes/simple.rs
- crates/vm/src/builtins/dict.rs
- crates/vm/src/builtins/type.rs
- crates/vm/src/builtins/tuple.rs
- crates/vm/src/builtins/list.rs
- crates/stdlib/src/array.rs
- crates/vm/src/builtins/union.rs
- crates/vm/src/builtins/mappingproxy.rs
- crates/vm/src/stdlib/collections.rs
- crates/vm/src/stdlib/ctypes/pointer.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 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/builtins/str.rscrates/vm/src/builtins/descriptor.rscrates/vm/src/builtins/bytes.rscrates/vm/src/builtins/bytearray.rscrates/vm/src/types/slot.rs
🧠 Learnings (1)
📚 Learning: 2025-12-27T14:03:49.034Z
Learnt from: CR
Repo: RustPython/RustPython PR: 0
File: .github/copilot-instructions.md:0-0
Timestamp: 2025-12-27T14:03:49.034Z
Learning: Applies to **/*.rs : Use the macro system (`pyclass`, `pymodule`, `pyfunction`, etc.) when implementing Python functionality in Rust
Applied to files:
crates/vm/src/builtins/descriptor.rs
🧬 Code graph analysis (3)
crates/vm/src/builtins/bytes.rs (3)
crates/vm/src/builtins/bytearray.rs (1)
__mod__(534-537)crates/vm/src/builtins/str.rs (1)
__mod__(947-949)crates/vm/src/builtins/int.rs (1)
b(659-659)
crates/vm/src/builtins/bytearray.rs (1)
crates/vm/src/builtins/bytes.rs (2)
__mod__(518-521)a(670-670)
crates/vm/src/types/slot.rs (2)
crates/vm/src/builtins/type.rs (1)
mro(1680-1682)crates/vm/src/builtins/descriptor.rs (1)
cls(379-379)
🔇 Additional comments (7)
crates/vm/src/builtins/bytearray.rs (1)
534-537: Remainder (mod) wiring for PyByteArray looks correct
__mod__simply wrapsinner().cformatandAsNumber::remaindernow delegates to it viato_pyresult(vm), matching the pattern used forbytes/strand keeping formatting logic centralized. No issues from a correctness or API-consistency standpoint.Also applies to: 812-819
crates/vm/src/builtins/bytes.rs (1)
518-521: Bytes mod implementation and numeric remainder slot are consistentThe new
__mod__wrapper aroundinner.cformatplusAsNumber::remainderdelegating to it mirrors the bytearray/str setup and keeps%-formatting behavior in one place. The conversion throughinto()andto_pyresult(vm)is type-safe and idiomatic here.Also applies to: 669-675
crates/vm/src/builtins/str.rs (1)
947-949: String%formatting routed through mod and AsNumber cleanlyRenaming the helper to
__mod__and havingAsNumber::remaindercall it centralizes%-formatting viacformat_stringwhile keeping the return asWtf8Bufconverted throughto_pyresult(vm). This aligns string behavior with bytes/bytearray and looks semantically correct.Also applies to: 1556-1562
crates/vm/src/builtins/descriptor.rs (2)
8-8: SeqRepeat now uses ArgSize for count – consistent with size/index semanticsSwitching
SlotFunc::SeqRepeatto bind(ArgSize,)and passn.into()to theSeqRepeatFuncaligns sequence repeat with other ArgSize-based sites (e.g.,str/bytearray__mul__), improving bounds/overflow handling without changing call sites of the slot itself.Also applies to: 595-598
776-785: Type-compatibility check in PyMethodWrapper.call matches CPython’s descriptor rulesThe added
fast_isinstanceguard before invoking the wrapped slot prevents calling a method-wrapper on an incompatible instance and produces a clear TypeError, mirroring CPython’s behavior for wrappers bound to the wrong class. This is a good safety improvement with no downside.crates/vm/src/types/slot.rs (2)
441-449: Sequence repeat slots correctly delegate to__mul__/__imul__wrappers
sequence_repeat_wrapperandsequence_inplace_repeat_wrappernow forward to__mul__/__imul__viacall_special_method, andSqRepeat/SqInplaceRepeatare wired through them withupdate_sub_slot. This keeps the C-API-style sequence slots in sync with Python-level dunder methods and respects existing type-specific SeqRepeat functions when present.Also applies to: 1307-1316
1365-1368: MRO-based slot lookup now honors wrapper.typ compatibilityThe new
is_subclass_of+try_extractlogic ensures awrapper_descriptor’s slot is only reused when the target class’s MRO actually containswrapper.typ, and it stops once a real method (non-wrapper) is found. Using appropriate MRO slices (&mro,&mro[i+1..]) keeps the subclass relationship checks accurate across the hierarchy. This tightens correctness around slot reuse without affecting existing valid cases.Also applies to: 1371-1374, 1388-1402
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.