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 Methodsvm/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 Methodsvm/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 Methodvm/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 Methodvm/src/builtins/namespace.rs |
Removed the new_ref associated function from PyNamespace. |
Update Usage of Deprecated Methodsvm/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 Commentvm/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>
Estimated code review effort
🎯 2 (Simple) | ⏱️ ~8 minutes
Possibly related PRs
- PyPayload::into_ref #3744: Removes usages of deprecated
new_refmethods, directly related to this PR’s refactoring and deprecation of those methods. - Integrate PyTupleTyped into PyTuple #5959: Refactors
PyTupleto be generic and removes old typed tuple wrappers, related through tuple construction and usage patterns.
Suggested reviewers
- coolreader18
Poem
A rabbit hopped through fields of code,
Marking old paths with "deprecated" mode.
Constructors now are sleek and neat,
Withfromandinto_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
📒 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 fmtto 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.rsvm/src/stdlib/marshal.rsvm/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)toPySet::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)toPySet::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 patternThe addition of
PyPayloadto the imports aligns with the broader refactoring where object construction patterns are being standardized to useinto_refmethods.
189-189: LGTM: Consistent migration from new_ref to default().into_ref()The change from
PySet::new_ref(&vm.ctx)toPySet::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 patternThe change from
PyNamespace::new_reftoPyPayload::into_ref(PyNamespace {}, &$vm.ctx)correctly adapts the macro to work with the deprecation of the new_ref method. The direct struct construction followed byinto_refis 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.
🪧 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
@coderabbitaiin a new review comment at the desired location with your query. Examples:@coderabbitai explain this code block.@coderabbitai modularize this function.
- PR comments: Tag
@coderabbitaiin 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 pauseto pause the reviews on a PR.@coderabbitai resumeto resume the paused reviews.@coderabbitai reviewto trigger an incremental review. This is useful when automatic reviews are disabled for the repository.@coderabbitai full reviewto do a full review from scratch and review all the files again.@coderabbitai summaryto regenerate the summary of the PR.@coderabbitai generate docstringsto generate docstrings for this PR.@coderabbitai generate sequence diagramto generate a sequence diagram of the changes in this PR.@coderabbitai generate unit teststo generate unit tests for this PR.@coderabbitai resolveresolve all the CodeRabbit review comments.@coderabbitai configurationto show the current CodeRabbit configuration for the repository.@coderabbitai helpto get help.
Other keywords and placeholders
- Add
@coderabbitai ignoreanywhere in the PR description to prevent this PR from being reviewed. - Add
@coderabbitai summaryto generate the high-level summary at a specific location in the PR description. - Add
@coderabbitaianywhere 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.