Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 4 commits into from Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
"""