Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
44 changes: 27 additions & 17 deletions rope/refactor/patchedast.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import collections
import io
import numbers
import re
import tokenize
import warnings
from itertools import chain

Expand Down Expand Up @@ -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 "_"])
Expand Down
29 changes: 29 additions & 0 deletions ropetest/refactor/patchedasttest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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("""\
Expand Down
Loading