From b66f0070f237577b053077d0032f3d6dbd10b61a Mon Sep 17 00:00:00 2001 From: Marlon Costa Date: Mon, 23 Nov 2026 22:16:07 +0100 Subject: [PATCH] patchedast: match MatchSequence delimiters by tokenising _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 #896. --- CHANGELOG.md | 1 + rope/refactor/patchedast.py | 44 ++++++++++++++++++----------- ropetest/refactor/patchedasttest.py | 29 +++++++++++++++++++ 3 files changed, 57 insertions(+), 17 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 88e4748e2..abf4e51c2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ - #922 Classify autoimport project sources by path containment (@marlon-costa-dc) - #926, #929 Raise RefactoringError instead of IndexError/ModuleSyntaxError when extracting an empty or whitespace-only region (@aliassar) - #933 Add type annotations to project.py (@lieryan) +- #882 Match MatchSequence delimiters by tokenizing the node segment (@marlon-costa-dc) - #119, #928 Fix IndexError in rename and find occurrences when a `from` import is the last thing in a module without a trailing newline (@mcepl) - #921 Patch the else suite of try statements that also have a finally (@marlon-costa-dc) - #937 Preserve non-ASCII argument expressions and default values when changing function signatures (@Jinyong-Li) diff --git a/rope/refactor/patchedast.py b/rope/refactor/patchedast.py index fbebceab2..dff025bbf 100644 --- a/rope/refactor/patchedast.py +++ b/rope/refactor/patchedast.py @@ -1,6 +1,8 @@ import collections +import io import numbers import re +import tokenize import warnings from itertools import chain @@ -751,24 +753,32 @@ def _MatchSequence(self, node): self._handle(node, children) def _get_surrounding_parens(self, node: ast.MatchSequence): + """The sequence's own ``[...]``/``(...)`` delimiters, if it has them. + + The text between the node's start and its first pattern can also + hold grouping parentheses of that pattern -- ``case (("a" | b), c)`` + -- which the AST does not represent. The sequence is delimited only + when the bracket at its first character is the one closed at its + last character; ``case (1), (2)`` starts and ends with parentheses + that belong to its elements. + """ node_start, node_end = self.ast_adapter[node] - first_pattern_start, _ = self.ast_adapter[node.patterns[0]] - _, last_pattern_end = self.ast_adapter[node.patterns[-1]] - opening_paren = self.source[node_start:first_pattern_start].strip() - closing_paren = self.source[last_pattern_end:node_end].strip() - - if opening_paren not in ["[", "(", ""]: - warnings.warn( - f"Unexpected character in MatchSequence's opening_paren <{opening_paren}>; please report!", - RuntimeWarning, - ) - - if closing_paren not in ["]", ")", ""]: - warnings.warn( - f"Unexpected character in MatchSequence's closing_paren <{closing_paren}>; please report!", - RuntimeWarning, - ) - return opening_paren, closing_paren + segment = self.source[node_start:node_end] + pairs = {"[": "]", "(": ")"} + if segment[:1] not in pairs or segment[-1:] != pairs[segment[0]]: + return "", "" + depth = 0 + for token in tokenize.generate_tokens(io.StringIO(segment).readline): + if token.type != tokenize.OP or token.string not in "()[]{}": + continue + depth += 1 if token.string in "([{" else -1 + if depth == 0: + end_row = segment.count("\n") + 1 + end_col = len(segment) - (segment.rfind("\n") + 1) + if token.end != (end_row, end_col): + return "", "" + return segment[0], segment[-1] + return "", "" def _MatchStar(self, node): self._handle(node, ["*", node.name or "_"]) diff --git a/ropetest/refactor/patchedasttest.py b/ropetest/refactor/patchedasttest.py index f3320f450..8add1aa7f 100644 --- a/ropetest/refactor/patchedasttest.py +++ b/ropetest/refactor/patchedasttest.py @@ -1597,6 +1597,35 @@ def test_match_node_with_match_sequence_with_no_parens(self): "MatchValue", "", ",", " ", "MatchValue", ]) + @testutils.only_for_versions_higher("3.10") + def test_match_node_with_match_sequence_with_grouped_first_pattern(self): + source = dedent("""\ + match x: + case (("a" | None), None): + pass + """) + ast_frag = patchedast.get_patched_ast(source, True) + checker = _ResultChecker(self, ast_frag) + checker.check_children("MatchSequence", [ + "(", "(", "MatchOr", ")", ",", " ", "MatchSingleton", "", ")", + ]) + start = source.index("((") + checker.check_region("MatchSequence", start, source.index(":\n", start)) + self.assertEqual(source, patchedast.write_ast(ast_frag)) + + @testutils.only_for_versions_higher("3.10") + def test_match_node_with_match_sequence_of_parenthesized_patterns(self): + source = dedent("""\ + match x: + case (1), (2): + pass + """) + ast_frag = patchedast.get_patched_ast(source, True) + checker = _ResultChecker(self, ast_frag) + start = source.index("(1)") + checker.check_region("MatchSequence", start, start + len("(1), (2)")) + self.assertEqual(source, patchedast.write_ast(ast_frag)) + @testutils.only_for_versions_higher("3.10") def test_match_node_with_match_sequence_with_square_parens(self): source = dedent("""\