Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7d062ec

Browse files
Do not put the closing quotes in a docstring on a separate line (psf#3430)
Fixes psf#3320. Followup from psf#3044.
1 parent abd2b25 commit 7d062ec

3 files changed

Lines changed: 10 additions & 11 deletions

File tree

CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
<!-- Changes that affect Black's preview style -->
1616

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

1921
### Configuration
2022

src/black/linegen.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -389,19 +389,18 @@ def visit_STRING(self, leaf: Leaf) -> Iterator[Line]:
389389
# We need to find the length of the last line of the docstring
390390
# to find if we can add the closing quotes to the line without
391391
# exceeding the maximum line length.
392-
# If docstring is one line, then we need to add the length
393-
# of the indent, prefix, and starting quotes. Ending quotes are
394-
# handled later.
392+
# If docstring is one line, we don't put the closing quotes on a
393+
# separate line because it looks ugly (#3320).
395394
lines = docstring.splitlines()
396395
last_line_length = len(lines[-1]) if docstring else 0
397396

398-
if len(lines) == 1:
399-
last_line_length += len(indent) + len(prefix) + quote_len
400-
401397
# If adding closing quotes would cause the last line to exceed
402398
# the maximum line length then put a line break before the
403399
# closing quotes
404-
if last_line_length + quote_len > self.mode.line_length:
400+
if (
401+
len(lines) > 1
402+
and last_line_length + quote_len > self.mode.line_length
403+
):
405404
leaf.value = prefix + quote + docstring + "\n" + indent + quote
406405
else:
407406
leaf.value = prefix + quote + docstring + quote

tests/data/preview/docstring_preview.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,11 @@ def single_quote_docstring_over_line_limit2():
5454

5555

5656
def docstring_almost_at_line_limit():
57-
"""long docstring.................................................................
58-
"""
57+
"""long docstring................................................................."""
5958

6059

6160
def docstring_almost_at_line_limit_with_prefix():
62-
f"""long docstring................................................................
63-
"""
61+
f"""long docstring................................................................"""
6462

6563

6664
def mulitline_docstring_almost_at_line_limit():

0 commit comments

Comments
 (0)