From 37b838ff293dc2147dc4c263f7cbfb5e35311641 Mon Sep 17 00:00:00 2001 From: Henri Holopainen Date: Tue, 31 Oct 2023 14:07:15 +0200 Subject: [PATCH 1/4] Add test case --- tests/data/cases/docstring.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/tests/data/cases/docstring.py b/tests/data/cases/docstring.py index c31d6a68783..e983c5bd438 100644 --- a/tests/data/cases/docstring.py +++ b/tests/data/cases/docstring.py @@ -221,6 +221,12 @@ def stable_quote_normalization_with_immediate_inner_single_quote(self): ''' +def foo(): + """ + Docstring with a backslash followed by a space\ + and then another line + """ + # output class MyClass: @@ -442,3 +448,10 @@ def stable_quote_normalization_with_immediate_inner_single_quote(self): """ + + +def foo(): + """ + Docstring with a backslash followed by a space\ + and then another line + """ From 392d693b7c5f7d8752d77a2bab999dc4244da172 Mon Sep 17 00:00:00 2001 From: Henri Holopainen Date: Tue, 31 Oct 2023 14:17:14 +0200 Subject: [PATCH 2/4] Don't format docstrings that end with backslash followed by whitespace(s) --- src/black/linegen.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/black/linegen.py b/src/black/linegen.py index 43bc08efbbd..121c6e314fe 100644 --- a/src/black/linegen.py +++ b/src/black/linegen.py @@ -2,6 +2,7 @@ Generating lines of code. """ +import re import sys from dataclasses import replace from enum import Enum, auto @@ -420,7 +421,7 @@ def visit_STRING(self, leaf: Leaf) -> Iterator[Line]: if Preview.hex_codes_in_unicode_sequences in self.mode: normalize_unicode_escape_sequences(leaf) - if is_docstring(leaf) and "\\\n" not in leaf.value: + if is_docstring(leaf) and not re.search(r"\\\s*\n", leaf.value): # We're ignoring docstrings with backslash newline escapes because changing # indentation of those changes the AST representation of the code. if self.mode.string_normalization: From 42ed17eb7c4959c6fe2b15618e859aa400545446 Mon Sep 17 00:00:00 2001 From: Henri Holopainen Date: Tue, 31 Oct 2023 14:17:32 +0200 Subject: [PATCH 3/4] Add changelog line --- CHANGES.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGES.md b/CHANGES.md index dd5f52cf706..d5bcee7bc60 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -8,7 +8,8 @@ ### Stable style - +- Fix a crash when whitespace(s) followed a backslash before newline in a docstring + (#XXXX) ### Preview style From 0da28a62b3c1194c41d8764d81bcee41804cbdbc Mon Sep 17 00:00:00 2001 From: Henri Holopainen Date: Tue, 31 Oct 2023 14:23:53 +0200 Subject: [PATCH 4/4] Update PR number --- CHANGES.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGES.md b/CHANGES.md index d5bcee7bc60..e910fbed162 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -9,7 +9,7 @@ ### Stable style - Fix a crash when whitespace(s) followed a backslash before newline in a docstring - (#XXXX) + (#4008) ### Preview style