Skip to content

Conversation

@walker84837
Copy link
Contributor

@walker84837 walker84837 commented Oct 20, 2025

This MR changes TypeAliasType so that it uses a PyStrRef object for its name field instead of a PyObjectRef.

Previously, the name field was a PyObjectRef. Although PyObjectRef is flexible, using a more specific type, like PyStrRef, improves transparency and type safety.

Changes include:

  • As mentioned, updating TypeAliasType.name from PyObjectRef to PyStrRef.
  • Changing TypeAliasType::new() and __name__ getter to handle PyStrRef.
  • Adding small type checks in:
    • vm/src/frame.rs
    • vm/src/stdlib/typing.rs
      to enforce that the TypeAliasType name is a string.
  • Since the type of name is now a PyStrRef, the repr_str implementation for TypeAliasType was simplified by using PyStrRef::as_str() directly.

Summary by CodeRabbit

  • Bug Fixes
    • Strengthened validation for TypeAliasType names so non-string names now produce a clear "TypeAliasType name must be a string" error.
    • Improved consistency of TypeAliasType construction and string representation, reducing confusion when inspecting or creating type aliases.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Oct 20, 2025

Walkthrough

Adds runtime validation ensuring TypeAliasType names are Python strings and changes the internal TypeAliasType.name field from PyObjectRef to PyStrRef, updating constructors, repr, and name handling accordingly.

Changes

Cohort / File(s) Summary
TypeAlias intrinsic validation
vm/src/frame.rs
Downcasts the provided name to PyStr; returns TypeError("TypeAliasType name must be a string") on failure before creating the TypeAliasType.
TypeAliasType type safety & API
vm/src/stdlib/typing.rs
Changes TypeAliasType.name: PyObjectRefPyStrRef; new/py_new accept/validate PyStrRef; __name__ now returns self.name.clone().into(); repr_str returns name.as_str() and ignores VM; type_params downcast handling tightened.

Sequence Diagram(s)

sequenceDiagram
    participant Caller as Intrinsic Handler (frame.rs)
    participant Typing as TypeAliasType (typing.rs)
    participant VM as VirtualMachine

    Caller->>Typing: provide name (PyObjectRef) and type_params, value
    Typing->>Typing: downcast name -> PyStr
    alt downcast succeeds
        Typing->>Typing: construct TypeAliasType{name: PyStrRef, type_params, value}
        Typing-->>Caller: return TypeAliasType instance
    else downcast fails
        Typing-->>Caller: raise TypeError("TypeAliasType name must be a string")
    end
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

  • Type alias type #6011 — Modifies TypeAliasType construction/representation in vm/src/stdlib/typing.rs, overlapping constructor/validation changes.
  • typing TypeAlias #5945 — Changes frame.rs intrinsic handling and typing.rs TypeAlias representation; closely related to combined edits here.
  • Fix TypeParams, TypeAlias compile #5862 — Adjusts TypeAlias handling in frame.rs, including name/type_params validation patterns.

Poem

🐰 I hopped through code with nimble paws,
I checked each name for stringly laws.
PyStrRef snug where objects once lay,
Errors caught before they stray.
A tiny twitch, a safer day ✨

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 25.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title Check ✅ Passed The title "Use PyStrRef for TypeAliasType name" directly and accurately captures the primary change in the pull request. The changeset centers on replacing the PyObjectRef type with PyStrRef for the TypeAliasType.name field, updating the constructor and related methods to reflect this change, and adding type validation. The title is concise, specific, and clearly communicates the main objective without vague language or unnecessary details. A reader reviewing the commit history would immediately understand that this PR refines type safety by using a more specific string reference type for the TypeAliasType name.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

📜 Recent review details

Configuration used: Path: .coderabbit.yml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 78601f3 and 1e6dbc6.

📒 Files selected for processing (1)
  • vm/src/stdlib/typing.rs (5 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • vm/src/stdlib/typing.rs

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

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Member

@youknowone youknowone left a comment

Choose a reason for hiding this comment

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

Thank you for contributing!

@youknowone youknowone merged commit b15e537 into RustPython:main Oct 21, 2025
12 checks passed
@walker84837 walker84837 deleted the fix/todo-use-pystrref branch October 21, 2025 11:30
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.

2 participants