Skip to content

Commit

Permalink
fix: Support generators in f-strings
Browse files Browse the repository at this point in the history
  • Loading branch information
sondrelg committed Apr 22, 2023
1 parent 94938e5 commit cbbc4f7
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,8 @@ mod tests {
TestCase { input: "logger.error(f'{ {\"foo\": str(e) for errors in all_errors for e in errors} }')".to_string(), expected_output: "logger.error('%s', {'foo': str(e) for errors in all_errors for e in errors})".to_string() },
// Call containing list comprehension
TestCase { input: "logger.error(f'{\", \".join([str(e) for e in errors for errors in all_errors])}')".to_string(), expected_output: "logger.error('%s', ', '.join([str(e) for e in errors for errors in all_errors],))".to_string() },
// Generator
TestCase { input: "logger.exception(f'{\", \".join(b for b in bs)}')".to_string(), expected_output: "logger.exception('%s', ', '.join([b for b in bs],))".to_string() },
]
}

Expand Down
2 changes: 1 addition & 1 deletion src/parse_fstring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ pub fn parse_formatted_value(value: &Expr, postfix: String, in_call: bool) -> Re
quotes.char()
)
}
ExprKind::ListComp { elt, generators } => {
ExprKind::ListComp { elt, generators } | ExprKind::GeneratorExp { elt, generators } => {
let mut s = format!("[{}", parse_formatted_value(elt, postfix.clone(), true)?,);
for generator in generators {
s.push_str(&format!(
Expand Down

0 comments on commit cbbc4f7

Please sign in to comment.