Skip to content

patchedast: locate multi-line f-strings, replacement fields and match-sequence delimiters by parser coordinates - #882

Open
marlon-costa-dc wants to merge 1 commit into
python-rope:masterfrom
marlon-costa-dc:pr/parser-coordinates
Open

marlon-costa-dc wants to merge 1 commit into
python-rope:masterfrom
marlon-costa-dc:pr/parser-coordinates

Conversation

@marlon-costa-dc

Copy link
Copy Markdown
Contributor

Description

Three patchedast defects found by walking a ~6,300-file Python 3.13 codebase with get_patched_ast(source, True) and requiring write_ast() to reproduce each file exactly:

  1. Multi-line replacement fields (PEP 701). x = (f"{a}{\n b\n}{c}").upper() raised MismatchedTokenError: _JoinedStr located the literal with the textual string pattern, whose short-string form excludes newlines. The literal's region now comes from the node's own coordinates (ast_adapter), implicit concatenation included.
  2. Replacement field brace in a plain string of the same concatenation. In "({})," f"{a}" the field's { was searched for as text and matched the { inside the plain part, so the FormattedValue region started at ( and write_ast duplicated source. Since Python 3.12 the parser locates each FormattedValue at its own {; the walker starts there when the coordinate points at one (older parsers give the enclosing literal's position, which is left untouched). This also fixes the case covered by test_extract_method_f_string_false_format_value_in_regular_string, whose @expectedFailure is removed.
  3. Grouped first pattern in a match sequence (related to patchedast is currently not handling some match-case statement syntaxes #623). case (("a" | None), None): warned Unexpected character in MatchSequence's opening_paren <((> and duplicated the source on write, because every character before the first pattern was taken as the delimiter. The sequence's delimiters are now the bracket pair that encloses the whole node, found by tokenizing its segment; case (1), (2):, whose outer parentheses belong to its elements, is also handled.

Checklist

  • I have added tests that prove my fix is effective: three new tests in ropetest/refactor/patchedasttest.py fail on current master and pass here; the previously expected-failure extract test passes.
  • I have updated CHANGELOG.md

🤖 Generated with Claude Code

marlon-costa-dc added a commit to marlon-costa-dc/rope that referenced this pull request Sep 25, 2026
Adds the parser-coordinate patchedast fixes (upstream PR python-rope#882): multi-line
f-string replacement fields, replacement-field braces in plain parts of a
concatenation, and grouped first patterns in match sequences.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@lieryan lieryan added this to the 1.16.0 milestone Sep 26, 2026
@lieryan lieryan added the patchedast-string Issues related to patchedast when parsing string syntax. label Sep 30, 2026
@marlon-costa-dc

Copy link
Copy Markdown
Contributor Author

Reworked the residual of this PR on top of current master (post-#896): the f-string bounding via parser coordinates landed through #896's ast_adapter regions, so what remains valuable here is the deterministic delimiter matching for MatchSequence — _get_surrounding_parens tokenizes the node segment and treats the sequence as delimited only when the bracket opening its first character closes at its last character (case (1), (2), grouped first patterns), removing the please report! RuntimeWarning heuristic.

Available for comparison at dc/pr-882 with the three tests from this PR adapted to the current master (patchedasttest 172 passed on 3.13).

@mcepl

mcepl commented Oct 26, 2026

Copy link
Copy Markdown
Contributor

The patched-AST cleanup in #896 overlaps with the f-string parts of this PR, but the MatchSequence delimiter work still appears useful.

Could you update this PR against current master and narrow it to the remaining MatchSequence behavior? Please retain regression tests for grouped first patterns, such as case (1), (2), and verify both source regions and exact source round trips.

The dc/pr-882 branch linked in the discussion may be a useful starting point. Please also check whether any f-string regression still fails on master before dropping it. If one does, we should track it separately rather than assume the cleanup fixed it.

Once the remaining change is isolated, a fresh CI matrix should give us much more useful results.

@mcepl
mcepl requested review from lieryan and mcepl and removed request for lieryan October 26, 2026 21:11
@marlon-costa-dc

Copy link
Copy Markdown
Contributor Author

Updated against current master and narrowed to the MatchSequence delimiter behaviour, as requested:

  • The f-string coordinate parts are dropped, since patchedast cleanup and refactoring #896 covers them. I checked the remaining f-string case from the previous version (a multiline replacement field, x = (f"{a}{\n b\n}{c}").upper()): it passes on master with the exact source round trip, so there is nothing to track separately.
  • _get_surrounding_parens now tokenizes the node segment. The sequence has its own delimiters exactly when the bracket opening its first character closes at its last character. This removes the "please report" RuntimeWarning heuristic.
  • Regression tests cover grouped first patterns (case (("a" | None), None):) and separately parenthesized elements (case (1), (2):), checking both the MatchSequence source region and the exact round trip through write_ast. On master, case (1), (2): raises MismatchedTokenError: Token <)> at (2, 17) cannot be matched.

Tested locally on Python 3.10, 3.11, 3.13 and 3.14.

@codecov

codecov Bot commented Oct 31, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 97.14286% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 95.48%. Comparing base (ff8a306) to head (b66f007).

Files with missing lines Patch % Lines
rope/refactor/patchedast.py 94.44% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #882      +/-   ##
==========================================
+ Coverage   95.44%   95.48%   +0.03%     
==========================================
  Files         134      134              
  Lines       26910    26936      +26     
==========================================
+ Hits        25685    25719      +34     
+ Misses       1225     1217       -8     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@mcepl mcepl 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.

The narrowed MatchSequence change looks good to me. The regression tests cover the grouped-pattern and separately parenthesized-element cases, and the checks pass. Please update the PR description to remove the f-string fixes, which are no longer part of this diff and landed via #896.

_get_surrounding_parens() treated all text between the start of
a MatchSequence and its first pattern as the opening delimiter
(and likewise for the closing one).  That text may also contain
grouping parentheses of the first pattern, which the AST does
not represent, so `case (("a" | None), None):` produced a bogus
"((" delimiter, a RuntimeWarning, and write_ast() duplicated
source.  Conversely, in `case (1), (2):` the leading and
trailing parentheses belong to the elements, not the sequence.

The delimiters are now the bracket pair at the first and last
character of the node's segment, accepted only when tokenising
the segment shows that the opening bracket is the one closed at
its very end.  Two regression tests cover both cases.

The originally proposed f-string fixes were dropped from this
change, as they already pass on master after python-rope#896.
@mcepl
mcepl force-pushed the pr/parser-coordinates branch from 07d5453 to b66f007 Compare November 23, 2026 21:18

@mcepl mcepl 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.

Thank you, this looks like the right fix. Accepting the delimiters only when tokenising shows that the bracket at the first character is the one closed at the last character is exactly the correct criterion, and the tokeniser makes it robust against brackets in strings, comments and multi-line patterns.

Housekeeping first: I have rebased the branch onto current master and squashed it into a single commit containing just the MatchSequence change (the f-string parts had already been dropped in your last commit, as they pass on master after #896). The resulting tree is identical to what you had before; the full test suite passes.

The fix actually covers more than the description claims. I ran a few extra cases through write_ast(get_patched_ast(src, True)) == src; all of them pass with this PR, while on master:

Case master
case ((1), (2)): RuntimeWarning ((()
case (\n 1,\n 2,\n): (multi-line, trailing comma) RuntimeWarning (,\n ))
case 1, (2): (grouped last element) MismatchedTokenError

Requests before merging:

  1. Please add regression tests for at least the grouped-last-element case (a real crash) and the multi-line trailing-comma case (very common in real code); one with a bracket inside a comment, e.g. case [1, # ]\n 2]:, would also be nice to pin down why tokenising is used.
  2. Please update the PR title and description — they still describe the three original fixes and the removal of @expectedFailure, neither of which is part of the PR any more. Something like "patchedast: match MatchSequence delimiters by tokenising the node segment".

Nits (optional):

  • token.string not in "()[]{}" is a substring test; it works because no operator token is a substring of it, but a set ({"(", ")", "[", "]", "{", "}"}) would state the intent.
  • The CHANGELOG entry is inserted in the middle of the unreleased list rather than appended, and could reference #623.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

patchedast-string Issues related to patchedast when parsing string syntax.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants