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

Commit e6a7c82

Browse files
committed
Make the missing docstring check more robust
1 parent e39f80b commit e6a7c82

File tree

3 files changed

+19
-5
lines changed

3 files changed

+19
-5
lines changed

src/pydocstyle/checker.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,13 @@ def checks(self):
186186
for this_check in vars(type(self)).values()
187187
if hasattr(this_check, '_check_for')
188188
]
189+
# This returns the checks in the order they are
190+
# listed in the file (since py3.6) if their priority is the same
189191
return sorted(all, key=lambda this_check: not this_check._terminal)
190192

193+
# Note - this needs to be listed before other checks
194+
# as f string evalutaion may cause malformed AST Nodes.
195+
# So we need to run this check first and terminate early.
191196
@check_for(Definition, terminal=True)
192197
def check_docstring_fstring(self, definition, docstring):
193198
"""D303: Docstrings may not be f-strings.
@@ -213,14 +218,11 @@ def check_docstring_missing(self, definition, docstring):
213218
with a single underscore.
214219
215220
"""
216-
if _is_fstring(docstring):
217-
return # checked above in check_docstring_fstring
218-
219221
if (
220222
not docstring
221223
and definition.is_public
222224
or docstring
223-
and is_blank(ast.literal_eval(docstring))
225+
and is_blank(docstring, literal_eval=True)
224226
):
225227
codes = {
226228
Module: violations.D100,

src/pydocstyle/utils.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,15 @@
1313
NON_ALPHANUMERIC_STRIP_RE = re.compile(r'[\W_]+')
1414

1515

16-
def is_blank(string: str) -> bool:
16+
def is_blank(string: str, literal_eval: bool = False) -> bool:
1717
"""Return True iff the string contains only whitespaces."""
18+
if literal_eval:
19+
try:
20+
string = ast.literal_eval(string)
21+
except ValueError:
22+
# This happens in case of an fstring
23+
# in which case let's return false
24+
return False
1825
return not string.strip()
1926

2027

src/tests/test_cases/fstrings.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,8 @@ def fstring_with_other_errors(arg=1, missing_arg=2):
4545
This should not raise any other errors since fstrings
4646
are a terminal check.
4747
"""
48+
49+
50+
@D303
51+
def fstring_with_blank_doc_string():
52+
f""" """

0 commit comments

Comments
 (0)