Skip to content

Deprecate ::new_ref #6046

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 29, 2025
Merged

Conversation

youknowone
Copy link
Member

@youknowone youknowone commented Jul 29, 2025

Summary by CodeRabbit

  • New Features

    • Added a new constructor method for static methods, allowing easier creation of static method instances.
  • Bug Fixes

    • None.
  • Refactor

    • Simplified several object creation methods to use a more consistent and modern approach for creating references.
    • Consolidated internal helper methods for complex number operations.
    • Updated namespace creation to use a new reference conversion method.
  • Style

    • Marked various object creation methods as deprecated, guiding users to preferred alternatives for better code clarity and future compatibility.
    • Added clarifying comments emphasizing important methods.
  • Documentation

    • Added deprecation notes to several methods, providing clear guidance on updated usage patterns.

Copy link
Contributor

coderabbitai bot commented Jul 29, 2025

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.

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: 1

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 57029f6 and eea1a1f.

📒 Files selected for processing (9)
  • 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/staticmethod.rs (1 hunks)
  • vm/src/builtins/str.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 (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/builtins/staticmethod.rs
  • vm/src/builtins/dict.rs
  • vm/src/builtins/bytes.rs
  • vm/src/builtins/bytearray.rs
  • vm/src/builtins/complex.rs
  • vm/src/builtins/str.rs
  • vm/src/builtins/list.rs
  • vm/src/builtins/function.rs
  • vm/src/builtins/classmethod.rs
🔇 Additional comments (11)
vm/src/builtins/dict.rs (1)

54-57: LGTM! Clean deprecation and simplification.

The deprecation note correctly guides users to the new pattern, and the implementation has been properly simplified to use Self::default().into_ref(ctx) directly. This is consistent with the broader refactoring effort.

vm/src/builtins/bytes.rs (1)

102-105: LGTM! Consistent with the refactoring pattern.

The deprecation note correctly guides users to the new PyBytes::from(...).into_ref() pattern, and the implementation has been properly simplified to use exactly that approach. This change is consistent with the broader refactoring effort across built-in types.

vm/src/builtins/bytearray.rs (1)

76-79: LGTM! Follows the established refactoring pattern.

The deprecation note correctly guides users to the new PyByteArray::from(...).into_ref() pattern, and the implementation has been properly simplified to use exactly that approach. This change is consistent with similar refactoring across other built-in types in this PR.

vm/src/builtins/str.rs (1)

421-425: LGTM: Well-implemented deprecation pattern

The deprecation is properly implemented with:

  • Clear guidance in the deprecation note directing users to use PyStr::from(...).into_ref() instead
  • Simplified implementation that delegates to into_ref(ctx) while maintaining the same functionality
  • Maintains backward compatibility during the transition period

This aligns well with the PR's objective to standardize instance creation patterns across built-in types.

vm/src/builtins/staticmethod.rs (2)

64-68: LGTM: Clean constructor implementation

The new new method provides a clean, idiomatic constructor that:

  • Takes the required PyObjectRef parameter
  • Properly wraps it in PyMutex as expected by the struct
  • Follows standard Rust constructor patterns

This serves as the foundation for the deprecation pattern being implemented across the codebase.


69-72: LGTM: Consistent deprecation implementation

The deprecation follows the established pattern:

  • Clear deprecation note directing users to PyStaticMethod::new(...).into_ref()
  • Simplified implementation that delegates to the new constructor method
  • Maintains backward compatibility during the transition

This is consistent with the deprecation pattern being applied across all built-in types in this PR.

vm/src/builtins/function.rs (1)

776-779: LGTM: Consistent deprecation pattern applied

The deprecation of PyBoundMethod::new_ref properly follows the established pattern:

  • Clear deprecation note with specific guidance to use Self::new(object, function).into_ref(ctx)
  • Simplified implementation that leverages the existing Self::new constructor
  • Maintains backward compatibility during the transition period

This completes the consistent application of the deprecation pattern across built-in types as outlined in the PR objectives.

vm/src/builtins/classmethod.rs (1)

114-117: LGTM! Clean deprecation implementation.

The deprecation follows Rust best practices with a clear migration path. The implementation correctly uses the idiomatic Self::from(callable).into_ref(ctx) pattern, which aligns with the PR objective to standardize constructor methods across builtin types.

vm/src/builtins/complex.rs (3)

231-234: LGTM! Consistent deprecation implementation.

The deprecation follows the same pattern as other builtin types in this PR, providing a clear migration path with the idiomatic Self::from(value).into_ref(ctx) pattern.


236-238: Good optimization with const fn.

Making to_complex64 a const fn is a nice optimization that allows compile-time evaluation when possible, while maintaining the same functionality.


244-254: Good refactoring with helper method.

The number_op helper method consolidates the common pattern of converting two PyObject references to Complex64 values and applying a binary operation. This reduces code duplication and improves maintainability.

@youknowone youknowone merged commit f402dee into RustPython:main Jul 29, 2025
12 checks passed
@youknowone youknowone deleted the deprecate-new-ref branch July 29, 2025 16:09
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