Skip to content
This repository was archived by the owner on Jan 13, 2026. It is now read-only.

Commit 263d33e

Browse files
committed
Start implementing visit_MatchMapping()
1 parent f898690 commit 263d33e

2 files changed

Lines changed: 52 additions & 4 deletions

File tree

rewrite/rewrite/python/_parser_visitor.py

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -926,7 +926,40 @@ def visit_MatchStar(self, node):
926926
)
927927

928928
def visit_MatchMapping(self, node):
929-
raise NotImplementedError("Implement visit_MatchMapping!")
929+
return py.MatchCase(
930+
random_id(),
931+
self.__whitespace(),
932+
Markers.EMPTY,
933+
py.MatchCase.Pattern(
934+
random_id(),
935+
Space.EMPTY,
936+
Markers.EMPTY,
937+
py.MatchCase.Pattern.Kind.MAPPING,
938+
JContainer(
939+
self.__source_before('{'),
940+
[self.__pad_list_element(py.MatchCase.Pattern(
941+
random_id(),
942+
Space.EMPTY,
943+
Markers.EMPTY,
944+
py.MatchCase.Pattern.Kind.KEY_VALUE,
945+
JContainer(
946+
Space.EMPTY,
947+
[
948+
self.__pad_right(self.__convert(node.keys[i]), self.__source_before(':')),
949+
self.__pad_right(self.__convert(node.patterns[i]), Space.EMPTY),
950+
],
951+
Markers.EMPTY
952+
),
953+
None
954+
), last=i == len(node.patterns) - 1, end_delim='}') for i, e in
955+
enumerate(node.patterns)] if node.patterns else [],
956+
Markers.EMPTY
957+
),
958+
None
959+
),
960+
None,
961+
None
962+
)
930963

931964
def visit_MatchClass(self, node):
932965
prefix = self.__whitespace()
@@ -1010,7 +1043,7 @@ def visit_MatchAs(self, node):
10101043
None,
10111044
None
10121045
)
1013-
else:
1046+
elif node.pattern:
10141047
return py.MatchCase(
10151048
random_id(),
10161049
self.__whitespace(),
@@ -1033,6 +1066,7 @@ def visit_MatchAs(self, node):
10331066
None,
10341067
None
10351068
)
1069+
return self.__convert_name(node.name)
10361070

10371071
def visit_MatchOr(self, node):
10381072
return py.MatchCase(

rewrite/tests/python/py312/match_test.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,14 +178,28 @@ def test(x, value):
178178

179179

180180
def test_mapping():
181+
# language=python
182+
rewrite_run(
183+
python(
184+
"""\
185+
def test(x):
186+
match x:
187+
case {"x": x, "y": y}:
188+
return x+y
189+
"""
190+
)
191+
)
192+
193+
194+
def test_mapping_with_rest():
181195
# language=python
182196
rewrite_run(
183197
python(
184198
"""\
185199
def test(x):
186200
match x:
187201
case {"x": x, "y": y, **z}:
188-
return x+y+z
202+
return x+y+sum(z.values())
189203
"""
190204
)
191205
)
@@ -211,7 +225,7 @@ def test_class(args):
211225
python(
212226
"""\
213227
from abc import ABC
214-
228+
215229
def test(x, y):
216230
match x:
217231
case ABC({0}):

0 commit comments

Comments
 (0)