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

Create SummaryAndDescriptionFormatter #82

Merged
merged 2 commits into from
Mar 18, 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
62 changes: 44 additions & 18 deletions pydocstringformatter/formatting/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import argparse
import re
import tokenize
from typing import Literal
from typing import Literal, Optional, Tuple


class Formatter:
Expand Down Expand Up @@ -97,24 +97,24 @@ def treat_token(self, tokeninfo: tokenize.TokenInfo) -> tokenize.TokenInfo:
)


class SummaryFormatter(StringAndQuotesFormatter):
"""Base class for formatter that only modifies the summary of a docstring."""
class SummaryAndDescriptionFormatter(StringAndQuotesFormatter):
"""Base class for formatter that modifies the summary and description."""

@abc.abstractmethod
def _treat_summary(self, summary: str, indent_length: int) -> str:
"""Return a modified summary."""

def _treat_string(
self,
tokeninfo: tokenize.TokenInfo,
indent_length: int,
quotes: str,
quotes_length: Literal[1, 3],
) -> str:
"""Return a modified string."""
# Split summary and description
if "\n\n" in tokeninfo.string:
summary, description = tokeninfo.string.split("\n\n", maxsplit=1)
@abc.abstractmethod
def _treat_description(self, description: str, indent_length: int) -> str:
"""Return a modified description."""

@staticmethod
def _separate_summary_and_description(
docstring: str, indent_length: int, quotes_length: Literal[1, 3]
) -> Tuple[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)

# Remove final indentation, ending quotes and new line
description = description[:-quotes_length]
Expand All @@ -123,7 +123,7 @@ def _treat_string(
if description.endswith("\n"):
description = description[:-1]
else:
summary, description = tokeninfo.string, None
summary, description = docstring, None

# Remove final indentation, ending quotes and new line
summary = summary[:-quotes_length]
Expand All @@ -132,17 +132,43 @@ def _treat_string(
if summary.endswith("\n"):
summary = summary[:-1]

return summary, description

def _treat_string(
self,
tokeninfo: tokenize.TokenInfo,
indent_length: int,
quotes: str,
quotes_length: Literal[1, 3],
) -> str:
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)

# Re-compose docstring
docstring = f"{quotes}{new_summary}"

if description:
docstring += f"\n\n{description}"
new_description = self._treat_description(description, indent_length)
docstring += f"\n\n{new_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}"


class SummaryFormatter(SummaryAndDescriptionFormatter):
"""Base class for formatter that only modifies the summary of a docstring."""

@abc.abstractmethod
def _treat_summary(self, summary: str, indent_length: int) -> str:
"""Return a modified summary."""

def _treat_description(self, description: str, indent_length: int) -> str:
return description
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ files = "pydocstringformatter,tests"
exclude = "tests/data.*"
strict = true
show_error_codes = true
enable_error_code = "ignore-without-code"

[tool.pylint.MASTER]
load-plugins=[
Expand Down