Skip to content

Commit

Permalink
Fix useless-type-doc false positive when args exist in the docstring
Browse files Browse the repository at this point in the history
  • Loading branch information
Pierre-Sassoulas committed Jun 25, 2021
1 parent f7396e1 commit e2c6b1d
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 3 deletions.
4 changes: 4 additions & 0 deletions pylint/extensions/_check_docs_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from typing import List

import astroid
from astroid import AssignName

from pylint.checkers import utils

Expand Down Expand Up @@ -206,6 +207,9 @@ def __init__(self, doc):
def __repr__(self) -> str:
return f"<{self.__class__.__name__}:'''{self.doc}'''>"

def arg_is_documented(self, arg_name: AssignName) -> bool:
return arg_name.name in self.doc

def is_valid(self):
return False

Expand Down
14 changes: 11 additions & 3 deletions pylint/extensions/docparams.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@
"""Pylint plugin for checking in Sphinx, Google, or Numpy style docstrings
"""
import re
from typing import Optional

import astroid

from pylint.checkers import BaseChecker
from pylint.checkers import utils as checker_utils
from pylint.extensions import _check_docs_utils as utils
from pylint.extensions._check_docs_utils import Docstring
from pylint.interfaces import IAstroidChecker
from pylint.utils import get_global_option

Expand Down Expand Up @@ -460,7 +462,11 @@ def _compare_ignored_args(
)

def check_arguments_in_docstring(
self, doc, arguments_node, warning_node, accept_no_param_doc=None
self,
doc: Docstring,
arguments_node: astroid.Arguments,
warning_node: astroid.NodeNG,
accept_no_param_doc: Optional[bool] = None,
):
"""Check that all parameters in a function, method or class constructor
on the one hand and the parameters mentioned in the parameter
Expand Down Expand Up @@ -541,10 +547,12 @@ class constructor.
)

for index, arg_name in enumerate(arguments_node.args):
if arguments_node.annotations[index]:
if arguments_node.annotations[index] and doc.arg_is_documented(arg_name):
params_with_type.add(arg_name.name)
for index, arg_name in enumerate(arguments_node.kwonlyargs):
if arguments_node.kwonlyargs_annotations[index]:
if arguments_node.kwonlyargs_annotations[index] and doc.arg_is_documented(
arg_name
):
params_with_type.add(arg_name.name)

if not tolerate_missing_params:
Expand Down
51 changes: 51 additions & 0 deletions tests/functional/u/useless/useless_type_doc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
"""demonstrate FP with useless-type-doc"""
# line-too-long


def function(public_param: int, _some_private_param: bool = False) -> None:
"""does things
Args:
public_param: an ordinary parameter
"""
for _ in range(public_param):
...
if _some_private_param:
...
else:
...


# +1: [useless-type-doc,useless-param-doc]
def function_useless_doc(public_param: int, _some_private_param: bool = False) -> None:
"""does things
Args:
public_param: an ordinary parameter
_some_private_param (bool): private param
"""
for _ in range(public_param):
...
if _some_private_param:
...
else:
...


def test(_new: str) -> str: # We don't expect useless-type-doc here
"""foobar
:return: comment
"""
return ""


# +1: [useless-type-doc,useless-param-doc]
def test_two(_new: str) -> str: # We don't expect useless-type-doc here
"""foobar
:param str _new:
:return: comment
"""
return ""
8 changes: 8 additions & 0 deletions tests/functional/u/useless/useless_type_doc.rc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[MASTER]
load-plugins=pylint.extensions.docparams,

[PARAMETER_DOCUMENTATION]
accept-no-param-doc=no
accept-no-raise-doc=no
accept-no-return-doc=no
accept-no-yields-doc=no

0 comments on commit e2c6b1d

Please sign in to comment.