From 576f623c53940df1dfd810ca858842bf914f310a Mon Sep 17 00:00:00 2001 From: hauntsaninja Date: Fri, 3 Nov 2023 01:04:10 -0700 Subject: [PATCH 1/3] Fix crash with f-string docstrings Python does not consider f-strings to be docstrings, so we probably shouldn't be formatting them as such Fixes #4018 Despite its name, I think docstring_preview.py might be testing stable style, which this unfortunately does change --- src/black/nodes.py | 2 +- tests/data/cases/docstring_preview.py | 3 ++- tests/data/cases/f_docstring.py | 20 +++++++++++++++++++ ...view_docstring_no_string_normalization.py} | 0 4 files changed, 23 insertions(+), 2 deletions(-) create mode 100644 tests/data/cases/f_docstring.py rename tests/data/cases/{docstring_preview_no_string_normalization.py => preview_docstring_no_string_normalization.py} (100%) diff --git a/src/black/nodes.py b/src/black/nodes.py index 5f6b280c035..a570d496f38 100644 --- a/src/black/nodes.py +++ b/src/black/nodes.py @@ -529,7 +529,7 @@ def is_docstring(leaf: Leaf) -> bool: return False prefix = get_string_prefix(leaf.value) - if "b" in prefix or "B" in prefix: + if "b" in prefix or "B" in prefix or "f" in prefix or "F" in prefix: return False if prev_siblings_are( diff --git a/tests/data/cases/docstring_preview.py b/tests/data/cases/docstring_preview.py index ff4819acb67..a3c656be2f8 100644 --- a/tests/data/cases/docstring_preview.py +++ b/tests/data/cases/docstring_preview.py @@ -58,7 +58,8 @@ def docstring_almost_at_line_limit(): def docstring_almost_at_line_limit_with_prefix(): - f"""long docstring................................................................""" + f"""long docstring................................................................ + """ def mulitline_docstring_almost_at_line_limit(): diff --git a/tests/data/cases/f_docstring.py b/tests/data/cases/f_docstring.py new file mode 100644 index 00000000000..667f550b353 --- /dev/null +++ b/tests/data/cases/f_docstring.py @@ -0,0 +1,20 @@ +def foo(e): + f""" {'.'.join(e)}""" + +def bar(e): + f"{'.'.join(e)}" + +def baz(e): + F""" {'.'.join(e)}""" + +# output +def foo(e): + f""" {'.'.join(e)}""" + + +def bar(e): + f"{'.'.join(e)}" + + +def baz(e): + f""" {'.'.join(e)}""" diff --git a/tests/data/cases/docstring_preview_no_string_normalization.py b/tests/data/cases/preview_docstring_no_string_normalization.py similarity index 100% rename from tests/data/cases/docstring_preview_no_string_normalization.py rename to tests/data/cases/preview_docstring_no_string_normalization.py From 513f4ead695a45a70bfa954e295ba5445a896681 Mon Sep 17 00:00:00 2001 From: Shantanu <12621235+hauntsaninja@users.noreply.github.com> Date: Fri, 3 Nov 2023 01:19:53 -0700 Subject: [PATCH 2/3] Update src/black/nodes.py Co-authored-by: Alex Waygood --- src/black/nodes.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/black/nodes.py b/src/black/nodes.py index a570d496f38..fff8e05a118 100644 --- a/src/black/nodes.py +++ b/src/black/nodes.py @@ -529,7 +529,7 @@ def is_docstring(leaf: Leaf) -> bool: return False prefix = get_string_prefix(leaf.value) - if "b" in prefix or "B" in prefix or "f" in prefix or "F" in prefix: + if set(prefix).intersection("bBfF"): return False if prev_siblings_are( From c548676aeed70d279e39f69d209b979e4b15b882 Mon Sep 17 00:00:00 2001 From: hauntsaninja Date: Fri, 3 Nov 2023 17:48:09 -0700 Subject: [PATCH 3/3] changelog --- CHANGES.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGES.md b/CHANGES.md index 5ce37943693..4f90f493ad8 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -13,6 +13,9 @@ - Fix crash on formatting code like `await (a ** b)` (#3994) +- No longer treat leading f-strings as docstrings. This matches Python's behaviour and + fixes a crash (#4019) + ### Preview style - Multiline dictionaries and lists that are the sole argument to a function are now