◐ Shell
clean mode source ↗

Introduce slot wrapper to __init__ by youknowone · Pull Request #4884 · RustPython/RustPython

Caution

Review failed

The pull request is closed.

📝 Walkthrough

Walkthrough

Adds wrapper descriptor types (PySlotWrapper and PyMethodWrapper), exposes them in the type zoo, wires an init slot wrapper into class extension, hides the Initializer::slot_init Python binding, refines init return errors, and adds a no-op Initializer for PyWeak; also adjusts BufferedMixin init exposure.

Changes

Cohort / File(s) Summary
Descriptor types
crates/vm/src/builtins/descriptor.rs
Add PySlotWrapper and PyMethodWrapper pypayloads/pyclasses with descriptor semantics, binding (get), call delegation, represent, hash, and compare implementations; public accessors and reduce for method-wrapper.
Type registration
crates/vm/src/types/zoo.rs
Add wrapper_descriptor_type and method_wrapper_type fields and initialize them via PySlotWrapper::init_builtin_type() and PyMethodWrapper::init_builtin_type().
Class extension
crates/vm/src/class.rs
Import descriptor::PySlotWrapper and instantiate/assign a PySlotWrapper as __init__ on classes that define an init slot but lack an __init__ in their dict.
Initializer/slot plumbing
crates/vm/src/types/slot.rs
Remove #[pymethod(name = "__init__")] from Initializer::slot_init (no automatic Python binding) and change the init-wrapper error message to include the concrete return type name.
Base object init validation
crates/vm/src/builtins/object.rs
Change slot_init parameter names to (zelf, args, vm) and add excess-args validation that considers heap vs non-heap types and whether __new__/__init__ are overridden.
WeakRef initializer
crates/vm/src/builtins/weakref.rs
Add Initializer impl (no-op) for PyWeak, include Initializer and PyRef in imports, and add Initializer to the pyclass trait list.
IO BufferedMixin cleanup
crates/vm/src/stdlib/io.rs
Remove public __init__ from BufferedMixin and call init directly in slot_init instead of delegating through __init__.

Sequence Diagram(s)

sequenceDiagram
    participant User as User Code
    participant Class as Class Object
    participant SW as PySlotWrapper
    participant MW as PyMethodWrapper
    participant Impl as Underlying Slot Impl

    User->>Class: access __init__
    Class->>SW: class-level descriptor lookup (PySlotWrapper)
    SW->>MW: __get__(instance) -> return PyMethodWrapper bound to instance
    User->>MW: call bound method ()
    MW->>SW: delegate call (enforce type, unwrap, forward)
    SW->>Impl: invoke underlying slot function (Init/New)
    Impl-->>User: result / return
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~50 minutes

Possibly related PRs

Suggested reviewers

  • arihant2math
  • ShaharNaveh
  • coolreader18

Poem

🐰 I wrap the slots, I bind with care,
A method friend appears to share.
Init checks tighten, errors shine bright,
Small hops of code, all snug tonight. 🥕

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 48.28% 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 'Introduce slot wrapper to init' directly describes the main change—adding PySlotWrapper and PyMethodWrapper descriptor support, with PySlotWrapper specifically wrapping the init slot. The title is concise and accurately reflects the primary objective of the changeset.

📜 Recent review details

Configuration used: Path: .coderabbit.yml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 3cb6b52 and 1f9df15.

⛔ Files ignored due to path filters (4)
  • Lib/test/test_descr.py is excluded by !Lib/**
  • Lib/test/test_inspect/test_inspect.py is excluded by !Lib/**
  • Lib/test/test_types.py is excluded by !Lib/**
  • Lib/test/test_weakref.py is excluded by !Lib/**
📒 Files selected for processing (7)
  • crates/vm/src/builtins/descriptor.rs
  • crates/vm/src/builtins/object.rs
  • crates/vm/src/builtins/weakref.rs
  • crates/vm/src/class.rs
  • crates/vm/src/stdlib/io.rs
  • crates/vm/src/types/slot.rs
  • crates/vm/src/types/zoo.rs

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

Comment @coderabbitai help to get the list of available commands and usage tips.