diff --git a/pydocstringformatter/formatting/base.py b/pydocstringformatter/formatting/base.py index 0aaeaae1..e7ca39d6 100644 --- a/pydocstringformatter/formatting/base.py +++ b/pydocstringformatter/formatting/base.py @@ -111,7 +111,7 @@ def _treat_description(self, description: str, indent_length: int) -> str: @staticmethod def _separate_summary_and_description( docstring: str, indent_length: int, quotes_length: Literal[1, 3] - ) -> Tuple[str, Optional[str]]: + ) -> Tuple[str, str, Optional[str]]: """Split the summary and description and handle quotes and indentation.""" if "\n\n" in docstring: summary, description = docstring.split("\n\n", maxsplit=1) @@ -132,7 +132,16 @@ def _separate_summary_and_description( if summary.endswith("\n"): summary = summary[:-1] - return summary, description + # Remove opening quotes + summary = summary[quotes_length:] + + # Prefix is the new-line + idententation for summaries that + # are not on the same line as the opening quotes + prefix = "" + if summary.startswith("\n"): + prefix = "\n" + indent_length * " " + summary = summary[1 + indent_length :] + return prefix, summary, description def _treat_string( self, @@ -141,17 +150,14 @@ def _treat_string( quotes: str, quotes_length: Literal[1, 3], ) -> str: - summary, description = self._separate_summary_and_description( + prefix, summary, description = self._separate_summary_and_description( tokeninfo.string, indent_length, quotes_length, ) - # Remove opening quotes - summary = summary[quotes_length:] - new_summary = self._treat_summary(summary, indent_length) - docstring = f"{quotes}{new_summary}" + docstring = f"{quotes}{prefix}{new_summary}" if description: new_description = self._treat_description(description, indent_length) diff --git a/tests/data/format/summary_splitter/function_docstrings.py b/tests/data/format/summary_splitter/function_docstrings.py index 3ca65e6d..aea4385b 100644 --- a/tests/data/format/summary_splitter/function_docstrings.py +++ b/tests/data/format/summary_splitter/function_docstrings.py @@ -26,3 +26,11 @@ def func(): def inner_func(): """Summary. Body without period""" + + +def func(): + """ + Summary. + + Body of the docstring. + """ diff --git a/tests/data/format/summary_splitter/function_docstrings.py.out b/tests/data/format/summary_splitter/function_docstrings.py.out index 881ccbf6..0b3fa42c 100644 --- a/tests/data/format/summary_splitter/function_docstrings.py.out +++ b/tests/data/format/summary_splitter/function_docstrings.py.out @@ -42,3 +42,11 @@ def func(): Body without period """ + + +def func(): + """ + Summary. + + Body of the docstring. + """