Skip to content
This repository has been archived by the owner on Nov 3, 2023. It is now read-only.

Commit

Permalink
Make the missing docstring check more robust
Browse files Browse the repository at this point in the history
  • Loading branch information
samj1912 committed Sep 6, 2020
1 parent e39f80b commit e6a7c82
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
10 changes: 6 additions & 4 deletions src/pydocstyle/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,13 @@ def checks(self):
for this_check in vars(type(self)).values()
if hasattr(this_check, '_check_for')
]
# This returns the checks in the order they are
# listed in the file (since py3.6) if their priority is the same
return sorted(all, key=lambda this_check: not this_check._terminal)

# Note - this needs to be listed before other checks
# as f string evalutaion may cause malformed AST Nodes.
# So we need to run this check first and terminate early.
@check_for(Definition, terminal=True)
def check_docstring_fstring(self, definition, docstring):
"""D303: Docstrings may not be f-strings.
Expand All @@ -213,14 +218,11 @@ def check_docstring_missing(self, definition, docstring):
with a single underscore.
"""
if _is_fstring(docstring):
return # checked above in check_docstring_fstring

if (
not docstring
and definition.is_public
or docstring
and is_blank(ast.literal_eval(docstring))
and is_blank(docstring, literal_eval=True)
):
codes = {
Module: violations.D100,
Expand Down
9 changes: 8 additions & 1 deletion src/pydocstyle/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,15 @@
NON_ALPHANUMERIC_STRIP_RE = re.compile(r'[\W_]+')


def is_blank(string: str) -> bool:
def is_blank(string: str, literal_eval: bool = False) -> bool:
"""Return True iff the string contains only whitespaces."""
if literal_eval:
try:
string = ast.literal_eval(string)
except ValueError:
# This happens in case of an fstring
# in which case let's return false
return False
return not string.strip()


Expand Down
5 changes: 5 additions & 0 deletions src/tests/test_cases/fstrings.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,8 @@ def fstring_with_other_errors(arg=1, missing_arg=2):
This should not raise any other errors since fstrings
are a terminal check.
"""


@D303
def fstring_with_blank_doc_string():
f""" """

0 comments on commit e6a7c82

Please sign in to comment.