Support PEP 695 type parameters on class definitions and type aliases - #845
Conversation
rope 1.14 (and master) lack `_PatchingASTWalker` handlers for part of the Python 3.12/3.13 parser surface, so modules using this syntax emit noisy `Unknown node type <...>` warnings and, combined with other tokens, can abort rope analysis (rename / inline / census) with MismatchedTokenError. Add native handlers rendering the source token stream: - PEP 695: `type X[T] = ...` (_TypeAlias); type-parameter lists on `def f[T]` / `class C[T]` via a shared _type_params_children helper; and _TypeVar / _ParamSpec / _TypeVarTuple. - Structural pattern matching: _MatchSequence (list/tuple aware via the opening token), _MatchStar, _MatchOr, _MatchSingleton. Adds regression tests. Full refactor suite stays green.
There was a problem hiding this comment.
Pull request overview
This PR extends rope.refactor.patchedast._PatchingASTWalker to understand additional Python 3.12/3.13 AST nodes (PEP 695 type parameters and additional match subpattern nodes) so the patched-AST region walker can consume the source token stream without emitting Unknown node type <...> warnings or triggering MismatchedTokenError during refactor operations.
Changes:
- Add a shared helper to render PEP 695 type-parameter lists (
[T, *Ts, **P]) for classes and functions. - Add patchedast handlers for
TypeAlias/TypeVar/ParamSpec/TypeVarTupleand forMatchSequence/MatchStar/MatchOr/MatchSingleton. - Add/extend regression tests and document the fix in
CHANGELOG.md.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| rope/refactor/patchedast.py | Adds new node handlers and a type-parameter rendering helper to keep region-walking stable on newer syntax. |
| ropetest/refactor/patchedasttest.py | Adds regression tests for PEP 695 type params and additional match subpattern nodes. |
| CHANGELOG.md | Records the upcoming-release fix for patchedast handling of newer Python syntax. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
# Conflicts: # CHANGELOG.md # rope/refactor/patchedast.py
Addresses review: the generic function test now uses a type parameter name that occurs only in the [TP] clause and asserts both the FunctionDef children and the TypeVar region; the generic class test asserts the ClassDef children and TypeVar region; the match test asserts the MatchSequence children (including brackets) and region.
# Conflicts: # ropetest/refactor/patchedasttest.py
There was a problem hiding this comment.
The change LGTM. I've only got a couple minor stylistic changes to make. I'll merge those and merge this PR.
Thanks for the contribution @marlon-costa-dc.
Co-authored-by: Lie Ryan <lie.1296@gmail.com>
|
@allcontributors please add @marlon-costa-dc for code contribution |
|
I've put up a pull request to add @marlon-costa-dc! 🎉 |
Upstream
masterhas since gained handlers forTypeAlias,TypeVar,ParamSpec,TypeVarTupleand thematchsubpatterns (#852, #853, #623/#819/#863), so this PR has been updated onto currentmasterand reduced to what is still missing:ClassDefandTypeAliasdid not consume the PEP 695 type-parameter clause, soclass C[T]: ...andtype Alias[T] = list[T]produced wrong child regions (AttributeError: 'TypeVar' object has no attribute 'sorted_children'). Generic functions already handled it.[T, ...]clause is now appended by one helper,_add_type_params, shared by the function, class and type-alias handlers (the function handler's inline code moved into it).Tests: regression tests for generic classes, functions and type aliases (children and
TypeVarregions), and formatchsequence/star/or/singleton patterns (children and regions), per the review.ropetest/refactor/patchedasttest.py: 168 passed. The four new PEP 695 and match tests were confirmed to fail on the previous handlers.