Skip to content

Commit

Permalink
Start implementing visit_MatchMapping()
Browse files Browse the repository at this point in the history
  • Loading branch information
knutwannheden committed Nov 14, 2024
1 parent f898690 commit 263d33e
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 4 deletions.
38 changes: 36 additions & 2 deletions rewrite/rewrite/python/_parser_visitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -926,7 +926,40 @@ def visit_MatchStar(self, node):
)

def visit_MatchMapping(self, node):
raise NotImplementedError("Implement visit_MatchMapping!")
return py.MatchCase(
random_id(),
self.__whitespace(),
Markers.EMPTY,
py.MatchCase.Pattern(
random_id(),
Space.EMPTY,
Markers.EMPTY,
py.MatchCase.Pattern.Kind.MAPPING,
JContainer(
self.__source_before('{'),
[self.__pad_list_element(py.MatchCase.Pattern(
random_id(),
Space.EMPTY,
Markers.EMPTY,
py.MatchCase.Pattern.Kind.KEY_VALUE,
JContainer(
Space.EMPTY,
[
self.__pad_right(self.__convert(node.keys[i]), self.__source_before(':')),
self.__pad_right(self.__convert(node.patterns[i]), Space.EMPTY),
],
Markers.EMPTY
),
None
), last=i == len(node.patterns) - 1, end_delim='}') for i, e in
enumerate(node.patterns)] if node.patterns else [],
Markers.EMPTY
),
None
),
None,
None
)

def visit_MatchClass(self, node):
prefix = self.__whitespace()
Expand Down Expand Up @@ -1010,7 +1043,7 @@ def visit_MatchAs(self, node):
None,
None
)
else:
elif node.pattern:
return py.MatchCase(
random_id(),
self.__whitespace(),
Expand All @@ -1033,6 +1066,7 @@ def visit_MatchAs(self, node):
None,
None
)
return self.__convert_name(node.name)

def visit_MatchOr(self, node):
return py.MatchCase(
Expand Down
18 changes: 16 additions & 2 deletions rewrite/tests/python/py312/match_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,14 +178,28 @@ def test(x, value):


def test_mapping():
# language=python
rewrite_run(
python(
"""\
def test(x):
match x:
case {"x": x, "y": y}:
return x+y
"""
)
)


def test_mapping_with_rest():
# language=python
rewrite_run(
python(
"""\
def test(x):
match x:
case {"x": x, "y": y, **z}:
return x+y+z
return x+y+sum(z.values())
"""
)
)
Expand All @@ -211,7 +225,7 @@ def test_class(args):
python(
"""\
from abc import ABC
def test(x, y):
match x:
case ABC({0}):
Expand Down

0 comments on commit 263d33e

Please sign in to comment.