Skip to content

Commit c3d7578

Browse files
committed
Add formatter of final period for single line docstrings and summaries
1 parent d1bbf0e commit c3d7578

File tree

11 files changed

+216
-1
lines changed

11 files changed

+216
-1
lines changed

README.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ multi-line docstring
6666
"""
6767
```
6868

69-
**PEP 256: _The closing quotes are on the same line as the opening quotes_**
69+
**PEP 257: _The closing quotes are on the same line as the opening quotes_**
7070

7171
For consistency this rule also gets applied to multi-line docstrings
7272

@@ -93,6 +93,28 @@ multi-line docstring
9393
"""
9494
```
9595

96+
**PEP 257: _The docstring is a phrase ending in a period & Multi-line docstrings consist
97+
of a summary line just like a one-line docstring_**
98+
99+
```python
100+
# Bad
101+
"""My docstring"""
102+
103+
"""Summary
104+
105+
My docstring
106+
"""
107+
108+
109+
# Good
110+
"""My docstring."""
111+
112+
"""Summary.
113+
114+
My docstring
115+
"""
116+
```
117+
96118
## Development
97119

98120
For development and contributing guidelines please see

pydocstringformatter/formatting/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66
from pydocstringformatter.formatting.formatter import (
77
BeginningQuotesFormatter,
88
ClosingQuotesFormatter,
9+
FinalPeriodFormatter,
910
)
1011

1112
FORMATTERS: List[Formatter] = [
1213
BeginningQuotesFormatter(),
1314
ClosingQuotesFormatter(),
15+
FinalPeriodFormatter(),
1416
]

pydocstringformatter/formatting/formatter.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,32 @@ def _treat_string(self, tokeninfo: tokenize.TokenInfo) -> str:
3737
elif len(split_string) == 2 and split_string[-1] == good_end:
3838
new_string = "\n".join(split_string[:-1]) + tokeninfo.string[0] * 3
3939
return new_string
40+
41+
42+
class FinalPeriodFormatter(StringFormatter):
43+
"""Add a period to the end of single line docstrings and summaries."""
44+
45+
name = "final-period"
46+
47+
def _treat_string(self, tokeninfo: tokenize.TokenInfo) -> str:
48+
"""Add a period to the end of single-line docstrings and summaries."""
49+
# Handle single line docstrings
50+
if not tokeninfo.string.count("\n"):
51+
if tokeninfo.string[-4] != ".":
52+
return tokeninfo.string[:-3] + "." + tokeninfo.string[-3:]
53+
# Handle multi-line docstrings
54+
else:
55+
first_linebreak = tokeninfo.string.index("\n")
56+
# If first linebreak is followed by another we're dealing with a summary
57+
if tokeninfo.string[tokeninfo.string.index("\n") + 1] == "\n":
58+
if tokeninfo.string[first_linebreak - 1] != ".":
59+
return (
60+
tokeninfo.string[:first_linebreak]
61+
+ "."
62+
+ tokeninfo.string[first_linebreak:]
63+
)
64+
# pylint: disable=fixme
65+
# TODO: Handle multi-line docstrings that do not have a summary
66+
# This is obviously dependent on whether 'pydocstringformatter' will
67+
# start enforcing summaries :)
68+
return tokeninfo.string
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class MyClass:
2+
"""Docstring"""
3+
4+
class InnerClass:
5+
"""Docstring"""
6+
7+
8+
class MyClass:
9+
"""A multi-line
10+
docstring
11+
"""
12+
13+
class InnerClass:
14+
"""A multi-line
15+
docstring
16+
"""
17+
18+
19+
class MyClass:
20+
"""Summary
21+
22+
docstring
23+
"""
24+
25+
class InnerClass:
26+
"""Summary
27+
28+
docstring
29+
"""
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class MyClass:
2+
"""Docstring."""
3+
4+
class InnerClass:
5+
"""Docstring."""
6+
7+
8+
class MyClass:
9+
"""A multi-line
10+
docstring
11+
"""
12+
13+
class InnerClass:
14+
"""A multi-line
15+
docstring
16+
"""
17+
18+
19+
class MyClass:
20+
"""Summary.
21+
22+
docstring
23+
"""
24+
25+
class InnerClass:
26+
"""Summary.
27+
28+
docstring
29+
"""
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
def func()
2+
"""Docstring"""
3+
4+
def inner_func()
5+
"""Docstring"""
6+
7+
8+
def func()
9+
"""A multi-line
10+
docstring
11+
"""
12+
13+
def inner_func()
14+
"""A multi-line
15+
docstring
16+
"""
17+
18+
19+
def func()
20+
"""Summary
21+
22+
docstring
23+
"""
24+
25+
def inner_func()
26+
"""Summary
27+
28+
docstring
29+
"""
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
def func()
2+
"""Docstring."""
3+
4+
def inner_func()
5+
"""Docstring."""
6+
7+
8+
def func()
9+
"""A multi-line
10+
docstring
11+
"""
12+
13+
def inner_func()
14+
"""A multi-line
15+
docstring
16+
"""
17+
18+
19+
def func()
20+
"""Summary.
21+
22+
docstring
23+
"""
24+
25+
def inner_func()
26+
"""Summary.
27+
28+
docstring
29+
"""
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
"""Docstring"""
2+
3+
"""A multi-line
4+
docstring
5+
"""
6+
7+
"""Summary
8+
9+
docstring
10+
"""
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
"""Docstring."""
2+
3+
"""A multi-line
4+
docstring
5+
"""
6+
7+
"""Summary.
8+
9+
docstring
10+
"""
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
MYVAR = 1
2+
"""Docstring"""
3+
4+
MYVAR = 1
5+
"""A multi-line
6+
docstring
7+
"""
8+
9+
MYVAR = 1
10+
"""Summary
11+
12+
A docstring
13+
"""

0 commit comments

Comments
 (0)