Skip to content

Support PEP 695 type parameters on class definitions and type aliases - #845

Merged
lieryan merged 7 commits into
python-rope:masterfrom
marlon-costa-dc:fix/pep695-and-match-patchedast-handlers
Sep 26, 2026
Merged

lieryan merged 7 commits into
python-rope:masterfrom
marlon-costa-dc:fix/pep695-and-match-patchedast-handlers

Conversation

@marlon-costa-dc

@marlon-costa-dc marlon-costa-dc commented Jul 22, 2026 •

Copy link
Copy Markdown
Contributor

Upstream master has since gained handlers for TypeAlias, TypeVar, ParamSpec, TypeVarTuple and the match subpatterns (#852, #853, #623/#819/#863), so this PR has been updated onto current master and reduced to what is still missing:

  • ClassDef and TypeAlias did not consume the PEP 695 type-parameter clause, so class C[T]: ... and type Alias[T] = list[T] produced wrong child regions (AttributeError: 'TypeVar' object has no attribute 'sorted_children'). Generic functions already handled it.
  • The [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 TypeVar regions), and for match sequence/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.

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
Contributor

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 thread ropetest/refactor/patchedasttest.py Outdated
Comment thread ropetest/refactor/patchedasttest.py Outdated
Comment thread ropetest/refactor/patchedasttest.py Outdated
@lieryan lieryan added this to the 1.15.0 milestone Aug 16, 2026
marlon-costa-dc and others added 4 commits September 25, 2026 11:21
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
@marlon-costa-dc marlon-costa-dc changed the title Add patchedast handlers for PEP 695 type params and match subpatterns Support PEP 695 type parameters on class definitions and type aliases Sep 25, 2026

@lieryan lieryan left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.

Comment thread ropetest/refactor/patchedasttest.py Outdated
Comment thread ropetest/refactor/patchedasttest.py Outdated
Comment thread ropetest/refactor/patchedasttest.py Outdated
Comment thread ropetest/refactor/patchedasttest.py Outdated
Comment thread ropetest/refactor/patchedasttest.py Outdated
Comment thread CHANGELOG.md Outdated
@lieryan
lieryan enabled auto-merge September 26, 2026 12:44
@lieryan

lieryan commented Sep 26, 2026

Copy link
Copy Markdown
Member

@allcontributors please add @marlon-costa-dc for code contribution

@allcontributors

Copy link
Copy Markdown
Contributor

@lieryan

I've put up a pull request to add @marlon-costa-dc! 🎉

@lieryan
lieryan merged commit 16050ab into python-rope:master Sep 26, 2026
22 checks passed
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.

3 participants