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

Fix the SummaryFormatter #76

Merged
merged 4 commits into from
Mar 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
- name: Install dependencies
run: pip install -U -r requirements-test.txt
- name: Run pytest
run: pytest --cov --cov-report= tests/
run: pytest -vv --cov --cov-report= tests/
- name: Upload coverage artifact
uses: actions/[email protected]
with:
Expand Down Expand Up @@ -81,4 +81,4 @@ jobs:
- name: Install dependencies
run: pip install -U -r requirements-test.txt
- name: Run pytest
run: pytest
run: pytest -vv
18 changes: 16 additions & 2 deletions pydocstringformatter/formatting/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,15 +112,25 @@ def _treat_string(
quotes_length: Literal[1, 3],
) -> str:
"""Return a modified string."""
# TODO(#67): Handle identation at the end of a summary a little better

# Split summary and description
if "\n\n" in tokeninfo.string:
summary, description = tokeninfo.string.split("\n\n", maxsplit=1)

# Remove final indentation, ending quotes and new line
description = description[:-quotes_length]
if indent_length and description.endswith(indent_length * " "):
description = description[:-indent_length]
if description.endswith("\n"):
description = description[:-1]
else:
summary, description = tokeninfo.string, None

# Remove final indentation, ending quotes and new line
summary = summary[:-quotes_length]
if indent_length and summary.endswith(indent_length * " "):
summary = summary[:-indent_length]
if summary.endswith("\n"):
summary = summary[:-1]

# Remove opening quotes
summary = summary[quotes_length:]
Expand All @@ -131,4 +141,8 @@ def _treat_string(
docstring = f"{quotes}{new_summary}"
if description:
docstring += f"\n\n{description}"

# Determine whether ending quotes were initially on same or new line
if tokeninfo.string.splitlines()[-1] == indent_length * " " + quotes:
return f"{docstring}\n{indent_length * ' '}{quotes}"
return f"{docstring}{quotes}"
24 changes: 6 additions & 18 deletions pydocstringformatter/formatting/formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,29 +161,17 @@ def _treat_summary(self, summary: str, indent_length: int) -> str:
)

# Handle summaries that end with a period and a direct new line
# but not a double new line.
elif summary[index + 1] == "\n":
# If this is the end of the docstring, don't do anything
if summary[index + 2 :] == indent_length * " ":
new_summary = summary
# Split between period and rest of docstring
else:
new_summary = summary[:index] + ".\n\n" + summary[index + 2 :]
new_summary = summary[:index] + ".\n\n" + summary[index + 2 :]

# Try to split on max length
if not new_summary and summary.count("\n") > self.config.max_summary_lines - 1:
lines = summary.splitlines()
new_summary = "\n".join(lines[: self.config.max_summary_lines])

# Handle summaries without any additional text beyond max lines
if lines[self.config.max_summary_lines] == indent_length * " ":
new_summary += "\n" + lines[self.config.max_summary_lines]

# Split between max lines and rest of docstring
else:
new_summary += "\n\n" + "\n".join(
lines[self.config.max_summary_lines :]
)
new_summary = (
"\n".join(lines[: self.config.max_summary_lines])
+ "\n\n"
+ "\n".join(lines[self.config.max_summary_lines :])
)

return new_summary or summary

Expand Down
2 changes: 2 additions & 0 deletions tests/data/format/summary_splitter/ending_quotes.args
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
--split-summary-body
--no-closing-quotes
22 changes: 22 additions & 0 deletions tests/data/format/summary_splitter/ending_quotes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
def func():
"""We should keep the position of the quotes
as is.
"""


def func():
"""We should keep the position of the quotes
as is."""


def func():
"""We should keep the position of the quotes

as is.
"""


def func():
"""We should keep the position of the quotes

as is."""
24 changes: 24 additions & 0 deletions tests/data/format/summary_splitter/ending_quotes.py.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
def func():
"""We should keep the position of the quotes.

as is.
"""


def func():
"""We should keep the position of the quotes.

as is."""


def func():
"""We should keep the position of the quotes.

as is.
"""


def func():
"""We should keep the position of the quotes.

as is."""