File tree Expand file tree Collapse file tree 4 files changed +48
-18
lines changed Expand file tree Collapse file tree 4 files changed +48
-18
lines changed Original file line number Diff line number Diff line change 4
4
docs /_build
5
5
6
6
.coverage
7
+ * .pyc
Original file line number Diff line number Diff line change 16
16
from pydocstringformatter ._formatting .formatters_numpydoc import (
17
17
NumpydocNameColonTypeFormatter ,
18
18
NumpydocSectionHyphenLengthFormatter ,
19
+ NumpydocSectionNameFormatter ,
19
20
NumpydocSectionOrderingFormatter ,
20
21
NumpydocSectionSpacingFormatter ,
21
22
)
35
36
NumpydocNameColonTypeFormatter (),
36
37
NumpydocSectionSpacingFormatter (),
37
38
NumpydocSectionHyphenLengthFormatter (),
39
+ NumpydocSectionNameFormatter (),
38
40
LineWrapperFormatter (),
39
41
BeginningQuotesFormatter (),
40
42
ClosingQuotesFormatter (),
Original file line number Diff line number Diff line change 4
4
5
5
from pydocstringformatter ._formatting .base import NumpydocSectionFormatter
6
6
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
+
7
25
8
26
class NumpydocSectionOrderingFormatter (NumpydocSectionFormatter ):
9
27
"""Change section order to match numpydoc guidelines."""
10
28
11
29
name = "numpydoc-section-order"
12
30
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
30
32
31
33
def treat_sections (
32
34
self , sections : OrderedDict [str , list [str ]]
@@ -105,6 +107,22 @@ def treat_sections(
105
107
return sections
106
108
107
109
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
+
108
126
class NumpydocSectionHyphenLengthFormatter (NumpydocSectionFormatter ):
109
127
"""Ensure hyphens after section header lines are proper length."""
110
128
Original file line number Diff line number Diff line change @@ -128,6 +128,9 @@ def format_file_tokens(
128
128
UnstableResultError::
129
129
If the formatters are not able to get to a stable result.
130
130
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.
131
134
"""
132
135
formatted_tokens : list [tokenize .TokenInfo ] = []
133
136
is_changed = False
@@ -136,7 +139,13 @@ def format_file_tokens(
136
139
new_tokeninfo = tokeninfo
137
140
138
141
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
140
149
is_changed = is_changed or bool (changers )
141
150
142
151
# Run formatters again (3rd time) to check if the result is stable
You can’t perform that action at this time.
0 commit comments