Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Respect docstring-min-length in docparams extension #10104

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions doc/whatsnew/fragments/10104.false_negative
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix false negative for `missing-raises-doc`, `missing-return-doc` and `missing-yield-doc` when the method length is less than docstring-min-length.

Refs #10104
28 changes: 25 additions & 3 deletions pylint/extensions/docparams.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,9 +209,7 @@
return

# skip functions smaller than 'docstring-min-length'
lines = checker_utils.get_node_last_lineno(node) - node.lineno
max_lines = self.linter.config.docstring_min_length
if max_lines > -1 and lines < max_lines:
if self._is_shorter_than_min_length(node):
return

self.check_functiondef_params(node, node_doc)
Expand Down Expand Up @@ -281,6 +279,10 @@
if not isinstance(func_node, astroid.FunctionDef):
return

# skip functions smaller than 'docstring-min-length'
if self._is_shorter_than_min_length(node):
return

# skip functions that match the 'no-docstring-rgx' config option
no_docstring_rgx = self.linter.config.no_docstring_rgx
if no_docstring_rgx and re.match(no_docstring_rgx, func_node.name):
Expand Down Expand Up @@ -338,6 +340,10 @@
if self.linter.config.accept_no_return_doc:
return

# skip functions smaller than 'docstring-min-length'
if self._is_shorter_than_min_length(node):
return

Check warning on line 345 in pylint/extensions/docparams.py

View check run for this annotation

Codecov / codecov/patch

pylint/extensions/docparams.py#L345

Added line #L345 was not covered by tests
Comment on lines +343 to +345
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure it's possible to cover this, do return nodes ever have a docstring attached ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if I understand the problem 😅

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those two lines are not covered by tests, and it's in a visit_return function, so I suppose it's not covered because it's impossible to ever reach this code (return node don't have docstrings, right ?)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what return node is but there is a part in docstrings that documents what the function is returning

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Return node would be obtained from something like return 4, once parsed you'll get an ast's Return node:

Parsing:

print(ast.dump(ast.parse('return 4')))

Resut:

Module(body=[Return(value=Constant(value=4))])

(Which then become an astroid return node in pylint, before we use the visitor pattern to do something with it)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume the extension is deciding the return part of the docstring from -> int part of the function definition. Then it wouldn't care about the return node

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, would it be possible to create a functional test for it then ? (I.e. with a docstring too small and return type information that we don't need to analyse because the docstring is too small if I understood correctly)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


func_node: astroid.FunctionDef = node.frame()

# skip functions that match the 'no-docstring-rgx' config option
Expand All @@ -364,6 +370,10 @@
if self.linter.config.accept_no_yields_doc:
return

# skip functions smaller than 'docstring-min-length'
if self._is_shorter_than_min_length(node):
return

func_node: astroid.FunctionDef = node.frame()

# skip functions that match the 'no-docstring-rgx' config option
Expand Down Expand Up @@ -671,6 +681,18 @@
confidence=HIGH,
)

def _is_shorter_than_min_length(self, node: nodes.FunctionDef) -> bool:
"""Returns true on functions smaller than 'docstring-min-length'.

:param node: Node for a function or method definition in the AST
:type node: :class:`astroid.scoped_nodes.Function`

:rtype: bool
"""
lines = checker_utils.get_node_last_lineno(node) - node.lineno
min_lines = self.linter.config.docstring_min_length
return bool(min_lines > -1) and bool(lines < min_lines)


def register(linter: PyLinter) -> None:
linter.register_checker(DocstringParameterChecker(linter))
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
"""Tests for missing-raises-doc for non-specified style docstrings
with accept-no-raise-doc = no and docstring-min-length = 3
"""
# pylint: disable=invalid-name, broad-exception-raised

# Example of a function that is less than 'docstring-min-length' config option
# No error message is emitted.
def test_skip_docstring_min_length():
"""function is too short and is missing raise documentation"""
raise Exception
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[MAIN]
load-plugins = pylint.extensions.docparams

[BASIC]
accept-no-raise-doc=no
docstring-min-length=3
no-docstring-rgx=^$
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
"""Tests for missing-return-doc for non-specified style docstrings
with accept-no-return-doc = no and docstring-min-length = 3
"""
# pylint: disable=invalid-name

# Example of a function that is less than 'docstring-min-length' config option
# No error message is emitted.
def test_skip_docstring_min_length() -> None:
"""function is too short and is missing return documentation"""
return None
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[MAIN]
load-plugins = pylint.extensions.docparams

[BASIC]
accept-no-return-doc=no
docstring-min-length=3
no-docstring-rgx=^$
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
"""Tests for missing-yield-doc for non-specified style docstrings
with accept-no-yields-doc = no and docstring-min-length = 3
"""
# pylint: disable=invalid-name

# Example of a function that is less than 'docstring-min-length' config option
# No error message is emitted.
def test_skip_docstring_min_length():
"""function is too short and is missing yield documentation"""
yield None
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[MAIN]
load-plugins = pylint.extensions.docparams

[BASIC]
accept-no-yields-doc=no
docstring-min-length=3
no-docstring-rgx=^$
Loading