Skip to content

Fix TypeParams, TypeAlias compile #5862

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
Jun 29, 2025
Merged

Conversation

youknowone
Copy link
Member

@youknowone youknowone commented Jun 28, 2025

Summary by CodeRabbit

  • Bug Fixes
    • Improved handling of type aliases with optional type parameters, ensuring correct behavior and compatibility with PEP 695 and CPython standards.
    • Fixed stack order and processing for type aliases, allowing both None and tuple type parameters as valid inputs.

Copy link

coderabbitai bot commented Jun 28, 2025

Walkthrough

The changes update the compilation and execution of Python's TypeAlias statements to correctly support optional type parameters per PEP 695. The compiler now manages type parameter scoping and stack order, while the virtual machine's execution logic accepts either None or a tuple for type parameters, matching CPython's behavior.

Changes

File(s) Change Summary
compiler/codegen/src/compile.rs Refactored Stmt::TypeAlias compilation to handle optional type parameters and ensure correct stack order.
vm/src/frame.rs Updated TypeAlias instruction handling to accept None or tuple for type parameters, matching CPython.

Poem

In the warren where type aliases dwell,
Now with tuples or None, all is well.
The stack is in order, the scope is just right,
Type parameters hop in, no error in sight!
With PEP 695, we leap and we bound—
Hooray for the changes, so robust and sound! 🐇


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 6d30ff8 and fbc9e50.

📒 Files selected for processing (2)
  • compiler/codegen/src/compile.rs (1 hunks)
  • vm/src/frame.rs (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • vm/src/frame.rs
  • compiler/codegen/src/compile.rs
✨ 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.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

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.

@youknowone youknowone changed the title Compatibility utilities Fix TypeParams, TypeAlias compil Jun 28, 2025
@youknowone youknowone changed the title Fix TypeParams, TypeAlias compil Fix TypeParams, TypeAlias compile Jun 28, 2025
Copy link

@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

🧹 Nitpick comments (2)
vm/src/frame.rs (1)

1269-1280: Use PyTupleRef::try_from_object for a cleaner & more permissive tuple-conversion

The new None || tuple handling looks correct 👍.
You can remove the manual downcast boilerplate (and transparently accept tuple sub-classes, mirroring CPython) by leaning on the existing helper:

-    let type_params: PyTupleRef = if vm.is_none(&type_params_obj) {
-        // If None, use empty tuple (matching CPython's behavior)
-        vm.ctx.empty_tuple.clone()
-    } else {
-        type_params_obj
-            .downcast()
-            .map_err(|_| vm.new_type_error("Type params must be a tuple."))?
-    };
+    let type_params: PyTupleRef = if vm.is_none(&type_params_obj) {
+        // If None, use the shared empty tuple (CPython behaviour)
+        vm.ctx.empty_tuple.clone()
+    } else {
+        PyTupleRef::try_from_object(vm, type_params_obj)?
+    };

This keeps semantics identical while reducing code and aligning the acceptance rules with CPython’s (i.e. tuple subclasses are valid).

compiler/codegen/src/compile.rs (1)

1031-1076: LGTM! Solid PEP 695 TypeAlias implementation with minor optimization opportunity.

The TypeAlias compilation logic correctly handles both cases:

  • With type parameters: Proper symbol table management, type params compiled first (making them available for value expression), and correct stack order via Rotate2
  • Without type parameters: Clean path that uses None instead of empty tuple for CPython compatibility

The stack manipulation logic is sound and the comments clearly explain the PEP 695 compliance requirements.

Minor optimization: Consider avoiding the extra string clone:

                let name_string = name.id.to_string();
-                self.emit_load_const(ConstantData::Str {
-                    value: name_string.clone().into(),
-                });
+                self.emit_load_const(ConstantData::Str {
+                    value: name_string.as_str().into(),
+                });
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 07a04ac and 5efda1b.

📒 Files selected for processing (10)
  • compiler/codegen/src/compile.rs (1 hunks)
  • vm/src/builtins/dict.rs (1 hunks)
  • vm/src/builtins/memory.rs (2 hunks)
  • vm/src/builtins/tuple.rs (0 hunks)
  • vm/src/builtins/type.rs (4 hunks)
  • vm/src/builtins/union.rs (3 hunks)
  • vm/src/exceptions.rs (1 hunks)
  • vm/src/frame.rs (1 hunks)
  • vm/src/protocol/object.rs (1 hunks)
  • vm/src/types/structseq.rs (1 hunks)
💤 Files with no reviewable changes (1)
  • vm/src/builtins/tuple.rs
🧰 Additional context used
🧠 Learnings (2)
vm/src/builtins/memory.rs (2)
Learnt from: CR
PR: RustPython/RustPython#0
File: .github/copilot-instructions.md:0-0
Timestamp: 2025-06-26T12:52:11.138Z
Learning: Minimize modifications to CPython standard library files in the Lib/ directory; bug fixes should be made through Rust code modifications whenever possible.
Learnt from: CR
PR: RustPython/RustPython#0
File: .github/copilot-instructions.md:0-0
Timestamp: 2025-06-26T12:52:11.138Z
Learning: Follow Rust best practices for error handling and memory management in all RustPython Rust code.
vm/src/builtins/type.rs (2)
Learnt from: CR
PR: RustPython/RustPython#0
File: .github/copilot-instructions.md:0-0
Timestamp: 2025-06-26T12:52:11.138Z
Learning: Use the macro system (such as 'pyclass', 'pymodule', 'pyfunction') when implementing Python functionality in Rust for RustPython.
Learnt from: CR
PR: RustPython/RustPython#0
File: .github/copilot-instructions.md:0-0
Timestamp: 2025-06-26T12:52:11.138Z
Learning: When implementing a Python module in Rust for RustPython, use the #[pymodule] attribute and expose functions and classes using #[pyfunction], #[pyclass], and #[pymethod] as appropriate.
⏰ Context from checks skipped due to timeout of 90000ms (4)
  • GitHub Check: Run snippets and cpython tests (windows-latest)
  • GitHub Check: Check Rust code with rustfmt and clippy
  • GitHub Check: Ensure compilation on various targets
  • GitHub Check: Run rust tests (windows-latest)
🔇 Additional comments (15)
vm/src/builtins/type.rs (6)

161-168: New utility function for MRO-based subtype checking looks good.

The is_subtype_with_mro function provides a clean way to check subtype relationships by iterating through the MRO. The implementation correctly uses reference equality with item.is(b).


209-213: Proper implementation of CPython's PyType_Check equivalent.

The check method correctly implements downcasting to check if an object is an instance of PyType (or its subclass), matching CPython's PyType_Check macro behavior.


457-459: Well-implemented subtype checking method.

The is_subtype method properly uses the new is_subtype_with_mro utility with the MRO read guard. The implementation is clean and follows Rust patterns.


461-465: Correct implementation of exact type checking.

The check_exact method properly implements CPython's PyType_CheckExact behavior using downcast_ref_if_exact, which ensures the object is exactly a PyType and not a subclass.


470-470: Minor cleanup in type signature.

Removing the fully qualified path crate:: from the type parameter is a good cleanup - the unqualified PyObject is clearer and more consistent.


1244-1248: Excellent improvement to avoid recursion in subclass checking.

The change to use subclass.real_is_subclass(self.as_object(), vm) instead of calling fast_issubclass directly is a significant improvement. This matches CPython's behavior by using _PyObject_RealIsSubclass and prevents potential infinite recursion through __subclasscheck__.

vm/src/types/structseq.rs (1)

100-100: Proper replacement of fast_getitem with direct indexing.

The change from zelf.fast_getitem(i.into()) to zelf[i as usize].to_owned() correctly follows the systematic removal of the fast_getitem method. Direct indexing is more idiomatic and the .to_owned() maintains the same ownership semantics.

vm/src/protocol/object.rs (1)

407-407: Correct replacement of fast_getitem with direct indexing.

The change from tuple.fast_getitem(0).clone() to tuple[0].clone() properly replaces the method call with direct indexing. This is part of the systematic removal of fast_getitem and maintains the same semantics.

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

213-219: LGTM: Safe direct indexing replacement

The replacement of fast_getitem with direct indexing x[0].to_owned() is correct and safe. The bounds check if x.len() == 1 ensures the indexing won't panic, and the semantics are preserved.


1068-1074: LGTM: Consistent refactoring pattern

Same safe replacement pattern as the unpack_single method. Direct indexing with proper bounds checking maintains correctness while improving performance and consistency.

vm/src/exceptions.rs (1)

1669-1671: LGTM: Safe tuple element access

The replacement of fast_getitem with direct indexing location_tuple[i].to_owned() is safe due to the bounds check if location_tup_len > i. This change maintains consistency with the broader codebase refactoring effort.

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

238-243: LGTM: Clean refactoring from fast_getitem to direct indexing.

The replacement of fast_getitem calls with direct tuple indexing is correct and maintains the same semantics. The tuple has already been validated to have length 2, making direct indexing safe.

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

43-46: LGTM: Useful addition for internal access.

The new get_args method provides direct access to the internal args tuple, aligning with CPython's _Py_union_args attribute. This is a reasonable addition for internal VM use.


202-202: LGTM: Correct refactoring from fast_getitem to direct indexing.

The replacement of args.fast_getitem(0) with args[0].to_owned() is safe since this code path is only reached when args.len() == 1.


220-220: LGTM: Consistent tuple access pattern.

The change from new_args.fast_getitem(0) to new_args[0].to_owned() maintains the same semantics and is safe in this context where we know the tuple is non-empty.

@youknowone youknowone merged commit 28dff8a into RustPython:main Jun 29, 2025
12 checks passed
@youknowone youknowone deleted the patches branch June 29, 2025 02:24
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