Skip to content

Commit 2ed3313

Browse files
committed
Refactor for less nesting
1 parent 4324c89 commit 2ed3313

File tree

1 file changed

+40
-32
lines changed

1 file changed

+40
-32
lines changed

flake8_rst_docstrings.py

Lines changed: 40 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,40 @@ def code_mapping(
122122
return default
123123

124124

125+
def special_case_arg(docstring):
126+
"""Can RST213 for ``*arg`` be ignored."""
127+
if "\nArgs:\n" in docstring and docstring.find("\nArgs:\n") < docstring.find(
128+
" *args:"
129+
):
130+
# Ignore special case used in Google docstring style
131+
return True
132+
elif "\nParameters\n----------\n" in docstring:
133+
if docstring.find("\nParameters\n----------\n") < docstring.find(
134+
"\n*args\n"
135+
) or docstring.find("\nParameters\n----------\n") < docstring.find("\n*args :"):
136+
# Ignore special case used in NumPy docstring style
137+
return True
138+
return False
139+
140+
141+
def special_case_kwargs(docstring):
142+
"""Can RST210 for ``*kwarg`` be ignored."""
143+
if "\nArgs:\n" in docstring and docstring.find("\nArgs:\n") < docstring.find(
144+
" **kwargs:"
145+
):
146+
# Ignore special case used in Google docstring style
147+
return True
148+
elif "\nParameters\n----------\n" in docstring:
149+
if docstring.find("\nParameters\n----------\n") < docstring.find(
150+
"\n**kwargs\n"
151+
) or docstring.find("\nParameters\n----------\n") < docstring.find(
152+
"\n**kwargs :"
153+
):
154+
# Ignore special case used in NumPy docstring style
155+
return True
156+
return False
157+
158+
125159
class reStructuredTextChecker:
126160
"""Checker of Python docstrings as reStructuredText."""
127161

@@ -256,38 +290,12 @@ def run(self):
256290
code += 100 * rst_error.level
257291
msg = "%s%03i %s" % (rst_prefix, code, msg)
258292

259-
if code == 210:
260-
if "\nArgs:\n" in docstring and docstring.find(
261-
"\nArgs:\n"
262-
) < docstring.find(" **kwargs:"):
263-
# Ignore special case used in Google docstring style
264-
continue
265-
if "\nParameters\n----------\n" in docstring and docstring.find(
266-
"\nParameters\n----------\n"
267-
) < docstring.find("\n**kwargs\n"):
268-
# Ignore special case used in NumPy docstring style
269-
continue
270-
if "\nParameters\n----------\n" in docstring and docstring.find(
271-
"\nParameters\n----------\n"
272-
) < docstring.find("\n**kwargs :"):
273-
# Ignore special case used in NumPy docstring style
274-
continue
275-
elif code == 213:
276-
if "\nArgs:\n" in docstring and docstring.find(
277-
"\nArgs:\n"
278-
) < docstring.find(" *args:"):
279-
# Ignore special case used in Google docstring style
280-
continue
281-
if "\nParameters\n----------\n" in docstring and docstring.find(
282-
"\nParameters\n----------\n"
283-
) < docstring.find("\n*args\n"):
284-
# Ignore special case used in NumPy docstring style
285-
continue
286-
if "\nParameters\n----------\n" in docstring and docstring.find(
287-
"\nParameters\n----------\n"
288-
) < docstring.find("\n*args :"):
289-
# Ignore special case used in NumPy docstring style
290-
continue
293+
if code == 210 and special_case_kwargs(docstring):
294+
# Ignore special case used in Google/NumPy docstring style
295+
continue
296+
elif code == 213 and special_case_arg(docstring):
297+
# Ignore special case used in Google/NumPy docstring style
298+
continue
291299

292300
# We don't know the column number, leaving as zero.
293301
yield start + rst_error.line, 0, msg, type(self)

0 commit comments

Comments
 (0)