From 46919f80bb3cb7b396901b611ca0c0ea773d3663 Mon Sep 17 00:00:00 2001 From: Henri Holopainen Date: Tue, 31 Oct 2023 21:18:52 +0200 Subject: [PATCH] Don't toggle printables quotes in debug f-strings --- src/black/trans.py | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/black/trans.py b/src/black/trans.py index a3f6467cc9e..71ffa3ed4d6 100644 --- a/src/black/trans.py +++ b/src/black/trans.py @@ -590,11 +590,20 @@ def make_naked(string: str, string_prefix: str) -> str: """ assert_is_leaf_string(string) if "f" in string_prefix: - string = _toggle_fexpr_quotes(string, QUOTE) - # After quotes toggling, quotes in expressions won't be escaped - # because quotes can't be reused in f-strings. So we can simply - # let the escaping logic below run without knowing f-string - # expressions. + RE_QUOTES_IN_DEBUG_F_STRING = ( + r"{[^[]*\[\s*[\"\']\w*[\"\']\s*\][^=}]*=[^}]*}" + ) + is_debug_f_string_with_quotes = re.search( + RE_QUOTES_IN_DEBUG_F_STRING, string + ) + if not is_debug_f_string_with_quotes: + # We don't want to toggle debug f-string quotes in the printable + # part, because that would modify the AST + string = _toggle_fexpr_quotes(string, QUOTE) + # After quotes toggling, quotes in expressions won't be escaped + # because quotes can't be reused in f-strings. So we can simply + # let the escaping logic below run without knowing f-string + # expressions. RE_EVEN_BACKSLASHES = r"(?:(?