Skip to content

Commit

Permalink
Produce equivalent code for docstrings containing backslash followed …
Browse files Browse the repository at this point in the history
…by whitespace(s) before newline (#4008)

Fixes #3727
  • Loading branch information
henriholopainen committed Oct 31, 2023
1 parent ddfecf0 commit e501103
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
3 changes: 2 additions & 1 deletion CHANGES.md
Expand Up @@ -8,7 +8,8 @@

### 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

Expand Down
3 changes: 2 additions & 1 deletion src/black/linegen.py
Expand Up @@ -2,6 +2,7 @@
Generating lines of code.
"""

import re
import sys
from dataclasses import replace
from enum import Enum, auto
Expand Down Expand Up @@ -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:
Expand Down
13 changes: 13 additions & 0 deletions tests/data/cases/docstring.py
Expand Up @@ -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:
Expand Down Expand Up @@ -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>
"""


def foo():
"""
Docstring with a backslash followed by a space\
and then another line
"""

0 comments on commit e501103

Please sign in to comment.