Skip to content

Commit

Permalink
Fix #522. Implement patchedast parsing of MatchMapping
Browse files Browse the repository at this point in the history
Somehow, this was missed back when the rest of `match` statement was
implemented.
  • Loading branch information
lieryan committed Nov 21, 2022
1 parent d10fbe3 commit 8a092db
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
10 changes: 10 additions & 0 deletions rope/refactor/patchedast.py
Expand Up @@ -903,6 +903,16 @@ def _MatchClass(self, node):
def _MatchValue(self, node):
self._handle(node, [node.value])

def _MatchMapping(self, node):
children = []
children.append("{")
for index, (key, value) in enumerate(zip(node.keys, node.patterns)):
children.extend([key, ":", value])
if index < len(node.keys) - 1:
children.append(",")
children.append("}")
self._handle(node, children)


class _Source:
def __init__(self, source):
Expand Down
30 changes: 30 additions & 0 deletions ropetest/refactor/patchedasttest.py
Expand Up @@ -1565,6 +1565,36 @@ def test_match_node_with_match_class_match_as_capture_pattern_with_explicit_name
])


@testutils.only_for_versions_higher("3.10")
def test_match_node_with_match_mapping_match_as(self):
source = dedent("""\
match x:
case {"a": b} as c:
print(x)
""")
ast_frag = patchedast.get_patched_ast(source, True)
checker = _ResultChecker(self, ast_frag)
self.assert_single_case_match_block(checker, "MatchAs")
checker.check_children("MatchAs", [
"MatchMapping",
" ",
"as",
" ",
"c",
])
checker.check_children("MatchMapping", [
"{",
"",
"Constant",
"",
":",
" ",
"MatchAs",
"",
"}",
])


class _ResultChecker:
def __init__(self, test_case, ast):
self.test_case = test_case
Expand Down

0 comments on commit 8a092db

Please sign in to comment.