Skip to content

Commit 8a12f8b

Browse files
committed
Fix handling of summaries on a separate line
1 parent 1fbcde6 commit 8a12f8b

File tree

3 files changed

+29
-7
lines changed

3 files changed

+29
-7
lines changed

pydocstringformatter/formatting/base.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ def _treat_description(self, description: str, indent_length: int) -> str:
111111
@staticmethod
112112
def _separate_summary_and_description(
113113
docstring: str, indent_length: int, quotes_length: Literal[1, 3]
114-
) -> Tuple[str, Optional[str]]:
114+
) -> Tuple[str, str, Optional[str]]:
115115
"""Split the summary and description and handle quotes and indentation."""
116116
if "\n\n" in docstring:
117117
summary, description = docstring.split("\n\n", maxsplit=1)
@@ -132,7 +132,16 @@ def _separate_summary_and_description(
132132
if summary.endswith("\n"):
133133
summary = summary[:-1]
134134

135-
return summary, description
135+
# Remove opening quotes
136+
summary = summary[quotes_length:]
137+
138+
# Prefix is the new-line + idententation for summaries that
139+
# are not on the same line as the opening quotes
140+
prefix = ""
141+
if summary.startswith("\n"):
142+
prefix = "\n" + indent_length * " "
143+
summary = summary[1 + indent_length :]
144+
return prefix, summary, description
136145

137146
def _treat_string(
138147
self,
@@ -141,17 +150,14 @@ def _treat_string(
141150
quotes: str,
142151
quotes_length: Literal[1, 3],
143152
) -> str:
144-
summary, description = self._separate_summary_and_description(
153+
prefix, summary, description = self._separate_summary_and_description(
145154
tokeninfo.string,
146155
indent_length,
147156
quotes_length,
148157
)
149158

150-
# Remove opening quotes
151-
summary = summary[quotes_length:]
152-
153159
new_summary = self._treat_summary(summary, indent_length)
154-
docstring = f"{quotes}{new_summary}"
160+
docstring = f"{quotes}{prefix}{new_summary}"
155161

156162
if description:
157163
new_description = self._treat_description(description, indent_length)

tests/data/format/summary_splitter/function_docstrings.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,11 @@ def func():
2626
def inner_func():
2727
"""Summary.
2828
Body without period"""
29+
30+
31+
def func():
32+
"""
33+
Summary.
34+
35+
Body of the docstring.
36+
"""

tests/data/format/summary_splitter/function_docstrings.py.out

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,11 @@ def func():
4242

4343
Body without period
4444
"""
45+
46+
47+
def func():
48+
"""
49+
Summary.
50+
51+
Body of the docstring.
52+
"""

0 commit comments

Comments
 (0)