Skip to content

Commit

Permalink
Don't redundantly nest StarredElement inside another Element (#624)
Browse files Browse the repository at this point in the history
  • Loading branch information
isidentical authored Jan 23, 2022
1 parent d35b6a5 commit 2b2b25b
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
41 changes: 41 additions & 0 deletions libcst/_nodes/tests/test_tuple.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,47 @@ class TupleTest(CSTNodeTest):
"parser": parse_expression,
"expected_position": CodeRange((1, 1), (1, 11)),
},
# top-level two-element tuple, with one being starred
{
"node": cst.SimpleStatementLine(
body=[
cst.Expr(
value=cst.Tuple(
[
cst.Element(cst.Name("one"), comma=cst.Comma()),
cst.StarredElement(cst.Name("two")),
],
lpar=[],
rpar=[],
)
)
]
),
"code": "one,*two\n",
"parser": parse_statement,
},
# top-level three-element tuple, start/end is starred
{
"node": cst.SimpleStatementLine(
body=[
cst.Expr(
value=cst.Tuple(
[
cst.StarredElement(
cst.Name("one"), comma=cst.Comma()
),
cst.Element(cst.Name("two"), comma=cst.Comma()),
cst.StarredElement(cst.Name("three")),
],
lpar=[],
rpar=[],
)
)
]
),
"code": "*one,two,*three\n",
"parser": parse_statement,
},
# missing spaces around tuple, okay with parenthesis
{
"node": cst.For(
Expand Down
9 changes: 6 additions & 3 deletions native/libcst/src/parser/grammar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2174,9 +2174,12 @@ fn make_assignment<'a>(
}

fn expr_to_element(expr: Expression) -> Element {
Element::Simple {
value: expr,
comma: Default::default(),
match expr {
Expression::StarredElement(inner_expr) => Element::Starred(inner_expr),
_ => Element::Simple {
value: expr,
comma: Default::default(),
},
}
}

Expand Down

0 comments on commit 2b2b25b

Please sign in to comment.