◐ Shell
clean mode source ↗

Deprecate ::new_ref by youknowone · Pull Request #6046 · RustPython/RustPython

Walkthrough

This change deprecates the new_ref constructor methods in several built-in types, replacing their explicit PyRef::new_ref usage with idiomatic from(...).into_ref(ctx) patterns. Deprecation attributes and notes are added to guide users toward the new approach. Some internal helper methods are consolidated or refactored for consistency. Additionally, the new_ref method was removed from PyNamespace, and a new public new constructor was added for PyStaticMethod.

Changes

Cohort / File(s) Change Summary
Deprecate and Refactor new_ref Methods
vm/src/builtins/bytearray.rs, vm/src/builtins/bytes.rs, vm/src/builtins/classmethod.rs, vm/src/builtins/complex.rs, vm/src/builtins/dict.rs, vm/src/builtins/function.rs, vm/src/builtins/list.rs, vm/src/builtins/staticmethod.rs, vm/src/builtins/str.rs, vm/src/builtins/set.rs
Added deprecation attributes and notes to new_ref methods in various built-in types. Updated implementations to use from(...).into_ref(ctx) or equivalent, replacing explicit PyRef::new_ref calls.
Consolidate and Refactor Helper Methods
vm/src/builtins/complex.rs
Moved to_complex64 into main impl as a const fn, added and consolidated number_op helper, removed duplicate implementation.
Add New Constructor and Modify Deprecated Method
vm/src/builtins/staticmethod.rs
Introduced a new public new method for PyStaticMethod, with new_ref now a deprecated wrapper calling the new method.
Remove Deprecated Method
vm/src/builtins/namespace.rs
Removed the new_ref associated function from PyNamespace.
Update Usage of Deprecated Methods
vm/src/frame.rs, vm/src/stdlib/marshal.rs, vm/src/macros.rs
Replaced calls to new_ref with default().into_ref(ctx) or PyPayload::into_ref for creating new instances, reflecting the deprecation and removal of new_ref.
Add Clarifying Comment
vm/src/builtins/tuple.rs
Added a comment cautioning not to deprecate new_ref due to the need to check for empty tuples.

Sequence Diagram(s)

sequenceDiagram
    participant Caller
    participant BuiltinType
    participant Context

    Caller->>BuiltinType: from(data)
    BuiltinType->>BuiltinType: construct instance
    Caller->>BuiltinType: into_ref(ctx)
    BuiltinType->>Context: create PyRef
    Context-->>Caller: PyRef<BuiltinType>
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Possibly related PRs

Suggested reviewers

  • coolreader18

Poem

A rabbit hopped through fields of code,
Marking old paths with "deprecated" mode.
Constructors now are sleek and neat,
With from and into_ref—oh, what a treat!
The garden of builtins pruned with care,
For future bunnies everywhere. 🐇✨

Note

⚡️ Unit Test Generation is now available in beta!

Learn more here, or try it out under "Finishing Touches" below.


📜 Recent review details

Configuration used: .coderabbit.yml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between ce73615 and 0a05a91.

📒 Files selected for processing (15)
  • vm/src/builtins/bytearray.rs (1 hunks)
  • vm/src/builtins/bytes.rs (1 hunks)
  • vm/src/builtins/classmethod.rs (1 hunks)
  • vm/src/builtins/complex.rs (1 hunks)
  • vm/src/builtins/dict.rs (1 hunks)
  • vm/src/builtins/function.rs (1 hunks)
  • vm/src/builtins/list.rs (1 hunks)
  • vm/src/builtins/namespace.rs (0 hunks)
  • vm/src/builtins/set.rs (1 hunks)
  • vm/src/builtins/staticmethod.rs (1 hunks)
  • vm/src/builtins/str.rs (1 hunks)
  • vm/src/builtins/tuple.rs (1 hunks)
  • vm/src/frame.rs (1 hunks)
  • vm/src/macros.rs (1 hunks)
  • vm/src/stdlib/marshal.rs (2 hunks)
💤 Files with no reviewable changes (1)
  • vm/src/builtins/namespace.rs
✅ Files skipped from review due to trivial changes (3)
  • vm/src/builtins/tuple.rs
  • vm/src/builtins/set.rs
  • vm/src/builtins/bytearray.rs
🚧 Files skipped from review as they are similar to previous changes (8)
  • vm/src/builtins/dict.rs
  • vm/src/builtins/bytes.rs
  • vm/src/builtins/str.rs
  • vm/src/builtins/function.rs
  • vm/src/builtins/classmethod.rs
  • vm/src/builtins/staticmethod.rs
  • vm/src/builtins/list.rs
  • vm/src/builtins/complex.rs
🧰 Additional context used
📓 Path-based instructions (1)
**/*.rs

📄 CodeRabbit Inference Engine (.github/copilot-instructions.md)

**/*.rs: Follow the default rustfmt code style (cargo fmt to format)
Always run clippy to lint code (cargo clippy) before completing tasks. Fix any warnings or lints that are introduced by your 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:

  • vm/src/frame.rs
  • vm/src/stdlib/marshal.rs
  • vm/src/macros.rs
🧠 Learnings (2)
vm/src/stdlib/marshal.rs (1)

Learnt from: CR
PR: RustPython/RustPython#0
File: .github/copilot-instructions.md:0-0
Timestamp: 2025-07-19T03:16:56.511Z
Learning: Applies to **/*.rs : Use the macro system (pyclass, pymodule, pyfunction, etc.) when implementing Python functionality in Rust

vm/src/macros.rs (1)

Learnt from: CR
PR: RustPython/RustPython#0
File: .github/copilot-instructions.md:0-0
Timestamp: 2025-07-19T03:16:56.511Z
Learning: Applies to **/*.rs : Use the macro system (pyclass, pymodule, pyfunction, etc.) when implementing Python functionality in Rust

🔇 Additional comments (5)
vm/src/frame.rs (2)

759-759: LGTM: Consistent migration from new_ref to default().into_ref()

The change from PySet::new_ref(&vm.ctx) to PySet::default().into_ref(&vm.ctx) aligns with the PR's objective to deprecate explicit new_ref constructors in favor of the idiomatic pattern.


767-767: LGTM: Consistent migration from new_ref to default().into_ref()

Same pattern as line 759 - the migration from PySet::new_ref(&vm.ctx) to PySet::default().into_ref(&vm.ctx) is semantically equivalent and follows the established refactoring pattern.

vm/src/stdlib/marshal.rs (2)

17-17: LGTM: Import addition supports the refactoring pattern

The addition of PyPayload to the imports aligns with the broader refactoring where object construction patterns are being standardized to use into_ref methods.


189-189: LGTM: Consistent migration from new_ref to default().into_ref()

The change from PySet::new_ref(&vm.ctx) to PySet::default().into_ref(&vm.ctx) follows the same pattern as the frame.rs changes and maintains semantic equivalence while adopting the new idiomatic construction pattern.

vm/src/macros.rs (1)

49-49: LGTM: Macro updated to use new construction pattern

The change from PyNamespace::new_ref to PyPayload::into_ref(PyNamespace {}, &$vm.ctx) correctly adapts the macro to work with the deprecation of the new_ref method. The direct struct construction followed by into_ref is consistent with the established refactoring pattern.

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • 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
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai generate unit tests to generate unit tests for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.