Skip to content

Commit

Permalink
Allow generator expressions in f-strings (#419)
Browse files Browse the repository at this point in the history
Fixes #388
  • Loading branch information
thatch authored Nov 17, 2020
1 parent 31bae01 commit 90df5a6
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
29 changes: 29 additions & 0 deletions libcst/_nodes/tests/test_atom.py
Original file line number Diff line number Diff line change
Expand Up @@ -668,6 +668,35 @@ class AtomTest(CSTNodeTest):
"parser": parse_expression,
"expected_position": CodeRange((1, 1), (1, 4)),
},
# Generator expression (doesn't make sense, but legal syntax)
{
"node": cst.FormattedString(
start='f"',
parts=[
cst.FormattedStringExpression(
expression=cst.GeneratorExp(
elt=cst.Name(
value="x",
),
for_in=cst.CompFor(
target=cst.Name(
value="x",
),
iter=cst.Name(
value="y",
),
),
lpar=[],
rpar=[],
),
),
],
end='"',
),
"code": 'f"{x for x in y}"',
"parser": parse_expression,
"expected_position": None,
},
# Concatenated strings
{
"node": cst.ConcatenatedString(
Expand Down
4 changes: 2 additions & 2 deletions libcst/_parser/conversions/expression.py
Original file line number Diff line number Diff line change
Expand Up @@ -1038,12 +1038,12 @@ def convert_fstring_equality(

@with_production(
"fstring_expr",
"'{' testlist [ fstring_equality ] [ fstring_conversion ] [ fstring_format_spec ] '}'",
"'{' testlist_comp_tuple [ fstring_equality ] [ fstring_conversion ] [ fstring_format_spec ] '}'",
version=">=3.8",
)
@with_production(
"fstring_expr",
"'{' testlist [ fstring_conversion ] [ fstring_format_spec ] '}'",
"'{' testlist_comp_tuple [ fstring_conversion ] [ fstring_format_spec ] '}'",
version="<=3.7",
)
def convert_fstring_expr(
Expand Down

0 comments on commit 90df5a6

Please sign in to comment.