Skip to content

Commit 7edb50f

Browse files
fix: additional newline added to docstring when the previous line length is less than the line length limit minus 1 (#4185)
Co-authored-by: Jelle Zijlstra <[email protected]>
1 parent 3e80de3 commit 7edb50f

File tree

6 files changed

+21
-4
lines changed

6 files changed

+21
-4
lines changed

CHANGES.md

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
- Move the `hug_parens_with_braces_and_square_brackets` feature to the unstable style
1818
due to an outstanding crash and proposed formatting tweaks (#4198)
19+
- Checking for newline before adding one on docstring that is almost at the line limit
20+
(#4185)
1921

2022
### Configuration
2123

docs/the_black_code_style/future_style.md

+2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ Currently, the following features are included in the preview style:
2828
longer normalized
2929
- `typed_params_trailing_comma`: consistently add trailing commas to typed function
3030
parameters
31+
- `docstring_check_for_newline`: checks if there is a newline before the terminating
32+
quotes of a docstring
3133

3234
(labels/unstable-features)=
3335

src/black/linegen.py

+10-3
Original file line numberDiff line numberDiff line change
@@ -477,15 +477,22 @@ def visit_STRING(self, leaf: Leaf) -> Iterator[Line]:
477477
last_line_length = len(lines[-1]) if docstring else 0
478478

479479
# If adding closing quotes would cause the last line to exceed
480-
# the maximum line length then put a line break before the
481-
# closing quotes
480+
# the maximum line length, and the closing quote is not
481+
# prefixed by a newline then put a line break before
482+
# the closing quotes
482483
if (
483484
len(lines) > 1
484485
and last_line_length + quote_len > self.mode.line_length
485486
and len(indent) + quote_len <= self.mode.line_length
486487
and not has_trailing_backslash
487488
):
488-
leaf.value = prefix + quote + docstring + "\n" + indent + quote
489+
if (
490+
Preview.docstring_check_for_newline in self.mode
491+
and leaf.value[-1 - quote_len] == "\n"
492+
):
493+
leaf.value = prefix + quote + docstring + quote
494+
else:
495+
leaf.value = prefix + quote + docstring + "\n" + indent + quote
489496
else:
490497
leaf.value = prefix + quote + docstring + quote
491498
else:

src/black/mode.py

+1
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ class Preview(Enum):
177177
wrap_long_dict_values_in_parens = auto()
178178
multiline_string_handling = auto()
179179
typed_params_trailing_comma = auto()
180+
docstring_check_for_newline = auto()
180181

181182

182183
UNSTABLE_FEATURES: Set[Preview] = {

src/black/resources/black.schema.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@
8585
"no_normalize_fmt_skip_whitespace",
8686
"wrap_long_dict_values_in_parens",
8787
"multiline_string_handling",
88-
"typed_params_trailing_comma"
88+
"typed_params_trailing_comma",
89+
"docstring_check_for_newline"
8990
]
9091
},
9192
"description": "Enable specific features included in the `--unstable` style. Requires `--preview`. No compatibility guarantees are provided on the behavior or existence of any unstable features."
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# flags: --preview
2+
"""
3+
87 characters ............................................................................
4+
"""

0 commit comments

Comments
 (0)