Skip to content

Commit

Permalink
Don't (ever) put a single-char closing docstring quote on a new line (#…
Browse files Browse the repository at this point in the history
…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 <jelle.zijlstra@gmail.com>
  • Loading branch information
ichard26 and JelleZijlstra committed Jul 14, 2022
1 parent 18c17be commit 4f0532d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CHANGES.md
Expand Up @@ -17,6 +17,9 @@

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

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

### _Blackd_

<!-- Changes to blackd -->
Expand Down
7 changes: 4 additions & 3 deletions src/black/linegen.py
Expand Up @@ -330,13 +330,14 @@ def visit_STRING(self, leaf: Leaf) -> Iterator[Line]:
# We could enforce triple quotes at this point.
quote = quote_char * quote_len

if Preview.long_docstring_quotes_on_newline in self.mode:
# It's invalid to put closing single-character quotes on a new line.
if Preview.long_docstring_quotes_on_newline in self.mode and quote_len == 3:
# 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 quote are
# handled later
# of the indent, prefix, and starting quotes. Ending quotes are
# handled later.
lines = docstring.splitlines()
last_line_length = len(lines[-1]) if docstring else 0

Expand Down
16 changes: 16 additions & 0 deletions tests/data/preview/docstring_preview.py
Expand Up @@ -42,6 +42,14 @@ def multiline_docstring_at_line_limit_with_prefix():
second line----------------------------------------------------------------------"""


def single_quote_docstring_over_line_limit():
"We do not want to put the closing quote on a new line as that is invalid (see GH-3141)."


def single_quote_docstring_over_line_limit2():
'We do not want to put the closing quote on a new line as that is invalid (see GH-3141).'


# output


Expand Down Expand Up @@ -87,3 +95,11 @@ def multiline_docstring_at_line_limit_with_prefix():
f"""first line----------------------------------------------------------------------
second line----------------------------------------------------------------------"""


def single_quote_docstring_over_line_limit():
"We do not want to put the closing quote on a new line as that is invalid (see GH-3141)."


def single_quote_docstring_over_line_limit2():
"We do not want to put the closing quote on a new line as that is invalid (see GH-3141)."

0 comments on commit 4f0532d

Please sign in to comment.