Skip to content

Commit 4f0532d

Browse files
Don't (ever) put a single-char closing docstring quote on a new line (#3166)
Doing so is invalid. Note this only fixes the preview style since the logic putting closing docstring quotes on their own line if they violate the line length limit is quite new. Co-authored-by: Jelle Zijlstra <[email protected]>
1 parent 18c17be commit 4f0532d

3 files changed

Lines changed: 23 additions & 3 deletions

File tree

CHANGES.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717

1818
<!-- Changes that affect Black's preview style -->
1919

20+
- Single-character closing docstring quotes are no longer moved to their own line as
21+
this is invalid. This was a bug introduced in version 22.6.0. (#3166)
22+
2023
### _Blackd_
2124

2225
<!-- Changes to blackd -->

src/black/linegen.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -330,13 +330,14 @@ def visit_STRING(self, leaf: Leaf) -> Iterator[Line]:
330330
# We could enforce triple quotes at this point.
331331
quote = quote_char * quote_len
332332

333-
if Preview.long_docstring_quotes_on_newline in self.mode:
333+
# It's invalid to put closing single-character quotes on a new line.
334+
if Preview.long_docstring_quotes_on_newline in self.mode and quote_len == 3:
334335
# We need to find the length of the last line of the docstring
335336
# to find if we can add the closing quotes to the line without
336337
# exceeding the maximum line length.
337338
# If docstring is one line, then we need to add the length
338-
# of the indent, prefix, and starting quotes. Ending quote are
339-
# handled later
339+
# of the indent, prefix, and starting quotes. Ending quotes are
340+
# handled later.
340341
lines = docstring.splitlines()
341342
last_line_length = len(lines[-1]) if docstring else 0
342343

tests/data/preview/docstring_preview.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,14 @@ def multiline_docstring_at_line_limit_with_prefix():
4242
second line----------------------------------------------------------------------"""
4343

4444

45+
def single_quote_docstring_over_line_limit():
46+
"We do not want to put the closing quote on a new line as that is invalid (see GH-3141)."
47+
48+
49+
def single_quote_docstring_over_line_limit2():
50+
'We do not want to put the closing quote on a new line as that is invalid (see GH-3141).'
51+
52+
4553
# output
4654

4755

@@ -87,3 +95,11 @@ def multiline_docstring_at_line_limit_with_prefix():
8795
f"""first line----------------------------------------------------------------------
8896
8997
second line----------------------------------------------------------------------"""
98+
99+
100+
def single_quote_docstring_over_line_limit():
101+
"We do not want to put the closing quote on a new line as that is invalid (see GH-3141)."
102+
103+
104+
def single_quote_docstring_over_line_limit2():
105+
"We do not want to put the closing quote on a new line as that is invalid (see GH-3141)."

0 commit comments

Comments
 (0)