From 709bac5595dee0576b6dd25eb30b56cfa6a04284 Mon Sep 17 00:00:00 2001 From: Tushar Sadhwani Date: Thu, 11 Jul 2024 02:22:54 +0530 Subject: [PATCH 1/3] fix: don't try to format internal f-strings --- src/black/linegen.py | 12 ++++++++++++ tests/data/cases/pep_701.py | 6 ++++++ 2 files changed, 18 insertions(+) diff --git a/src/black/linegen.py b/src/black/linegen.py index 9d22a7e7854..19b0b2fb080 100644 --- a/src/black/linegen.py +++ b/src/black/linegen.py @@ -510,6 +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) + if ( + "\\" in string_leaf.value + and any( + "\\" in str(child) + for child in node.children + if child.type == syms.fstring_replacement_field + ) + ): + # string normalization doesn't account for nested quotes, + # causing breakages. skip normalization when nested quotes exist + yield from self.visit_default(string_leaf) + return yield from self.visit_STRING(string_leaf) # TODO: Uncomment Implementation to format f-string children diff --git a/tests/data/cases/pep_701.py b/tests/data/cases/pep_701.py index d72d91c6799..717927d206d 100644 --- a/tests/data/cases/pep_701.py +++ b/tests/data/cases/pep_701.py @@ -128,6 +128,9 @@ f"""{''' '''}""" +f"{'\''}" +f"{f'\''}" + # output x = f"foo" @@ -258,3 +261,6 @@ f"""{''' '''}""" + +f"{'\''}" +f"{f'\''}" From 6c2cd6bfcf7bab9399aa9aca3ff10bd6b00245d1 Mon Sep 17 00:00:00 2001 From: Tushar Sadhwani Date: Thu, 11 Jul 2024 02:36:53 +0530 Subject: [PATCH 2/3] format --- src/black/linegen.py | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/src/black/linegen.py b/src/black/linegen.py index 19b0b2fb080..46945ca2a14 100644 --- a/src/black/linegen.py +++ b/src/black/linegen.py @@ -510,13 +510,10 @@ 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) - if ( - "\\" in string_leaf.value - and any( - "\\" in str(child) - for child in node.children - if child.type == syms.fstring_replacement_field - ) + if "\\" in string_leaf.value and any( + "\\" in str(child) + for child in node.children + if child.type == syms.fstring_replacement_field ): # string normalization doesn't account for nested quotes, # causing breakages. skip normalization when nested quotes exist From b52892e2d7868b6ab5c74efebb553f0a568b346f Mon Sep 17 00:00:00 2001 From: Tushar Sadhwani Date: Thu, 11 Jul 2024 02:38:49 +0530 Subject: [PATCH 3/3] Add changelog entry --- CHANGES.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGES.md b/CHANGES.md index a63503d539a..e3e37484a59 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -34,6 +34,9 @@ - Fix regression where Black failed to parse a multiline f-string containing another multiline string (#4339) +- Fix regression where Black failed to parse an escaped single quote inside an f-string + (#4401) + - Fix bug with Black incorrectly parsing empty lines with a backslash (#4343) ### Performance