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

Fix crash on docstrings ending with "\ " #2142

Merged
merged 3 commits into from Apr 26, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 2 additions & 0 deletions CHANGES.md
Expand Up @@ -4,6 +4,8 @@

#### _Black_

- Fix crash on docstrings ending with "\ ". (#2142)

- Reflect the `--skip-magic-trailing-comma` and `--experimental-string-processing` flags
in the name of the cache file. Without this fix, changes in these flags would not take
effect if the cache had already been populated. (#2131)
Expand Down
8 changes: 7 additions & 1 deletion src/black/__init__.py
Expand Up @@ -2179,7 +2179,13 @@ def visit_STRING(self, leaf: Leaf) -> Iterator[Line]:
indent = " " * 4 * self.current_line.depth
docstring = fix_docstring(docstring, indent)
else:
docstring = docstring.strip()
new_docstring = docstring.strip()
# If the docstring ended with "\ ", stripping away the space
# will be a syntax error. Just put the trailing space back.
if new_docstring.endswith("\\"):
docstring = docstring.lstrip()
else:
docstring = new_docstring

if docstring:
# Add some padding if the docstring starts / ends with a quote mark.
Expand Down
8 changes: 8 additions & 0 deletions tests/data/docstring.py
Expand Up @@ -158,6 +158,10 @@ def docstring_with_inline_tabs_and_tab_indentation():
"""
pass


def backslash_space():
"""\ """

# output

class MyClass:
Expand Down Expand Up @@ -316,3 +320,7 @@ def docstring_with_inline_tabs_and_tab_indentation():
line ends with some tabs
"""
pass


def backslash_space():
"""\ """