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

fix: additional newline added to docstring when the previous line length is less than the line length limit minus 1 #4185

Merged
merged 10 commits into from
Feb 5, 2024
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
<!-- Changes that affect Black's preview style -->

- Consistently add trailing comma on typed parameters (#4164)
- Checking for newline before adding one on docstring that is almost at the line limit
(#4185)

### Configuration

Expand Down
6 changes: 4 additions & 2 deletions src/black/linegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -477,13 +477,15 @@ def visit_STRING(self, leaf: Leaf) -> Iterator[Line]:
last_line_length = len(lines[-1]) if docstring else 0

# If adding closing quotes would cause the last line to exceed
# the maximum line length then put a line break before the
# closing quotes
# the maximum line length, and the closing quote is not
# prefixed by a newline then put a line break before
# the closing quotes
if (
len(lines) > 1
and last_line_length + quote_len > self.mode.line_length
and len(indent) + quote_len <= self.mode.line_length
and not has_trailing_backslash
and not leaf.value[-4] == "\n"
veryslowcode marked this conversation as resolved.
Show resolved Hide resolved
):
leaf.value = prefix + quote + docstring + "\n" + indent + quote
else:
Expand Down
4 changes: 4 additions & 0 deletions tests/data/cases/docstring_newline_preview.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# flags: --preview
"""
87 characters ............................................................................
"""