Skip to content
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
8 changes: 8 additions & 0 deletions .github/CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
## Development

### Linting

Use `pre-commit install` to install the pre-commit hook for the repository.

### Creating a new formatter

- Implement a Formatter by inheriting from ``pydocstringformatter.formatting.Formatter``
- Add your new formatter to ``pydocstringformatter.formatting.FORMATTERS``
- Choose a proper name because this will be user-facing: the name will be used for options of the CLI.

### Testing

To run all the tests:
Expand Down
5 changes: 4 additions & 1 deletion pydocstringformatter/formatting/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
__all__ = ["FORMATTERS"]

from typing import List

from pydocstringformatter.formatting.base import Formatter
from pydocstringformatter.formatting.formatter import (
BeginningQuotesFormatter,
ClosingQuotesFormatter,
)

FORMATTERS = [
FORMATTERS: List[Formatter] = [
BeginningQuotesFormatter(),
ClosingQuotesFormatter(),
]
12 changes: 12 additions & 0 deletions pydocstringformatter/formatting/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,18 @@
class Formatter:
"""Base class for docstring formatter."""

optional = False

@property
@abc.abstractmethod
def name(self) -> str:
"""Name of the Formatter.

This will be used to create argparse options when added to
'pydocstringformatter.formatting.FORMATTERS'. Therefore, it is
user-facing and should be chosen carefully.
"""

@abc.abstractmethod
def treat_token(self, tokeninfo: tokenize.TokenInfo) -> tokenize.TokenInfo:
"""Return a modified token."""
Expand Down
4 changes: 4 additions & 0 deletions pydocstringformatter/formatting/formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
class BeginningQuotesFormatter(StringFormatter):
"""Fix the position of the opening quotes"""

name = "beginning-quotes"

def _treat_string(self, tokeninfo: tokenize.TokenInfo) -> str:
new_string = tokeninfo.string
if new_string[3] == "\n":
Expand All @@ -17,6 +19,8 @@ def _treat_string(self, tokeninfo: tokenize.TokenInfo) -> str:
class ClosingQuotesFormatter(StringFormatter):
"""Fix the position of the closing quotes"""

name = "closing-quotes"

def _treat_string(self, tokeninfo: tokenize.TokenInfo) -> str:
"""Fix the position of end quotes for multi-line docstrings"""
new_string = tokeninfo.string
Expand Down
14 changes: 14 additions & 0 deletions tests/test_formatter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from typing import Set

from pydocstringformatter.formatting import FORMATTERS


def test_formatter_names() -> None:
"""Test that each formatter name exists and is unique."""
formatter_names: Set[str] = set()
for formatter in FORMATTERS:
assert formatter.name, "Each formatter should have a name set."
assert (
formatter.name not in formatter_names
), "Each formatter should have an unique name."
formatter_names.add(formatter.name)