Skip to content

Commit d9d1279

Browse files
Merge branch 'main' into issue_4149
2 parents 532e6b2 + 7edb50f commit d9d1279

File tree

8 files changed

+23
-6
lines changed

8 files changed

+23
-6
lines changed

.github/workflows/pypi_upload.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ jobs:
8989

9090
steps:
9191
- uses: actions/checkout@v4
92-
- uses: pypa/[email protected].4
92+
- uses: pypa/[email protected].5
9393
with:
9494
only: ${{ matrix.only }}
9595

CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
due to an outstanding crash and proposed formatting tweaks (#4198)
1919
- Fixed a bug where base expressions caused inconsistent formatting of \*\* in tenary
2020
expression (#4154)
21+
- Checking for newline before adding one on docstring that is almost at the line limit
22+
(#4185)
2123

2224
### Configuration
2325

docs/requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ Sphinx==7.2.6
66
docutils==0.20.1
77
sphinxcontrib-programoutput==0.17
88
sphinx_copybutton==0.5.2
9-
furo==2023.9.10
9+
furo==2024.1.29

docs/the_black_code_style/future_style.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ Currently, the following features are included in the preview style:
3030
parameters
3131
- `is_simple_lookup_for_doublestar_expression`: fix line length computation for certain
3232
expressions that involve the power operator
33+
- `docstring_check_for_newline`: checks if there is a newline before the terminating
34+
quotes of a docstring
3335

3436
(labels/unstable-features)=
3537

src/black/linegen.py

Lines changed: 10 additions & 3 deletions
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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ class Preview(Enum):
178178
multiline_string_handling = auto()
179179
typed_params_trailing_comma = auto()
180180
is_simple_lookup_for_doublestar_expression = auto()
181+
docstring_check_for_newline = auto()
181182

182183

183184
UNSTABLE_FEATURES: Set[Preview] = {

src/black/resources/black.schema.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@
8686
"wrap_long_dict_values_in_parens",
8787
"multiline_string_handling",
8888
"typed_params_trailing_comma",
89-
"is_simple_lookup_for_doublestar_expression"
89+
"is_simple_lookup_for_doublestar_expression",
90+
"docstring_check_for_newline"
9091
]
9192
},
9293
"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."
Lines changed: 4 additions & 0 deletions
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)