Produce equivalent code for docstrings containing backslash followed by whitespace(s) before newline (#4008)

Fixes #3727
This commit is contained in:
Henri Holopainen 2023-10-31 17:27:11 +02:00 committed by GitHub
parent ddfecf06c1
commit e50110353a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 17 additions and 2 deletions

View File

@ -8,7 +8,8 @@
### Stable style ### Stable style
<!-- Changes that affect Black's stable style --> - Fix a crash when whitespace(s) followed a backslash before newline in a docstring
(#4008)
### Preview style ### Preview style

View File

@ -2,6 +2,7 @@
Generating lines of code. Generating lines of code.
""" """
import re
import sys import sys
from dataclasses import replace from dataclasses import replace
from enum import Enum, auto 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: if Preview.hex_codes_in_unicode_sequences in self.mode:
normalize_unicode_escape_sequences(leaf) 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 # We're ignoring docstrings with backslash newline escapes because changing
# indentation of those changes the AST representation of the code. # indentation of those changes the AST representation of the code.
if self.mode.string_normalization: if self.mode.string_normalization:

View File

@ -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 # output
class MyClass: class MyClass:
@ -442,3 +448,10 @@ def stable_quote_normalization_with_immediate_inner_single_quote(self):
<text here, since without another non-empty line black is stable> <text here, since without another non-empty line black is stable>
""" """
def foo():
"""
Docstring with a backslash followed by a space\
and then another line
"""