Skip to content

Upgrade typing to 3.13.5 #5850

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open

Conversation

youknowone
Copy link
Member

@youknowone youknowone commented Jun 27, 2025

Summary by CodeRabbit

  • New Features

    • Introduced an override decorator for marking methods as overrides.
    • Enhanced string representation for ParamSpec objects.
  • Improvements

    • Expanded usage examples and documentation for several typing utilities, including get_origin, get_args, TypedDict, and get_protocol_members.
    • Improved error reporting for protocol subclass checks.
    • Updated internal handling of type alias definitions to better support PEP 695 syntax.
  • Bug Fixes

    • Adjusted type alias compilation and execution to properly handle cases with or without type parameters.
    • Fixed test metadata for typing-related tests.

Copy link

coderabbitai bot commented Jun 27, 2025

Walkthrough

This update introduces docstring examples and the override decorator in Lib/typing.py, corrects protocol subclass checks, and adjusts TypedDict and ParamSpec representations. The Rust backend is updated to handle PEP 695 type alias compilation and runtime, and ParamSpec gains a custom string representation. Two typing tests are re-enabled.

Changes

File(s) Change Summary
Lib/test/test_typing.py Removed @unittest.expectedFailure and related comments from two test methods.
Lib/typing.py Added docstring examples, implemented override decorator, fixed protocol subclass checks, enhanced TypedDict and ParamSpec documentation, and improved get_protocol_members docstring.
compiler/codegen/src/compile.rs Updated type alias compilation logic to handle PEP 695 type parameters and stack order.
vm/src/frame.rs Modified TypeAlias instruction handling to accept None or tuple for type parameters.
vm/src/stdlib/typing.rs Added Representable trait and string representation for ParamSpec.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant PythonCode
    participant RustCompiler
    participant VM

    User->>PythonCode: Define type alias with type parameters (PEP 695)
    PythonCode->>RustCompiler: Compile type alias statement
    RustCompiler->>RustCompiler: Push symbol table, compile type parameters, compile value, reorder stack, emit TypeAlias
    RustCompiler->>VM: Bytecode with TypeAlias instruction
    VM->>VM: Handle TypeAlias, accept None/tuple for type_params, create TypeAliasType
Loading

Possibly related PRs

  • RustPython/RustPython#5840: Removes @unittest.expectedFailure decorators and related comments in Lib/test/test_typing.py, similar to this PR's changes.
  • RustPython/RustPython#5844: Removes expected failure decorators from protocol-related typing tests, indicating related test enablement.
  • RustPython/RustPython#5837: Removes expected failure decorators from various typing tests, overlapping with this PR's test metadata updates.

Suggested reviewers

  • arihant2math

Poem

In the warren where type rabbits dwell,
Docstrings bloom and examples swell.
Protocols checked, ParamSpecs shine,
Rust and Python intertwine.
With override and tests anew,
This patch hops forward—typing grew!
🐇✨

✨ 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.

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

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

1267-1285: LGTM! Excellent CPython compatibility improvement.

The implementation correctly handles both None and tuple values for type parameters, matching CPython's behavior for PEP 695 type aliasing. The logic is clear and well-commented.

Minor suggestion: Consider updating the error message to be more descriptive:

-            .map_err(|_| vm.new_type_error("Type params must be a tuple."))?
+            .map_err(|_| vm.new_type_error("Type params must be a tuple or None."))?
vm/src/stdlib/typing.rs (1)

558-564: Minor inefficiency & potential borrow bug in repr_str

Calling zelf.__name__() goes through the Python attribute machinery and relies on auto-deref from Py<…> to &ParamSpec.
Directly reading the stored field, as done for TypeVar / TypeVarTuple, avoids that overhead and side-steps any future borrow-checker surprises.

-            let name = zelf.__name__().str(vm)?;
-            Ok(format!("~{}", name))
+            let name = zelf.name.str(vm)?;
+            Ok(format!("~{name}"))

No change in behaviour; only leaner and more idiomatic.

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between f5ccd4f and fd52673.

📒 Files selected for processing (5)
  • Lib/test/test_typing.py (0 hunks)
  • Lib/typing.py (8 hunks)
  • compiler/codegen/src/compile.rs (1 hunks)
  • vm/src/frame.rs (1 hunks)
  • vm/src/stdlib/typing.rs (2 hunks)
💤 Files with no reviewable changes (1)
  • Lib/test/test_typing.py
🧰 Additional context used
📓 Path-based instructions (1)
`Lib/**/*`: Files in the Lib/ directory (Python standard library copied from CPy...

Lib/**/*: Files in the Lib/ directory (Python standard library copied from CPython) should be edited very conservatively; modifications should be minimal and only to work around RustPython limitations.

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

List of files the instruction was applied to:

  • Lib/typing.py
🧬 Code Graph Analysis (1)
vm/src/stdlib/typing.rs (4)
vm/src/builtins/type.rs (4)
  • repr_str (1301-1320)
  • zelf (1261-1261)
  • zelf (1263-1263)
  • name (430-435)
vm/src/builtins/function.rs (2)
  • repr_str (570-576)
  • repr_str (803-817)
vm/src/builtins/genericalias.rs (1)
  • repr_str (488-490)
vm/src/builtins/descriptor.rs (2)
  • repr_str (148-154)
  • repr_str (331-337)
⏰ Context from checks skipped due to timeout of 90000ms (9)
  • GitHub Check: Check Rust code with rustfmt and clippy
  • GitHub Check: Run rust tests (windows-latest)
  • GitHub Check: Run snippets and cpython tests on wasm-wasi
  • GitHub Check: Run snippets and cpython tests (ubuntu-latest)
  • GitHub Check: Check the WASM package and demo
  • GitHub Check: Run rust tests (macos-latest)
  • GitHub Check: Run snippets and cpython tests (windows-latest)
  • GitHub Check: Run snippets and cpython tests (macos-latest)
  • GitHub Check: Ensure compilation on various targets
🔇 Additional comments (12)
vm/src/stdlib/typing.rs (1)

346-347: ParamSpec now advertises Representable – good addition

Exposing a custom __repr__ on ParamSpec brings it in-line with TypeVar/TypeVarTuple, avoids falling back to the generic object repr, and unblocks doctest expectations.
Looks correct and self-contained.

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

1045-1062: Well-implemented PEP 695 type parameter handling!

The implementation correctly handles the complex compilation sequence for type aliases with type parameters:

  1. Symbol table management: Properly pushes and pops symbol table for type parameter scope
  2. Compilation order: Type parameters compiled first to make them available during value expression compilation
  3. Stack manipulation: Uses Rotate2 to reorder stack from [type_params, value] to [value, type_params] as required by TypeAlias instruction

The extensive comments clearly explain the rationale and stack states, making this complex compilation logic maintainable.


1063-1068: Correct handling of type aliases without parameters.

The else branch properly handles the simpler case where no type parameters are present:

  • Compiles value expression first
  • Pushes None (not empty tuple) to represent absence of type parameters, matching CPython behavior

This ensures consistent stack layout for the TypeAlias instruction regardless of whether type parameters are present.


1070-1076: Final stack preparation and instruction emission looks correct.

The code correctly:

  1. Pushes the alias name as the final stack element
  2. Emits the TypeAlias instruction with the properly ordered stack: [value, type_params_or_none, name]
  3. Stores the resulting type alias using the name

This completes the PEP 695 type alias compilation implementation properly.

Lib/typing.py (8)

223-224: LGTM! Good documentation enhancement.

The ParamSpec examples in the docstring clearly demonstrate the intended behavior and align with the function's purpose.


268-269: LGTM! Clear documentation examples.

The examples effectively demonstrate how _collect_type_parameters works with ParamSpec and TypeVar combinations.


2097-2101: LGTM! Proper protocol subclass checking restored.

This change activates previously commented-out code that correctly raises TypeError when protocols with non-method members are used with issubclass(), providing a helpful error message with sorted member names.


534-544: LGTM! Comprehensive documentation examples.

The examples cover a wide range of type constructs and clearly demonstrate get_origin's behavior across different scenarios.


567-570: LGTM! Clear examples for complex type argument extraction.

The examples effectively demonstrate get_args's behavior with Union simplification, TypeVar substitution, and Callable argument extraction.


248-258: LGTM! Practical TypedDict usage examples.

The examples clearly demonstrate TypedDict class definition, type checking behavior, and runtime equality with regular dictionaries.


3715-3751: Consider if this addition aligns with conservative editing guidelines.

The override decorator implementation is well-written and follows established patterns (similar to the final decorator), but this represents a significant new feature addition to the stdlib code. Given the coding guidelines stating that "modifications should be minimal and only to work around RustPython limitations," this change may be more substantial than intended for conservative editing.

However, if this is part of coordinated PEP 698 support implementation, it appears correctly implemented with proper error handling and documentation.

Could you verify if adding the override decorator aligns with the project's conservative editing approach for stdlib files, or if this is part of a broader PEP 698 implementation effort?


777-785: LGTM! Clear protocol members example.

The example effectively demonstrates how get_protocol_members works with a Protocol class and shows the expected frozenset return value.

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