From 8a092dbe17463dd75b2017007bdf60aaabdbf1a5 Mon Sep 17 00:00:00 2001 From: Lie Ryan Date: Mon, 21 Nov 2022 22:24:29 +1100 Subject: [PATCH 1/2] Fix #522. Implement patchedast parsing of MatchMapping Somehow, this was missed back when the rest of `match` statement was implemented. --- rope/refactor/patchedast.py | 10 ++++++++++ ropetest/refactor/patchedasttest.py | 30 +++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/rope/refactor/patchedast.py b/rope/refactor/patchedast.py index 614ea51b1..f7882e232 100644 --- a/rope/refactor/patchedast.py +++ b/rope/refactor/patchedast.py @@ -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): diff --git a/ropetest/refactor/patchedasttest.py b/ropetest/refactor/patchedasttest.py index 0db04e463..bcfe482c2 100644 --- a/ropetest/refactor/patchedasttest.py +++ b/ropetest/refactor/patchedasttest.py @@ -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 From 6a61231191509976088f0c3ef0ac805c17b7d829 Mon Sep 17 00:00:00 2001 From: Lie Ryan Date: Mon, 21 Nov 2022 22:25:52 +1100 Subject: [PATCH 2/2] Update CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4741a06c9..6e7f0b0c5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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