Skip to content

Commit

Permalink
[native] Allow unparenthesized tuples inside f-strings (#621)
Browse files Browse the repository at this point in the history
  • Loading branch information
isidentical authored Jan 23, 2022
1 parent 2b2b25b commit 2345848
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 1 deletion.
63 changes: 63 additions & 0 deletions libcst/_nodes/tests/test_atom.py
Original file line number Diff line number Diff line change
Expand Up @@ -740,6 +740,69 @@ class AtomTest(CSTNodeTest):
"parser": parse_expression,
"expected_position": None,
},
# Unpacked tuple
{
"node": cst.FormattedString(
parts=[
cst.FormattedStringExpression(
expression=cst.Tuple(
elements=[
cst.Element(
value=cst.Name(
value="a",
),
comma=cst.Comma(
whitespace_before=cst.SimpleWhitespace(
value="",
),
whitespace_after=cst.SimpleWhitespace(
value=" ",
),
),
),
cst.Element(
value=cst.Name(
value="b",
),
),
],
lpar=[],
rpar=[],
),
),
],
start="f'",
end="'",
),
"code": "f'{a, b}'",
"parser": parse_expression,
"expected_position": None,
},
# Conditional expression
{
"node": cst.FormattedString(
parts=[
cst.FormattedStringExpression(
expression=cst.IfExp(
test=cst.Name(
value="b",
),
body=cst.Name(
value="a",
),
orelse=cst.Name(
value="c",
),
),
),
],
start="f'",
end="'",
),
"code": "f'{a if b else c}'",
"parser": parse_expression,
"expected_position": None,
},
# Concatenated strings
{
"node": cst.ConcatenatedString(
Expand Down
2 changes: 1 addition & 1 deletion native/libcst/src/parser/grammar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1396,7 +1396,7 @@ parser! {

rule _f_expr() -> Expression<'a>
= (g:_bare_genexp() {Expression::GeneratorExp(g)})
/ _conditional_expression()
/ star_expressions()
/ yield_expr()

rule _f_conversion() -> &'a str
Expand Down

0 comments on commit 2345848

Please sign in to comment.