8000 typing __parameters__ __type_params__ by youknowone · Pull Request #5909 · RustPython/RustPython · GitHub
[go: up one dir, main page]

Skip to content

typing __parameters__ __type_params__ #5909

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 3 commits into from
Jul 7, 2025

Conversation

youknowone
Copy link
Member
@youknowone youknowone commented Jul 7, 2025

Summary by CodeRabbit

  • New Features

    • Added support for default values in type parameters, aligning with PEP 695.
    • Classes with type parameters now automatically use a generic base if no explicit base is provided.
    • Type parameters and generic classes now better support Python's typing semantics.
  • Bug Fixes

    • Improved thread safety for type parameter default values.
    • Enhanced compatibility with the typing module by setting additional class attributes.
  • Chores

    • Internal improvements for intrinsic instructions and synchronization primitives.

Copy link
Contributor
coderabbitai bot commented Jul 7, 2025

Walkthrough

This update extends the compiler and runtime to support default values for type parameters and automatic insertion of generic base classes in line with PEP 695. It introduces new bytecode instructions, intrinsic function handling, synchronization for type parameter defaults, and updates class construction to manage type parameter attributes and defaults.

Changes

File(s) Change Summary
compiler/codegen/src/compile.rs Enhanced type parameter compilation to support defaults per PEP 695; updated class definition logic for automatic generic base insertion and deferred symbol table popping.
compiler/core/src/bytecode.rs Added enums for intrinsic functions, new bytecode instructions (CallIntrinsic1, CallIntrinsic2), and updated stack effect and instruction display logic.
vm/src/builtins/genericalias.rs Added subscript_generic function to create a GenericAlias from type parameters, mimicking CPython’s internal behavior.
vm/src/frame.rs Implemented execution for new intrinsic call instructions; added methods for intrinsic dispatch; minor fix in ParamSpec handling.
vm/src/stdlib/builtins.rs Modified __build_class__ to set both __type_params__ (cloned) and new __parameters__ attributes on classes for typing compatibility.
vm/src/stdlib/typevar.rs Changed evaluate_default fields to use PyMutex for thread-safe access; added set_typeparam_default function; adjusted Generic.__class_getitem__ argument unpacking.
vm/src/stdlib/typing.rs Re-exported set_typeparam_default from typevar module for public use.

Sequence Diagram(s)

sequenceDiagram
    participant Compiler
    participant VM
    participant Typing
    participant TypeParam

    Compiler->>VM: Emit instructions for type parameters
    alt Default value present
        Compiler->>VM: CallIntrinsic2(SetTypeparamDefault) with TypeParam, default
        VM->>Typing: set_typeparam_default(TypeParam, default)
        Typing-->>VM: Default set on TypeParam
    end
    Compiler->>VM: Emit class construction instructions
    alt TypeParams exist and no explicit bases
        Compiler->>VM: CallIntrinsic1(SubscriptGeneric) with type_params
        VM->>Typing: subscript_generic(type_params)
        Typing-->>VM: GenericAlias returned
        Compiler->>VM: Call __build_class__ with GenericAlias as base
    else Explicit bases present
        Compiler->>VM: Call __build_class__ with explicit bases
    else No type params
        Compiler->>VM: Call __build_class__ normally
    end
Loading

Possibly related PRs

  • __type_params__ in __build_class__ #5883: Refactors type parameter handling in the compiler, including special symbol table management and class/function compilation logic, closely related to this PR's enhancements for PEP 695 support.

  • Fix TypeParams, TypeAlias compile #5862: Fixes compilation and execution of type parameters specifically for TypeAlias; related by modifying compile.rs but focusing on different constructs than this PR.

Suggested reviewers

  • arihant2math

Poem

In the warren where code bunnies dwell,
Type parameters now have defaults as well!
With intrinsic bytecode, they hop in line,
And classes generic—oh, how they shine!
A tuple of types, a carrot of base,
PEP 695’s magic fills the place.
🥕✨


📜 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 f5d036e and bd54e53.

⛔ Files ignored due to path filters (2)
  • Lib/test/test_module/__init__.py is excluded by !Lib/**
  • Lib/test/test_typing.py is excluded by !Lib/**
📒 Files selected for processing (6)
  • compiler/codegen/src/compile.rs (2 hunks)
  • compiler/core/src/bytecode.rs (4 hunks)
  • vm/src/builtins/genericalias.rs (1 hunks)
  • vm/src/frame.rs (3 hunks)
  • vm/src/stdlib/typevar.rs (18 hunks)
  • vm/src/stdlib/typing.rs (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (5)
  • vm/src/builtins/genericalias.rs
  • vm/src/stdlib/typing.rs
  • compiler/core/src/bytecode.rs
  • vm/src/frame.rs
  • compiler/codegen/src/compile.rs
🧰 Additional context used
📓 Path-based instructions (1)
`**/*.rs`: Follow the default rustfmt code style (`cargo fmt` to format) Always ...

**/*.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

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

List of files the instruction was applied to:

  • vm/src/stdlib/typevar.rs
🧠 Learnings (1)
vm/src/stdlib/typevar.rs (1)
Learnt from: CR
PR: RustPython/RustPython#0
File: .github/copilot-instructions.md:0-0
Timestamp: 2025-06-30T10:08:48.858Z
Learning: Applies to **/*.rs : Use the macro system (`pyclass`, `pymodule`, `pyfunction`, etc.) when implementing Python functionality in Rust
⏰ 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). (11)
  • GitHub Check: Run snippets and cpython tests on wasm-wasi
  • GitHub Check: Run snippets and cpython tests (windows-latest)
  • GitHub Check: Check the WASM package and demo
  • GitHub Check: Run tests under miri
  • GitHub Check: Run rust tests (windows-latest)
  • GitHub Check: Run snippets and cpython tests (macos-latest)
  • GitHub Check: Run snippets and cpython tests (ubuntu-latest)
  • GitHub Check: Check Rust code with rustfmt and clippy
  • GitHub Check: Run rust tests (ubuntu-latest)
  • GitHub Check: Run rust tests (macos-latest)
  • GitHub Check: Ensure compilation on various targets
🔇 Additional comments (5)
vm/src/stdlib/typevar.rs (5)

5-5: LGTM: Thread safety improvements for TypeVar.

The conversion of evaluate_default to PyMutex<PyObjectRef> aligns with the existing thread safety pattern used by other fields like bound and constraints. The access patterns are correctly updated to use .lock() throughout the implementation.

Also applies to: 80-80, 149-152, 176-176, 363-363, 389-390


404-405: LGTM: ParamSpec field type changes and thread safety.

The changes correctly handle the transition from Option<PyObjectRef> to PyObjectRef for default_value, using the NoDefault singleton pattern. The thread safety improvements with PyMutex are consistent with other type parameter implementations.

Also applies to: 466-478, 481-483, 491-497, 591-591, 597-597, 619-629


638-638: LGTM: TypeVarTuple thread safety improvements.

The thread safety changes for evaluate_default are consistent with the pattern used in TypeVar and ParamSpec implementations.

Also applies to: 654-657, 666-666, 758-758, 781-781


974-976: LGTM: Fixed CPython compatibility in Generic.class_getitem.

The change to delegate to the typing module's _generic_class_getitem function addresses the previous compatibility issue with tuple unwrapping. This approach ensures behavior matches CPython's implementation.


984-1022: LGTM: Well-implemented set_typeparam_default function.

The implementation is clean and follows good Rust practices:

  • Uses a generic helper function to reduce code duplication
  • Properly handles all three type parameter types (TypeVar, ParamSpec, TypeVarTuple)
  • Correctly uses mutex locks for thread safety
  • Appropriate error handling for invalid type parameters
✨ Finishing Touches
  • 📝 Generate Docstrings

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 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
@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Summary of Changes

Hello @youknowone, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly advances the implementation of PEP 695 (Type Parameter Syntax) by introducing support for default values in type parameters and handling the implicit Generic base class for generic types. These changes involve extending the bytecode instruction set, adding new runtime intrinsic functions, and refactoring the internal representation of type parameters for improved consistency and functionality.

Highlights

  • PEP 695 Type Parameter Defaults: Added compiler and runtime support for specifying default values for TypeVar, ParamSpec, and TypeVarTuple instances, aligning with PEP 695. This includes compiling default expressions and setting them via a new intrinsic.
  • Automatic Generic Base for PEP 695 Classes: Implemented logic to automatically inject typing.Generic[*type_params] as a base class for generic classes declared with PEP 695 syntax when no explicit base classes are provided, mirroring CPython's behavior.
  • New Bytecode Instructions for Type Operations: Introduced CALL_INTRINSIC_1 and CALL_INTRINSIC_2 bytecode instructions, specifically for SubscriptGeneric (to handle Generic[*type_params] creation) and SetTypeparamDefault (to set type parameter defaults at runtime).
  • Refactored Type Parameter Storage: Updated the internal TypeVar, ParamSpec, and TypeVarTuple structures to use PyMutex<PyObjectRef> for evaluate_default fields, ensuring thread-safe access and consistent handling of default values.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in issue comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist is currently in preview and may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments to provide feedback.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces PEP 695 features, specifically handling default values in type parameters and generic class creation. The changes are well-structured, introducing new bytecode intrinsics and updating the VM to support them. However, there's a critical stack manipulation bug in the compiler when handling default values for type parameters, which will lead to runtime errors.

@youknowone youknowone force-pushed the typing-parameters branch from 4f9c355 to 2ffdc88 Compare July 7, 2025 12:25
@youknowone youknowone marked this pull request as ready for review July 7, 2025 12:25
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

♻️ Duplicate comments (3)
compiler/codegen/src/compile.rs (3)

1237-1250: Remove the unnecessary Duplicate instruction to prevent stale object on stack.

The first Duplicate instruction will leave a stale TypeVar object on the stack. The SetTypeparamDefault intrinsic consumes the type parameter and default value, then pushes back the modified type parameter.

Apply this diff to fix the issue:

                    // Handle default value if present (PEP 695)
                    if let Some(default_expr) = default {
-                       emit!(self, Instruction::Duplicate);
-
                        // Compile the default expression
                        self.compile_expression(default_expr)?;

                        emit!(
                            self,
                            Instruction::CallIntrinsic2 {
                                func: bytecode::IntrinsicFunction2::SetTypeparamDefault
                            }
                        );
                    }

1261-1274: Remove the unnecessary Duplicate instruction to prevent stale object on stack.

The first Duplicate instruction will leave a stale ParamSpec object on the stack. The SetTypeparamDefault intrinsic consumes the type parameter and default value, then pushes back the modified type parameter.

Apply this diff to fix the issue:

                    // Handle default value if present (PEP 695)
                    if let Some(default_expr) = default {
-                       emit!(self, Instruction::Duplicate);
-
                        // Compile the default expression
                        self.compile_expression(default_expr)?;

                        emit!(
                            self,
                            Instruction::CallIntrinsic2 {
                                func: bytecode::IntrinsicFunction2::SetTypeparamDefault
                            }
                        );
                    }

1285-1302: Remove the unnecessary Duplicate instruction to prevent stale object on stack.

The first Duplicate instruction will leave a stale TypeVarTuple object on the stack. The SetTypeparamDefault intrinsic consumes the type parameter and default value, then pushes back the modified type parameter.

Apply this diff to fix the issue:

                    // Handle default value if present (PEP 695)
                    if let Some(default_expr) = default {
-                       emit!(self, Instruction::Duplicate);
-
                        // Compile the default expression
                        self.compile_expression(default_expr)?;

                        // Handle starred expression (*default)
                        // CPython handles the unpacking during runtime evaluation of the default value
                        // We don't need special intrinsic for this - just compile the expression normally

                        emit!(
                            self,
                            Instruction::CallIntrinsic2 {
                                func: bytecode::IntrinsicFunction2::SetTypeparamDefault
                            }
                        );
                    }
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 7ebe018 and 2ffdc88.

⛔ Files ignored due to path filters (12)
  • Lib/test/_typed_dict_helper.py is excluded by !Lib/**
  • Lib/test/ann_module.py is excluded by !Lib/**
  • Lib/test/ann_module2.py is excluded by !Lib/**
  • Lib/test/ann_module3.py is excluded by !Lib/**
  • Lib/test/ann_module4.py is excluded by !Lib/**
  • Lib/test/ann_module5.py is excluded by !Lib/**
  • Lib/test/ann_module6.py is excluded by !Lib/**
  • Lib/test/ann_module7.py is excluded by !Lib/**
  • Lib/test/mod_generics_cache.py is excluded by !Lib/**
  • Lib/test/test_grammar.py is excluded by !Lib/**
  • Lib/test/test_typing.py is excluded by !Lib/**
  • Lib/test/typinganndata/mod_generics_cache.py is excluded by !Lib/**
📒 Files selected for processing (7)
  • compiler/codegen/src/compile.rs (2 hunks)
  • compiler/core/src/bytecode.rs (4 hunks)
  • vm/src/builtins/genericalias.rs (1 hunks)
  • vm/src/frame.rs (3 hunks)
  • vm/src/stdlib/builtins.rs (1 hunks)
  • vm/src/stdlib/typevar.rs (19 hunks)
  • vm/src/stdlib/typing.rs (1 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
`**/*.rs`: Follow the default rustfmt code style (`cargo fmt` to format) Always ...

**/*.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

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

List of files the instruction was applied to:

  • vm/src/stdlib/builtins.rs
  • vm/src/stdlib/typing.rs
  • vm/src/builtins/genericalias.rs
  • vm/src/frame.rs
  • compiler/core/src/bytecode.rs
  • compiler/codegen/src/compile.rs
  • vm/src/stdlib/typevar.rs
🧠 Learnings (1)
vm/src/stdlib/typevar.rs (1)
Learnt from: CR
PR: RustPython/RustPython#0
File: .github/copilot-instructions.md:0-0
Timestamp: 2025-06-30T10:08:48.858Z
Learning: Applies to **/*.rs : Use the macro system (`pyclass`, `pymodule`, `pyfunction`, etc.) when implementing Python functionality in Rust
🧬 Code Graph Analysis (2)
vm/src/stdlib/typing.rs (1)
vm/src/stdlib/typevar.rs (1)
  • set_typeparam_default (1019-1055)
vm/src/frame.rs (2)
vm/src/builtins/genericalias.rs (1)
  • subscript_generic (619-633)
vm/src/stdlib/typevar.rs (1)
  • set_typeparam_default (1019-1055)
⏰ 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). (11)
  • GitHub Check: Run snippets and cpython tests on wasm-wasi
  • GitHub Check: Check the WASM package and demo
  • GitHub Check: Check Rust code with rustfmt and clippy
  • GitHub Check: Run snippets and cpython tests (windows-latest)
  • GitHub Check: Run tests under miri
  • GitHub Check: Run rust tests (macos-latest)
  • GitHub Check: Run rust tests (ubuntu-latest)
  • GitHub Check: Run snippets and cpython tests (ubuntu-latest)
  • GitHub Check: Run snippets and cpython tests (macos-latest)
  • GitHub Check: Run rust tests (windows-latest)
  • GitHub Check: Ensure compilation on various targets
🔇 Additional comments (17)
vm/src/stdlib/typing.rs (1)

6-6: LGTM! Clean re-export for PEP 695 support.

The addition of set_typeparam_default to the public exports enables external access to the type parameter default functionality, which aligns with the broader PEP 695 implementation for default type parameters.

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

616-633: Excellent implementation of CPython's _Py_subscript_generic equivalent.

The function correctly handles both tuple and single parameter cases, has proper error handling, and includes clear documentation about its purpose for PEP 695 generic class support. The implementation follows Rust best practices and mirrors the expected CPython behavior.

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

1012-1014: Good addition for typing module compatibility.

The implementation correctly sets both __type_params__ and __parameters__ attributes for PEP 695 classes. The comment clearly explains the rationale for the dual attribute setup, and the error handling is appropriate.

vm/src/frame.rs (5)

1053-1058: LGTM! Clean implementation of CallIntrinsic1 instruction.

The instruction correctly manages the stack (pop input, push result) and follows established patterns for bytecode execution with proper error handling.


1059-1065: LGTM! Correct implementation of CallIntrinsic2 instruction.

The stack operations are properly ordered (value2 popped first, then value1) and the implementation follows established bytecode execution patterns.


2260-2272: LGTM! Proper intrinsic function dispatch implementation.

The method correctly matches on the intrinsic function enum and delegates to the appropriate implementation. The function signature matches the called subscript_generic function.


2274-2286: LGTM! Correct implementation of two-argument intrinsic dispatch.

The method properly handles the two-argument case and delegates to set_typeparam_default with the correct parameter order and types.


1304-1304: LGTM! Bug fix for missing vm parameter.

The addition of the vm parameter to ParamSpec::new correctly aligns with the expected function signature.

compiler/core/src/bytecode.rs (5)

378-405: LGTM! Well-structured intrinsic function enum.

The IntrinsicFunction1 enum follows established patterns with appropriate derives and #[repr(u8)] for bytecode compatibility. The commented variants suggest a planned but gradual implementation approach for PEP 695 features.


407-419: LGTM! Complementary enum for two-argument intrinsics.

The IntrinsicFunction2 enum mirrors the structure of IntrinsicFunction1 and properly implements the OpArgType trait via the macro. The SetTypeparamDefault = 5 variant aligns with the PEP 695 type parameter default functionality.


500-505: LGTM! Proper integration into instruction enum.

The new CallIntrinsic1 and CallIntrinsic2 instruction variants are correctly integrated into the Instruction enum with appropriate argument types. The placement maintains the existing instruction organization.


1287-1288: LGTM! Correct stack effect calculations.

The stack effects are properly calculated:

  • CallIntrinsic1: 0 (consumes 1, pushes 1)
  • CallIntrinsic2: -1 (consumes 2, pushes 1)

These align with the expected behavior for single and binary intrinsic operations.


1498-1499: LGTM! Consistent display formatting.

The display formatting uses the established w! macro pattern with debug formatting (?func) for the intrinsic function arguments, maintaining consistency with other instruction display implementations.

vm/src/stdlib/typevar.rs (4)

5-5: LGTM!

The import of PyMutex is correctly added to support the synchronization primitive change throughout the file.


80-80: Consistent synchronization primitive migration.

The migration from parking_lot::Mutex to PyMutex for evaluate_default fields is implemented consistently across all type parameter structs (TypeVar, ParamSpec, TypeVarTuple). All access points properly acquire locks before use, ensuring thread-safe access.

Also applies to: 149-151, 176-177, 364-364, 390-391, 406-406, 474-476, 485-487, 499-500, 614-614, 640-641, 655-655, 671-673, 683-684, 776-776, 799-799


1017-1055: Well-implemented type parameter default setter.

The set_typeparam_default function is elegantly implemented with proper error handling and code reuse through the generic try_set_default inner function. The function correctly handles all three type parameter types and provides clear error messages.


636-636: Document the breaking API change for ParamSpec::new

  • Verified all internal calls to ParamSpec::new (e.g. in vm/src/frame.rs) now include the new vm parameter.
  • No code changes required in the repository itself.
  • Please update any external consumers, extension modules, and release notes to reflect that ParamSpec::new now requires a vm: &VirtualMachine argument.

@youknowone youknowone force-pushed the typing-parameters branch 3 times, most recently from a57d350 to f5d036e Compare July 7, 2025 14:17
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

♻️ Duplicate comments (3)
compiler/codegen/src/compile.rs (3)

1237-1251: Remove the Duplicate instruction - it creates a stale stack object.

The intrinsic SetTypeparamDefault consumes both the type parameter and the default value, then pushes back the modified type parameter. Adding a Duplicate instruction here will leave a stale object on the stack.

This matches the previous review feedback about removing the Duplicate instruction to prevent stack issues.

-                    emit!(
-                        self,
-                        Instruction::CallIntrinsic2 {
-                            func: bytecode::IntrinsicFunction2::SetTypeparamDefault
-                        }
-                    );
-
-                    emit!(self, Instruction::Duplicate);
+                    emit!(
+                        self,
+                        Instruction::CallIntrinsic2 {
+                            func: bytecode::IntrinsicFunction2::SetTypeparamDefault
+                        }
+                    );

1259-1273: Remove the Duplicate instruction - it creates a stale stack object.

The intrinsic SetTypeparamDefault consumes both the type parameter and the default value, then pushes back the modified type parameter. Adding a Duplicate instruction here will leave a stale object on the stack.

This matches the previous review feedback about removing the Duplicate instruction to prevent stack issues.

-                    emit!(
-                        self,
-                        Instruction::CallIntrinsic2 {
-                            func: bytecode::IntrinsicFunction2::SetTypeparamDefault
-                        }
-                    );
-
-                    emit!(self, Instruction::Duplicate);
+                    emit!(
+                        self,
+                        Instruction::CallIntrinsic2 {
+                            func: bytecode::IntrinsicFunction2::SetTypeparamDefault
+                        }
+                    );

1281-1299: Remove the Duplicate instruction - it creates a stale stack object.

The intrinsic SetTypeparamDefault consumes both the type parameter and the default value, then pushes back the modified type parameter. Adding a Duplicate instruction here will leave a stale object on the stack.

This matches the previous review feedback about removing the Duplicate instruction to prevent stack issues.

-                    emit!(
-                        self,
-                        Instruction::CallIntrinsic2 {
-                            func: bytecode::IntrinsicFunction2::SetTypeparamDefault
-                        }
-                    );
-
-                    emit!(self, Instruction::Duplicate);
+                    emit!(
+                        self,
+                        Instruction::CallIntrinsic2 {
+                            func: bytecode::IntrinsicFunction2::SetTypeparamDefault
+                        }
+                    );
🧹 Nitpick comments (1)
compiler/codegen/src/compile.rs (1)

1286-1288: Clarify the comment about starred expressions in TypeVarTuple defaults.

The comment states "CPython handles the unpacking during runtime evaluation of the default value" and "We don't need special intrinsic for this". This could be clearer about why no special handling is needed and how the starred expression is properly handled.

-                        // Handle starred expression (*default)
-                        // CPython handles the unpacking during runtime evaluation of the default value
-                        // We don't need special intrinsic for this - just compile the expression normally
+                        // Handle starred expression (*default) 
+                        // The starred expression is compiled normally as CPython handles
+                        // the unpacking during runtime evaluation of the default value
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between a57d350 and f5d036e.

⛔ Files ignored due to path filters (2)
  • Lib/test/test_module/__init__.py is excluded by !Lib/**
  • Lib/test/test_typing.py is excluded by !Lib/**
📒 Files selected for processing (6)
  • compiler/codegen/src/compile.rs (2 hunks)
  • compiler/core/src/bytecode.rs (4 hunks)
  • vm/src/builtins/genericalias.rs (1 hunks)
  • vm/src/frame.rs (3 hunks)
  • vm/src/stdlib/typevar.rs (18 hunks)
  • vm/src/stdlib/typing.rs (1 hunks)
✅ Files skipped from review due to trivial changes (1)
  • vm/src/stdlib/typing.rs
🚧 Files skipped from review as they are similar to previous changes (4)
  • vm/src/builtins/genericalias.rs
  • vm/src/frame.rs
  • compiler/core/src/bytecode.rs
  • vm/src/stdlib/typevar.rs
🧰 Additional context used
📓 Path-based instructions (1)
`**/*.rs`: Follow the default rustfmt code style (`cargo fmt` to format) Always ...

**/*.rs: Follow the default rustfmt code style (cargo fmt to format)
Always run clippy to lint code (cargo clippy) before 8000 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

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

List of files the instruction was applied to:

  • compiler/codegen/src/compile.rs
⏰ 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). (11)
  • GitHub Check: Run snippets and cpython tests (macos-latest)
  • GitHub Check: Run tests under miri
  • GitHub Check: Run snippets and cpython tests (windows-latest)
  • GitHub Check: Check the WASM package and demo
  • GitHub Check: Run snippets and cpython tests on wasm-wasi
  • GitHub Check: Check Rust code with rustfmt and clippy
  • GitHub Check: Run snippets and cpython tests (ubuntu-latest)
  • GitHub Check: Run rust tests (macos-latest)
  • GitHub Check: Run rust tests (windows-latest)
  • GitHub Check: Run rust tests (ubuntu-latest)
  • GitHub Check: Ensure compilation on various targets
🔇 Additional comments (1)
compiler/codegen/src/compile.rs (1)

1810-1844: Verify PEP 695 Generic base insertion logic matches CPython

I didn’t find any existing tests in RustPython or CPython’s test suite that exercise this—please cross-check CPython’s implementation in Python/compile.c (the call to build_class) to ensure that:

  • Generic[*type_params] is only injected when no explicit bases are provided
  • No Generic is added if the class has any user-specified bases

Locations to review:

  • compiler/codegen/src/compile.rs, lines 1810–1844

Consider adding tests for both scenarios to lock in the correct behavior:

  • class C[T]: … → implicitly inherits from Generic[T]
  • class C[T](Base): … → no automatic Generic insertion

@youknowone youknowone force-pushed the typing-parameters branch from f5d036e to bd54e53 Compare July 7, 2025 14:23
@youknowone youknowone merged commit ec577e5 into RustPython:main Jul 7, 2025
12 checks passed
@youknowone youknowone deleted the typing-parameters branch July 7, 2025 15:08
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
0