Skip to content

Commit 44579c1

Browse files
Add a name and an optional attribute to Formatters (#18)
1 parent a411244 commit 44579c1

File tree

5 files changed

+42
-1
lines changed

5 files changed

+42
-1
lines changed

.github/CONTRIBUTING.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
## Development
22

3+
### Linting
4+
35
Use `pre-commit install` to install the pre-commit hook for the repository.
46

7+
### Creating a new formatter
8+
9+
- Implement a Formatter by inheriting from ``pydocstringformatter.formatting.Formatter``
10+
- Add your new formatter to ``pydocstringformatter.formatting.FORMATTERS``
11+
- Choose a proper name because this will be user-facing: the name will be used for options of the CLI.
12+
513
### Testing
614

715
To run all the tests:
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
__all__ = ["FORMATTERS"]
22

3+
from typing import List
4+
5+
from pydocstringformatter.formatting.base import Formatter
36
from pydocstringformatter.formatting.formatter import (
47
BeginningQuotesFormatter,
58
ClosingQuotesFormatter,
69
)
710

8-
FORMATTERS = [
11+
FORMATTERS: List[Formatter] = [
912
BeginningQuotesFormatter(),
1013
ClosingQuotesFormatter(),
1114
]

pydocstringformatter/formatting/base.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,18 @@
55
class Formatter:
66
"""Base class for docstring formatter."""
77

8+
optional = False
9+
10+
@property
11+
@abc.abstractmethod
12+
def name(self) -> str:
13+
"""Name of the Formatter.
14+
15+
This will be used to create argparse options when added to
16+
'pydocstringformatter.formatting.FORMATTERS'. Therefore, it is
17+
user-facing and should be chosen carefully.
18+
"""
19+
820
@abc.abstractmethod
921
def treat_token(self, tokeninfo: tokenize.TokenInfo) -> tokenize.TokenInfo:
1022
"""Return a modified token."""

pydocstringformatter/formatting/formatter.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
class BeginningQuotesFormatter(StringFormatter):
88
"""Fix the position of the opening quotes"""
99

10+
name = "beginning-quotes"
11+
1012
def _treat_string(self, tokeninfo: tokenize.TokenInfo) -> str:
1113
new_string = tokeninfo.string
1214
if new_string[3] == "\n":
@@ -17,6 +19,8 @@ def _treat_string(self, tokeninfo: tokenize.TokenInfo) -> str:
1719
class ClosingQuotesFormatter(StringFormatter):
1820
"""Fix the position of the closing quotes"""
1921

22+
name = "closing-quotes"
23+
2024
def _treat_string(self, tokeninfo: tokenize.TokenInfo) -> str:
2125
"""Fix the position of end quotes for multi-line docstrings"""
2226
new_string = tokeninfo.string

tests/test_formatter.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from typing import Set
2+
3+
from pydocstringformatter.formatting import FORMATTERS
4+
5+
6+
def test_formatter_names() -> None:
7+
"""Test that each formatter name exists and is unique."""
8+
formatter_names: Set[str] = set()
9+
for formatter in FORMATTERS:
10+
assert formatter.name, "Each formatter should have a name set."
11+
assert (
12+
formatter.name not in formatter_names
13+
), "Each formatter should have an unique name."
14+
formatter_names.add(formatter.name)

0 commit comments

Comments
 (0)