Skip to content

Conversation

@youknowone
Copy link
Member

@youknowone youknowone commented Dec 2, 2025

Summary by CodeRabbit

  • Refactor
    • Internal trait bound adjustments and type system improvements across object handling APIs to enhance code consistency and debugging capabilities. No functional changes or end-user impact.

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Dec 2, 2025

Walkthrough

This PR refactors the VM object system's trait bounds, replacing PyObjectPayload with PyPayload across public APIs and adding std::fmt::Debug constraints to ensure debug-printability. The PyPayload trait itself is narrowed by removing Debug from supertraits, with Debug requirements instead added to specific methods via where clauses.

Changes

Cohort / File(s) Summary
Foundational trait updates
crates/vm/src/object/payload.rs
Removed std::fmt::Debug from PyPayload supertrait; added where Self: std::fmt::Debug to five public methods (into_pyobject, _into_ref, into_exact_ref, into_ref, into_ref_with_type). Updated PyObjectPayload impl to require Debug on generic parameter.
Object system refactoring
crates/vm/src/object/core.rs, crates/vm/src/object/ext.rs
Broadly replaced PyObjectPayload with PyPayload across downcasting APIs, trait impls (Borrow, AsRef, Hash, PartialEq, Eq, Debug), and weak reference handling. Added std::fmt::Debug bounds to debug-related functions and trait implementations. Updated PyRef, Py<T>, PyWeakRef<T>, PyExact<T>, PyRefExact<T>, PyLease<'a, T>, and PyAtomicRef<T> generic constraints.
Trait bound additions
crates/vm/src/builtins/dict.rs, crates/vm/src/types/slot.rs, crates/vm/src/vm/context.rs
Added std::fmt::Debug constraint to DictView::ReverseIter associated type, DefaultConstructor trait supertraits, and Context::new_pyref<T, P> generic parameter.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Areas requiring extra attention:

  • payload.rs: The removal of Debug from trait supertraits and shift to method-level where clauses should be verified to ensure all implementors can satisfy the constraints without breaking downstream code.
  • core.rs & ext.rs: The large-scale replacement of PyObjectPayload with PyPayload across many impl blocks should be spot-checked for completeness and correctness, particularly in downcasting and weak reference paths.
  • Trait impact: Verify that the Debug constraints on PyPayload methods do not conflict with existing trait object usage patterns or generic specializations.

Possibly related PRs

  • Integrate PyTupleTyped into PyTuple #5959: Overlapping refactors to payload/traverse/type-bounds in the same files (payload.rs, object/core.rs, vm/context.rs), modifying related trait bounds and APIs.
  • Downcastable #5986: Related changes to PyObject downcasting APIs in object/core.rs affecting downcast/payload methods and trait bounds.

Suggested reviewers

  • arihant2math
  • coolreader18

Poem

🐰 Debug bounds hop through every trait,
PyPayload now leads our fate,
Constraints tighten, types align—
Where clauses make the code just fine,
A refactor clean, no bugs in sight!

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% 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 'Separate Debug from PyPayload' accurately captures the main objective of this PR, which systematically decouples the Debug trait requirement from the PyPayload trait across the codebase.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • 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

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

@youknowone youknowone marked this pull request as ready for review December 2, 2025 11:00
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 (1)
crates/vm/src/object/core.rs (1)

1056-1061: Consider adding documentation for the leak method.

The leak method implementation is correct (standard Rust leak pattern), but it would benefit from documentation explaining its purpose, when it should be used, and that the leaked object will remain in memory indefinitely.

Apply this diff to add documentation:

+    /// Leaks this reference, returning a static reference to the inner object.
+    ///
+    /// The reference count is not decremented, so the object will remain in memory
+    /// indefinitely. This is primarily useful for creating static singletons or
+    /// objects that need to live for the entire program duration.
     pub const fn leak(pyref: Self) -> &'static Py<T> {
📜 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 bf8152b and 4055764.

📒 Files selected for processing (6)
  • crates/vm/src/builtins/dict.rs (1 hunks)
  • crates/vm/src/object/core.rs (24 hunks)
  • crates/vm/src/object/ext.rs (16 hunks)
  • crates/vm/src/object/payload.rs (4 hunks)
  • crates/vm/src/types/slot.rs (1 hunks)
  • crates/vm/src/vm/context.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 by running cargo fmt to format Rust code
Always run clippy to lint Rust 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/types/slot.rs
  • crates/vm/src/builtins/dict.rs
  • crates/vm/src/vm/context.rs
  • crates/vm/src/object/payload.rs
  • crates/vm/src/object/ext.rs
  • crates/vm/src/object/core.rs
🧬 Code graph analysis (2)
crates/vm/src/object/ext.rs (2)
crates/vm/src/object/core.rs (2)
  • payload (654-662)
  • payload (912-914)
crates/vm/src/object/payload.rs (1)
  • std (22-22)
crates/vm/src/object/core.rs (1)
crates/vm/src/object/payload.rs (2)
  • std (22-22)
  • downcastable_from (27-29)
⏰ 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). (1)
  • GitHub Check: Run snippets and cpython tests (macos-latest)
🔇 Additional comments (16)
crates/vm/src/vm/context.rs (1)

380-386: LGTM - Debug bound appropriately added to generic helper.

The new_pyref method correctly adds std::fmt::Debug bound since it calls into_ref, which now requires Debug per the changes in payload.rs. This is consistent with the PR's goal of adding Debug constraints only where needed.

crates/vm/src/types/slot.rs (1)

796-805: LGTM - Debug bound correctly added to DefaultConstructor.

The Debug bound is necessary because construct_and_init calls into_ref, which now requires Self: std::fmt::Debug. This aligns with the PR's approach of adding Debug constraints at the method level where payloads are converted to references.

crates/vm/src/builtins/dict.rs (1)

753-755: LGTM - Debug bound appropriately added to associated type.

The ReverseIter associated type now requires PyPayload + std::fmt::Debug, which is consistent with the concrete iterator types that already #[derive(Debug)] (e.g., PyDictReverseKeyIterator at line 914). This makes the constraint explicit in the trait definition.

crates/vm/src/object/payload.rs (3)

19-23: Clean separation of Debug from PyPayload trait.

Removing Debug from the supertrait allows types to implement PyPayload without necessarily implementing Debug. The Debug constraint is now applied only to methods that actually need it (those that construct PyRef or PyObjectRef).


52-57: Correct per-method Debug constraint.

The into_pyobject method requires Debug because it calls into_ref, which needs to store the payload in a debuggable object structure.


119-124: PyObjectPayload correctly retains Debug requirement.

PyObjectPayload continues to require Debug since objects that are stored and potentially displayed/debugged need this capability. The blanket impl appropriately requires PyPayload + std::fmt::Debug + 'static.

crates/vm/src/object/ext.rs (4)

1-4: Import correctly updated to PyPayload.

The import change from PyObjectPayload to PyPayload aligns with the broader refactor of using PyPayload as the primary trait for type-safe payload handling.


61-63: PyExact struct declaration correctly uses no explicit bounds.

The #[repr(transparent)] struct doesn't require bounds on the declaration itself; bounds are appropriately placed on the impl blocks.


582-590: ToPyObject blanket impl correctly requires Debug.

This blanket implementation for T: PyPayload + std::fmt::Debug is correct because it calls PyPayload::into_pyobject, which requires the Debug bound per the changes in payload.rs.


261-271: Send/Sync impls appropriately constrained.

The Send and Sync implementations for PyAtomicRef<T> correctly use PyPayload bounds. These atomic reference types need thread-safety guarantees but don't require Debug for their safety invariants.

crates/vm/src/object/core.rs (6)

79-95: LGTM! Trait bound refactoring is correctly applied.

The function signatures correctly use PyPayload instead of PyObjectPayload, and the Debug bound is appropriately added only to debug_obj where it's required for formatting.


531-577: LGTM! Downcast API refactoring is consistent.

The downcast methods correctly use PyPayload bounds without requiring Debug, which is appropriate since these methods only perform type checking and casting, not object construction.


899-981: LGTM! Py trait implementations are correctly refactored.

The trait bounds are consistently applied across all implementations. The Debug bound is appropriately added only to the Debug impl while other trait impls correctly propagate their respective bounds alongside PyPayload.


1026-1061: LGTM! PyRef impl separation is well-designed.

The separation of two impl blocks—one requiring only PyPayload and another requiring PyPayload + Debug—is correct. The new_ref method needs Debug because it calls PyInner::new, while other methods like leak, from_non_null, and from_raw don't require debug-printability.


1147-1159: LGTM! PyWeakRef refactoring is consistent.

The weak reference type correctly adopts the PyPayload bound, maintaining type safety while aligning with the broader refactoring objectives.


16-16: Verify that clippy passes with the refactored trait bounds.

This broad refactoring changes trait bounds throughout the object model. Ensure cargo clippy runs without new warnings on the changed code as required by the coding guidelines.

@youknowone youknowone merged commit 563dc0f into RustPython:main Dec 2, 2025
23 of 24 checks passed
@youknowone youknowone deleted the debug-payload branch December 2, 2025 11:20
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