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

Do not put the closing quotes in a docstring on a separate line #3430

Merged
merged 1 commit into from Dec 13, 2022
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
2 changes: 2 additions & 0 deletions CHANGES.md
Expand Up @@ -15,6 +15,8 @@
<!-- Changes that affect Black's preview style -->

- Fix a crash in preview style with assert + parenthesized string (#3415)
- Do not put the closing quotes in a docstring on a separate line, even if the line is
too long (#3430)

### Configuration

Expand Down
13 changes: 6 additions & 7 deletions src/black/linegen.py
Expand Up @@ -389,19 +389,18 @@ def visit_STRING(self, leaf: Leaf) -> Iterator[Line]:
# We need to find the length of the last line of the docstring
# to find if we can add the closing quotes to the line without
# exceeding the maximum line length.
# If docstring is one line, then we need to add the length
# of the indent, prefix, and starting quotes. Ending quotes are
# handled later.
# If docstring is one line, we don't put the closing quotes on a
# separate line because it looks ugly (#3320).
lines = docstring.splitlines()
last_line_length = len(lines[-1]) if docstring else 0

if len(lines) == 1:
last_line_length += len(indent) + len(prefix) + quote_len

# If adding closing quotes would cause the last line to exceed
# the maximum line length then put a line break before the
# closing quotes
if last_line_length + quote_len > self.mode.line_length:
if (
len(lines) > 1
and last_line_length + quote_len > self.mode.line_length
):
ichard26 marked this conversation as resolved.
Show resolved Hide resolved
leaf.value = prefix + quote + docstring + "\n" + indent + quote
else:
leaf.value = prefix + quote + docstring + quote
Expand Down
6 changes: 2 additions & 4 deletions tests/data/preview/docstring_preview.py
Expand Up @@ -54,13 +54,11 @@ def single_quote_docstring_over_line_limit2():


def docstring_almost_at_line_limit():
"""long docstring.................................................................
"""
"""long docstring................................................................."""


def docstring_almost_at_line_limit_with_prefix():
f"""long docstring................................................................
"""
f"""long docstring................................................................"""


def mulitline_docstring_almost_at_line_limit():
Expand Down