Use PyStrRef for TypeAliasType name by walker84837 · Pull Request #6203 · RustPython/RustPython
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: PyObjectRef → PyStrRef; 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
Estimated code review effort
🎯 3 (Moderate) | ⏱️ ~20 minutes
Possibly related PRs
- Type alias type #6011 — Modifies
TypeAliasTypeconstruction/representation invm/src/stdlib/typing.rs, overlapping constructor/validation changes. - typing TypeAlias #5945 — Changes
frame.rsintrinsic handling andtyping.rsTypeAlias representation; closely related to combined edits here. - Fix TypeParams, TypeAlias compile #5862 — Adjusts
TypeAliashandling inframe.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 | 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
📒 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.
Comment @coderabbitai help to get the list of available commands and usage tips.