From 7f166165fed8da5c3068b620df231d75aec85893 Mon Sep 17 00:00:00 2001 From: hauntsaninja Date: Wed, 15 May 2024 23:33:23 -0400 Subject: [PATCH] Fix formatting of backslash escaped quote inside f-string Fixes #4350 --- src/black/linegen.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/black/linegen.py b/src/black/linegen.py index 9d22a7e7854..dd6b42a0be7 100644 --- a/src/black/linegen.py +++ b/src/black/linegen.py @@ -510,7 +510,18 @@ def visit_fstring(self, node: Node) -> Iterator[Line]: # currently we don't want to format and split f-strings at all. string_leaf = fstring_to_string(node) node.replace(string_leaf) - yield from self.visit_STRING(string_leaf) + if not ( + "\\" in string_leaf.value + and any( + "\\" not in str(child) + for child in node.children + if node.type == syms.fstring_replacement_field + ) + ): + yield from self.visit_STRING(string_leaf) + return + yield from self.visit_default(string_leaf) + return # TODO: Uncomment Implementation to format f-string children # fstring_start = node.children[0]