Skip to content

Commit dbb5742

Browse files
committed
add numpydoc section name checker
1 parent 94bec35 commit dbb5742

File tree

4 files changed

+48
-18
lines changed

4 files changed

+48
-18
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ dist/
44
docs/_build
55

66
.coverage
7+
*.pyc

pydocstringformatter/_formatting/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from pydocstringformatter._formatting.formatters_numpydoc import (
1717
NumpydocNameColonTypeFormatter,
1818
NumpydocSectionHyphenLengthFormatter,
19+
NumpydocSectionNameFormatter,
1920
NumpydocSectionOrderingFormatter,
2021
NumpydocSectionSpacingFormatter,
2122
)
@@ -35,6 +36,7 @@
3536
NumpydocNameColonTypeFormatter(),
3637
NumpydocSectionSpacingFormatter(),
3738
NumpydocSectionHyphenLengthFormatter(),
39+
NumpydocSectionNameFormatter(),
3840
LineWrapperFormatter(),
3941
BeginningQuotesFormatter(),
4042
ClosingQuotesFormatter(),

pydocstringformatter/_formatting/formatters_numpydoc.py

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,31 @@
44

55
from pydocstringformatter._formatting.base import NumpydocSectionFormatter
66

7+
NUMPYDOC_SECTIONS = (
8+
"Summary",
9+
"Parameters",
10+
"Attributes",
11+
"Methods",
12+
"Returns",
13+
"Yields",
14+
"Receives",
15+
"Other Parameters",
16+
"Raises",
17+
"Warns",
18+
"Warnings",
19+
"See Also",
20+
"Notes",
21+
"References",
22+
"Examples",
23+
) # Order must match numpydoc
24+
725

826
class NumpydocSectionOrderingFormatter(NumpydocSectionFormatter):
927
"""Change section order to match numpydoc guidelines."""
1028

1129
name = "numpydoc-section-order"
1230

13-
numpydoc_section_order = (
14-
"Summary",
15-
"Parameters",
16-
"Attributes",
17-
"Methods",
18-
"Returns",
19-
"Yields",
20-
"Receives",
21-
"Other Parameters",
22-
"Raises",
23-
"Warns",
24-
"Warnings",
25-
"See Also",
26-
"Notes",
27-
"References",
28-
"Examples",
29-
)
31+
numpydoc_section_order = NUMPYDOC_SECTIONS
3032

3133
def treat_sections(
3234
self, sections: OrderedDict[str, list[str]]
@@ -105,6 +107,22 @@ def treat_sections(
105107
return sections
106108

107109

110+
class NumpydocSectionNameFormatter(NumpydocSectionFormatter):
111+
"""Check if sections are named correctly."""
112+
113+
name = "numpydoc-section-name-checker"
114+
115+
def treat_sections(
116+
self, sections: OrderedDict[str, list[str]]
117+
) -> OrderedDict[str, list[str]]:
118+
"""Ensure proper spacing between sections."""
119+
for section_name in sections:
120+
if section_name not in NUMPYDOC_SECTIONS:
121+
raise ValueError(f"Invalid section_name '{section_name}'")
122+
123+
return sections
124+
125+
108126
class NumpydocSectionHyphenLengthFormatter(NumpydocSectionFormatter):
109127
"""Ensure hyphens after section header lines are proper length."""
110128

pydocstringformatter/run.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,9 @@ def format_file_tokens(
128128
UnstableResultError::
129129
If the formatters are not able to get to a stable result.
130130
It reports what formatters are still modifying the tokens.
131+
RuntimeError::
132+
If a formatter fails to apply on a docstring. It reports which
133+
file and which line numbers where the formatter failed.
131134
"""
132135
formatted_tokens: list[tokenize.TokenInfo] = []
133136
is_changed = False
@@ -136,7 +139,13 @@ def format_file_tokens(
136139
new_tokeninfo = tokeninfo
137140

138141
if _utils.is_docstring(new_tokeninfo, tokens[index - 1]):
139-
new_tokeninfo, changers = self.apply_formatters(new_tokeninfo)
142+
try:
143+
new_tokeninfo, changers = self.apply_formatters(new_tokeninfo)
144+
except Exception as err:
145+
start, end = new_tokeninfo.start[0], new_tokeninfo.end[0]
146+
raise RuntimeError(
147+
f"In {filename} L{start}-L{end}:\n\n{err}"
148+
) from err
140149
is_changed = is_changed or bool(changers)
141150

142151
# Run formatters again (3rd time) to check if the result is stable

0 commit comments

Comments
 (0)