Skip to content

Commit b5c9b8c

Browse files
committed
Add support for checking missing parameters in Numpy docstrings
Fixes PyCQA#394 This adds support for checking missing arguments in Numpy docstrings.
1 parent f6ddbcb commit b5c9b8c

File tree

2 files changed

+93
-5
lines changed

2 files changed

+93
-5
lines changed

src/pydocstyle/checker.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -676,7 +676,7 @@ def _check_numpy_section(cls, docstring, definition, context):
676676
if suffix:
677677
yield violations.D406(capitalized_section, context.line.strip())
678678

679-
if capitalized_section in ("Parameters"):
679+
if capitalized_section == "Parameters":
680680
yield from cls._check_parameters_section(docstring, definition, context)
681681

682682
@staticmethod
@@ -712,7 +712,7 @@ def _check_parameters_section(docstring, definition, context):
712712
# on the current line.
713713
else:
714714
parameters = current_line.strip()
715-
# Numpy allow grouping of multiple parameters of same
715+
# Numpy allows grouping of multiple parameters of same
716716
# type in the same line. They are comma separated.
717717
parameter_list = parameters.split(",")
718718
for parameter in parameter_list:
@@ -734,6 +734,19 @@ def _check_args_section(docstring, definition, context):
734734
match = ConventionChecker.GOOGLE_ARGS_REGEX.match(line)
735735
if match:
736736
docstring_args.add(match.group(1))
737+
yield from ConventionChecker._check_missing_args(docstring_args, definition)
738+
739+
740+
@staticmethod
741+
def _check_missing_args(docstring_args, definition):
742+
"""D417: Yield error for missing arguments in docstring.
743+
744+
Given a list of arguments found in the docstring and the
745+
callable definition, it checks if all the arguments of the
746+
callable are present in the docstring, else it yields a
747+
D417 with a list of missing arguments.
748+
749+
"""
737750
function_args = get_function_args(definition.source)
738751
# If the method isn't static, then we skip the first
739752
# positional argument as it is `cls` or `self`

src/tests/test_cases/sections.py

Lines changed: 78 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -277,8 +277,8 @@ def missing_colon_google_style_section(): # noqa: D406, D407
277277
@expect(_D213)
278278
@expect("D417: Missing arguments in the docstring "
279279
"(argument(s) 'y' missing in "
280-
"'test_missing_args' docstring)")
281-
def test_missing_args(x=1, y=2): # noqa: D406, D407
280+
"'test_missing_google_args' docstring)")
281+
def test_missing_google_args(x=1, y=2): # noqa: D406, D407
282282
"""Toggle the gizmo.
283283
284284
Args:
@@ -287,7 +287,7 @@ def test_missing_args(x=1, y=2): # noqa: D406, D407
287287
"""
288288

289289

290-
class Test: # noqa: D203
290+
class TestGoogle: # noqa: D203
291291
"""Test class."""
292292

293293
def test_method(self, test, another_test): # noqa: D213, D407
@@ -333,3 +333,78 @@ def test_missing_args_static_method(a, x, y, z=3): # noqa: D213, D407
333333
x: Another parameter.
334334
335335
"""
336+
337+
338+
@expect(_D213)
339+
@expect("D417: Missing arguments in the docstring "
340+
"(argument(s) 'y' missing in "
341+
"'test_missing_numpy_args' docstring)")
342+
def test_missing_numpy_args(x=1, y=2): # noqa: D406, D407
343+
"""Toggle the gizmo.
344+
345+
Parameters
346+
----------
347+
x : int
348+
The greatest integer.
349+
350+
"""
351+
352+
353+
class TestNumpy: # noqa: D203
354+
"""Test class."""
355+
356+
def test_method(self, test, another_test, x=1, y=2): # noqa: D213, D407
357+
"""Test a valid args section.
358+
359+
Parameters
360+
----------
361+
test, another_test
362+
Some parameters without type.
363+
x, y : int
364+
Some integer parameters.
365+
366+
"""
367+
368+
@expect("D417: Missing arguments in the docstring "
369+
"(argument(s) 'test, y, z' missing in "
370+
"'test_missing_args' docstring)", arg_count=4)
371+
def test_missing_args(self, test, x, y, z=3, t=1): # noqa: D213, D407
372+
"""Test a valid args section.
373+
374+
Parameters
375+
----------
376+
x, t : int
377+
Some parameters.
378+
379+
380+
"""
381+
382+
@classmethod
383+
@expect("D417: Missing arguments in the docstring "
384+
"(argument(s) 'test, y, z' missing in "
385+
"'test_missing_args_class_method' docstring)", arg_count=4)
386+
def test_missing_args_class_method(cls, test, x, y, z=3): # noqa: D213, D407
387+
"""Test a valid args section.
388+
389+
Parameters
390+
----------
391+
x
392+
Another parameter.
393+
394+
"""
395+
396+
@staticmethod
397+
@expect("D417: Missing arguments in the docstring "
398+
"(argument(s) 'a, z' missing in "
399+
"'test_missing_args_static_method' docstring)", arg_count=3)
400+
def test_missing_args_static_method(a, x, y, z=3, t=1): # noqa: D213, D407
401+
"""Test a valid args section.
402+
403+
Parameters
404+
----------
405+
x, y
406+
Another parameter.
407+
t : int
408+
Yet another parameter.
409+
410+
"""

0 commit comments

Comments
 (0)