Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #522. Implement patchedast parsing of MatchMapping #525

Merged
merged 3 commits into from Nov 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -2,6 +2,7 @@

- #492 Feat: Global configuration support
- #509 Fix read/write analysis of the left-hand side of an augmented assignment
- #522 Implement patchedast parsing of MatchMapping

# Release 1.4.0

Expand Down
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