diff --git a/pydocstringformatter/formatting/formatter.py b/pydocstringformatter/formatting/formatter.py index 8a6d8f97..5fde7a51 100644 --- a/pydocstringformatter/formatting/formatter.py +++ b/pydocstringformatter/formatting/formatter.py @@ -70,8 +70,21 @@ def _treat_summary( ) -> str: """Wrap the summary of a docstring.""" - # We need to deduct ending quotes if there is no description - line_length = 88 if description_exists else 88 - quotes_length + line_length = 88 + + # Without a description we need to consider the length including closing quotes + if not description_exists: + + # Calculate length without the ending quotes + length_without_ending = indent_length + quotes_length + len(summary) + + # If potential length is less than line length we need to consider ending + # quotes as well for the line length + if length_without_ending < line_length: + # We substract one more because we don't want a new line with just the + # ending quotes + line_length -= quotes_length + 1 + summary_lines = summary.splitlines() new_summary = "\n".join( diff --git a/tests/data/format/linewrap_summary/function_docstrings.py b/tests/data/format/linewrap_summary/function_docstrings.py index 3f1d8318..f104ca35 100644 --- a/tests/data/format/linewrap_summary/function_docstrings.py +++ b/tests/data/format/linewrap_summary/function_docstrings.py @@ -25,3 +25,17 @@ def func(): # exceeds the max length. def func(): """A multi-line summary that can be on one line. Something that is just too longgg.""" + + +def func(): + """A multi-line summary that can be on one line. Something that is just too long.""" + + +def func(): + """A multi-line summary that can be on one line. Something that is just too lon.""" + + +# Regression for bug found in pylint +# We should re-add the quotes to line length if they will never be on the first line. +class LinesChunk: + """The LinesChunk object computes and stores the hash of some consecutive stripped lines of a lineset.""" diff --git a/tests/data/format/linewrap_summary/function_docstrings.py.out b/tests/data/format/linewrap_summary/function_docstrings.py.out index 847df4af..8ecaef32 100644 --- a/tests/data/format/linewrap_summary/function_docstrings.py.out +++ b/tests/data/format/linewrap_summary/function_docstrings.py.out @@ -29,3 +29,21 @@ def func(): """A multi-line summary that can be on one line. Something that is just too longgg. """ + + +def func(): + """A multi-line summary that can be on one line. Something that is just too + long. + """ + + +def func(): + """A multi-line summary that can be on one line. Something that is just too lon.""" + + +# Regression for bug found in pylint +# We should re-add the quotes to line length if they will never be on the first line. +class LinesChunk: + """The LinesChunk object computes and stores the hash of some consecutive stripped + lines of a lineset. + """