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

Don't (ever) put a single-char closing docstring quote on a new line #3166

Merged
merged 2 commits into from Jul 14, 2022
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
3 changes: 3 additions & 0 deletions CHANGES.md
Expand Up @@ -14,6 +14,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. (#3165)
ichard26 marked this conversation as resolved.
Show resolved Hide resolved

### _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)."