Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions libcst/_nodes/statement.py
Original file line number Diff line number Diff line change
Expand Up @@ -3245,8 +3245,6 @@ class MatchMapping(MatchPattern):
rpar: Sequence[RightParen] = ()

def _validate(self) -> None:
if isinstance(self.trailing_comma, Comma) and self.rest is not None:
raise CSTValidationError("Cannot have a trailing comma without **rest")
super(MatchMapping, self)._validate()

def _visit_and_replace_children(self, visitor: CSTVisitorT) -> "MatchMapping":
Expand Down
17 changes: 17 additions & 0 deletions libcst/_nodes/tests/test_match.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,22 @@ class MatchTest(CSTNodeTest):
),
body=cst.SimpleStatementSuite((cst.Pass(),)),
),
cst.MatchCase( # rest with trailing comma - valid syntax (issue #1436)
pattern=cst.MatchMapping(
[
cst.MatchMappingElement(
key=cst.SimpleString('"a"'),
pattern=cst.MatchValue(cst.Integer("1")),
comma=cst.Comma(
whitespace_after=cst.SimpleWhitespace(" ")
),
),
],
rest=cst.Name("keys"),
trailing_comma=cst.Comma(),
),
body=cst.SimpleStatementSuite((cst.Pass(),)),
),
],
),
"code": (
Expand All @@ -234,6 +250,7 @@ class MatchTest(CSTNodeTest):
+ ' case {"a": None,"b": None}: pass\n'
+ ' case {"a": None,}: pass\n'
+ " case {**rest}: pass\n"
+ ' case {"a": 1, **keys,}: pass\n'
),
"parser": parser,
},
Expand Down
2 changes: 2 additions & 0 deletions native/libcst/tests/fixtures/malicious_match.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,6 @@
case 1, 2: pass
case ( Foo ( ) ) : pass
case (lol) if ( True , ) :pass
case {"a": 1, **keys,} : pass
case {**rest,} : pass

Loading