Add patchedast handlers for PEP 695 type params and match subpatterns#845
Open
marlon-costa-dc wants to merge 1 commit into
Open
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.
Comment on lines
+288
to
+293
| source = "def f[T](x: T) -> T:\n return x\n" | ||
| ast_frag = patchedast.get_patched_ast(source, True) | ||
| # The type parameter list must be rendered between name and '('. | ||
| assert "[T]" in source | ||
| checker = _ResultChecker(self, ast_frag) | ||
| checker.check_children("TypeVar", ["T"]) |
Comment on lines
+304
to
+307
| source = "match x:\n case [1, *rest]:\n pass\n" | ||
| ast_frag = patchedast.get_patched_ast(source, True) | ||
| checker = _ResultChecker(self, ast_frag) | ||
| checker.check_children("MatchStar", ["*", "", "rest"]) |
Comment on lines
+297
to
+300
| source = "class C[T]:\n pass\n" | ||
| ast_frag = patchedast.get_patched_ast(source, True) | ||
| checker = _ResultChecker(self, ast_frag) | ||
| checker.check_children("TypeVar", ["T"]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
patchedast._PatchingASTWalkerlacks handlers for part of the Python 3.12 / 3.13 parser surface. Modules using this syntax emit noisyUnknown node type <...>warnings during rope analysis, and in combination with surrounding tokens the region walker can fail withMismatchedTokenError, aborting rename / inline for the whole module.Missing node handlers (confirmed against 1.14 and current
master):ast.TypeAlias,ast.TypeVar,ast.ParamSpec,ast.TypeVarTuple, and type-parameter lists onFunctionDef/AsyncFunctionDef/ClassDef(def f[T],class C[T],type X[T] = ...).ast.MatchSequence,ast.MatchStar,ast.MatchOr,ast.MatchSingleton(_MatchValue,_MatchMapping,_MatchClass,_MatchAsalready existed).Fix
Add handlers that render the source token stream so the patched-AST region walker keeps working:
_type_params_childrenhelper renders[T, *Ts, **P]between the name and the opening parenthesis; it returns[]when a node has no type params, so pre-3.12 code is unaffected._TypeAliasrenderstype Name[...] = value._TypeVarrenders the name and an optional: bound;_ParamSpec/_TypeVarTuplerender the**/*prefix._MatchSequenceinspects the opening token to distinguish[...],(...), and bare group forms;_MatchStar,_MatchOr,_MatchSingletonmirror the source.Testing
ropetest/refactor/patchedasttest.py(test_handling_pep695_*,test_handling_match_*).ropetest/refactor/= 949 passed, 7 skipped, 1 xfailed.Unknown node typewarnings remain for these constructs; real rename/inline over PEP 695 + match modules verified end-to-end.Checklist