Skip to content

Commit e559ff1

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

File tree

1 file changed

+25
-32
lines changed

1 file changed

+25
-32
lines changed

flake8_rst_docstrings.py

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

124124

125+
def special_case_unescaped(docstring, variable):
126+
"""Can RST213 for ``*arg``, or RST210 for ``*kwarg`` be ignored."""
127+
if "\nArgs:\n" in docstring and docstring.find("\nArgs:\n") < docstring.find(
128+
f" {variable}:"
129+
):
130+
# Ignore special case used in Google docstring style
131+
return True
132+
elif "\nParameters\n----------\n" in docstring:
133+
i = docstring.find("\nParameters\n----------\n")
134+
if i < docstring.find(f"\n{variable}\n") or i < docstring.find(
135+
f"\n{variable} :"
136+
):
137+
# Ignore special case used in NumPy docstring style
138+
return True
139+
return False
140+
141+
125142
class reStructuredTextChecker:
126143
"""Checker of Python docstrings as reStructuredText."""
127144

@@ -256,38 +273,14 @@ def run(self):
256273
code += 100 * rst_error.level
257274
msg = "%s%03i %s" % (rst_prefix, code, msg)
258275

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
276+
# Silence special case use of *args and **kwargs
277+
# (hopefully won't mask a true positive in same docstring)
278+
if code == 210 and special_case_unescaped(docstring, "**kwargs"):
279+
# Ignore special case used in Google/NumPy docstring style
280+
continue
281+
elif code == 213 and special_case_unescaped(docstring, "*args"):
282+
# Ignore special case used in Google/NumPy docstring style
283+
continue
291284

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

0 commit comments

Comments
 (0)