Skip to content

Commit

Permalink
Refactor for less nesting
Browse files Browse the repository at this point in the history
  • Loading branch information
peterjc committed Dec 22, 2023
1 parent 4324c89 commit 2ed3313
Showing 1 changed file with 40 additions and 32 deletions.
72 changes: 40 additions & 32 deletions flake8_rst_docstrings.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,40 @@ def code_mapping(
return default


def special_case_arg(docstring):
"""Can RST213 for ``*arg`` be ignored."""
if "\nArgs:\n" in docstring and docstring.find("\nArgs:\n") < docstring.find(
" *args:"
):
# Ignore special case used in Google docstring style
return True
elif "\nParameters\n----------\n" in docstring:
if docstring.find("\nParameters\n----------\n") < docstring.find(
"\n*args\n"
) or docstring.find("\nParameters\n----------\n") < docstring.find("\n*args :"):
# Ignore special case used in NumPy docstring style
return True
return False


def special_case_kwargs(docstring):
"""Can RST210 for ``*kwarg`` be ignored."""
if "\nArgs:\n" in docstring and docstring.find("\nArgs:\n") < docstring.find(
" **kwargs:"
):
# Ignore special case used in Google docstring style
return True
elif "\nParameters\n----------\n" in docstring:
if docstring.find("\nParameters\n----------\n") < docstring.find(
"\n**kwargs\n"
) or docstring.find("\nParameters\n----------\n") < docstring.find(
"\n**kwargs :"
):
# Ignore special case used in NumPy docstring style
return True
return False


class reStructuredTextChecker:
"""Checker of Python docstrings as reStructuredText."""

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

if code == 210:
if "\nArgs:\n" in docstring and docstring.find(
"\nArgs:\n"
) < docstring.find(" **kwargs:"):
# Ignore special case used in Google docstring style
continue
if "\nParameters\n----------\n" in docstring and docstring.find(
"\nParameters\n----------\n"
) < docstring.find("\n**kwargs\n"):
# Ignore special case used in NumPy docstring style
continue
if "\nParameters\n----------\n" in docstring and docstring.find(
"\nParameters\n----------\n"
) < docstring.find("\n**kwargs :"):
# Ignore special case used in NumPy docstring style
continue
elif code == 213:
if "\nArgs:\n" in docstring and docstring.find(
"\nArgs:\n"
) < docstring.find(" *args:"):
# Ignore special case used in Google docstring style
continue
if "\nParameters\n----------\n" in docstring and docstring.find(
"\nParameters\n----------\n"
) < docstring.find("\n*args\n"):
# Ignore special case used in NumPy docstring style
continue
if "\nParameters\n----------\n" in docstring and docstring.find(
"\nParameters\n----------\n"
) < docstring.find("\n*args :"):
# Ignore special case used in NumPy docstring style
continue
if code == 210 and special_case_kwargs(docstring):
# Ignore special case used in Google/NumPy docstring style
continue
elif code == 213 and special_case_arg(docstring):
# Ignore special case used in Google/NumPy docstring style
continue

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

0 comments on commit 2ed3313

Please sign in to comment.