Skip to content

Add patchedast handlers for PEP 695 type params and match subpatterns#845

Open
marlon-costa-dc wants to merge 1 commit into
python-rope:masterfrom
marlon-costa-dc:fix/pep695-and-match-patchedast-handlers
Open

Add patchedast handlers for PEP 695 type params and match subpatterns#845
marlon-costa-dc wants to merge 1 commit into
python-rope:masterfrom
marlon-costa-dc:fix/pep695-and-match-patchedast-handlers

Conversation

@marlon-costa-dc

Copy link
Copy Markdown

Description

patchedast._PatchingASTWalker lacks handlers for part of the Python 3.12 / 3.13 parser surface. Modules using this syntax emit noisy Unknown node type <...> warnings during rope analysis, and in combination with surrounding tokens the region walker can fail with MismatchedTokenError, aborting rename / inline for the whole module.

Missing node handlers (confirmed against 1.14 and current master):

  • PEP 695: ast.TypeAlias, ast.TypeVar, ast.ParamSpec, ast.TypeVarTuple, and type-parameter lists on FunctionDef / AsyncFunctionDef / ClassDef (def f[T], class C[T], type X[T] = ...).
  • Structural pattern matching: ast.MatchSequence, ast.MatchStar, ast.MatchOr, ast.MatchSingleton (_MatchValue, _MatchMapping, _MatchClass, _MatchAs already existed).

Fix

Add handlers that render the source token stream so the patched-AST region walker keeps working:

  • A shared _type_params_children helper 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.
  • _TypeAlias renders type Name[...] = value.
  • _TypeVar renders the name and an optional : bound; _ParamSpec / _TypeVarTuple render the ** / * prefix.
  • _MatchSequence inspects the opening token to distinguish [...], (...), and bare group forms; _MatchStar, _MatchOr, _MatchSingleton mirror the source.

Testing

  • New regression tests in ropetest/refactor/patchedasttest.py (test_handling_pep695_*, test_handling_match_*).
  • Full refactor suite green: ropetest/refactor/ = 949 passed, 7 skipped, 1 xfailed.
  • No Unknown node type warnings remain for these constructs; real rename/inline over PEP 695 + match modules verified end-to-end.

Checklist

  • I have added tests that prove my fix is effective
  • I have updated CHANGELOG.md
  • I have made corresponding changes to user documentation (n/a — bug fix)
  • I have made corresponding changes to library documentation (n/a — no public API change)

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.
Copilot AI review requested due to automatic review settings July 22, 2026 14:31

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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/TypeVarTuple and for MatchSequence/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"])
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