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

Docstring too long (issue #1632 fix) #3044

Merged
merged 19 commits into from May 8, 2022
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
471babd
Check line length of last line of docstring to decide whether or not …
idorrington92 May 3, 2022
0cd15a1
Fixing bugs. last_line_length must be 0 for empty strings and we need…
idorrington92 May 3, 2022
6413776
updating change log
idorrington92 May 3, 2022
3d27415
Moving change to preview style section
idorrington92 May 4, 2022
e618a5a
Adding to Preview
idorrington92 May 4, 2022
bbf6053
added tests for docstrings that go over line limit and one at the lin…
idorrington92 May 4, 2022
07fc574
Converting to preview
idorrington92 May 4, 2022
ff75a6f
using preview correctly this time
idorrington92 May 4, 2022
af631ef
Changing test to fit with using the preview now
idorrington92 May 4, 2022
06f907a
Adding preview tests and removing from regular test script
idorrington92 May 4, 2022
ef3e6c3
Putting tests back
idorrington92 May 4, 2022
ed261cb
Update tests/test_format.py
idorrington92 May 4, 2022
9923e9c
The logic was slighly wrong. It needed to account for the fact that m…
idorrington92 May 4, 2022
c52f493
Merge branch 'docstring_too_ling' of https://github.com/idorrington92…
idorrington92 May 4, 2022
5ff6d8b
renaming preview test file
idorrington92 May 4, 2022
70b4164
added new test that checks multiline docstrings at the line limit
idorrington92 May 4, 2022
9e8dc5f
Fixing bug where prefix was sometimes considered in line length when …
idorrington92 May 5, 2022
cf6700f
Added tests with prefixes
idorrington92 May 5, 2022
85d5bf9
Fixing comments
idorrington92 May 5, 2022
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
1 change: 1 addition & 0 deletions CHANGES.md
Expand Up @@ -17,6 +17,7 @@

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

- Fixed bug where docstrings with triple quotes could exceed max line length (#3044)
- Remove redundant parentheses around awaited objects (#2991)
- Parentheses around return annotations are now managed (#2990)
- Remove unnecessary parentheses from `with` statements (#2926)
Expand Down
21 changes: 19 additions & 2 deletions src/black/linegen.py
Expand Up @@ -305,9 +305,9 @@ def visit_STRING(self, leaf: Leaf) -> Iterator[Line]:
quote_len = 1 if docstring[1] != quote_char else 3
docstring = docstring[quote_len:-quote_len]
docstring_started_empty = not docstring
indent = " " * 4 * self.current_line.depth

if is_multiline_string(leaf):
indent = " " * 4 * self.current_line.depth
felix-hilden marked this conversation as resolved.
Show resolved Hide resolved
docstring = fix_docstring(docstring, indent)
else:
docstring = docstring.strip()
Expand All @@ -329,7 +329,24 @@ def visit_STRING(self, leaf: Leaf) -> Iterator[Line]:

# We could enforce triple quotes at this point.
quote = quote_char * quote_len
leaf.value = prefix + quote + docstring + quote

if Preview.long_docstring_quotes_on_newline in self.mode:
leaf.value = prefix + quote + docstring + quote
else:
felix-hilden marked this conversation as resolved.
Show resolved Hide resolved
# Put closing quotes on new line if max line length exceeded
last_line_length = len(docstring.splitlines()[-1]) if docstring else 0

# Make the docstring apart from the closing quotes, which happen below
docstring = prefix + quote + docstring
if (
len(indent) + len(prefix) + 2 * len(quote) + last_line_length
<= self.mode.line_length
):
felix-hilden marked this conversation as resolved.
Show resolved Hide resolved
docstring += quote
else:
docstring += "\n" + indent + quote

leaf.value = docstring

yield from self.visit_default(leaf)

Expand Down
1 change: 1 addition & 0 deletions src/black/mode.py
Expand Up @@ -147,6 +147,7 @@ class Preview(Enum):
remove_redundant_parens = auto()
one_element_subscript = auto()
annotation_parens = auto()
long_docstring_quotes_on_newline = auto()


class Deprecated(UserWarning):
Expand Down
32 changes: 32 additions & 0 deletions tests/data/docstring.py
Expand Up @@ -188,6 +188,22 @@ def my_god_its_full_of_stars_2():
"I'm sorry Dave "


def docstring_almost_at_line_limit():
"""long docstring.................................................................
"""


def docstring_almost_at_line_limit2():
"""long docstring.................................................................

..................................................................................
"""


def docstring_at_line_limit():
"""long docstring................................................................"""


# output

class MyClass:
Expand Down Expand Up @@ -375,3 +391,19 @@ def my_god_its_full_of_stars_1():
# the space below is actually a \u2001, removed in output
def my_god_its_full_of_stars_2():
"I'm sorry Dave"


def docstring_almost_at_line_limit():
"""long docstring.................................................................
"""


def docstring_almost_at_line_limit2():
"""long docstring.................................................................

..................................................................................
"""


def docstring_at_line_limit():
"""long docstring................................................................"""