From 1a7d9c2f58de1ffcbbe6d133f60f283601ba3f54 Mon Sep 17 00:00:00 2001 From: Henri Holopainen Date: Wed, 8 Nov 2023 06:19:32 +0200 Subject: [PATCH] Preserve visible quote types for f-string debug expressions (#4005) Co-authored-by: Jelle Zijlstra --- CHANGES.md | 2 + src/black/trans.py | 21 +++++-- tests/data/cases/preview_long_strings.py | 80 ++++++++++++++++++++++++ 3 files changed, 98 insertions(+), 5 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 17882194aad..26e4db5848b 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -26,6 +26,8 @@ indented less (#3964) - Multiline list and dict unpacking as the sole argument to a function is now also indented less (#3992) +- In f-string debug expressions preserve quote types that are visible in the final + string (#4005) - Fix a bug where long `case` blocks were not split into multiple lines. Also enable general trailing comma rules on `case` blocks (#4024) - Keep requiring two empty lines between module-level docstring and first function or diff --git a/src/black/trans.py b/src/black/trans.py index a3f6467cc9e..ab3197fa6df 100644 --- a/src/black/trans.py +++ b/src/black/trans.py @@ -590,11 +590,22 @@ 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. + f_expressions = ( + string[span[0] + 1 : span[1] - 1] # +-1 to get rid of curly braces + for span in iter_fexpr_spans(string) + ) + debug_expressions_contain_visible_quotes = any( + re.search(r".*[\'\"].*(?