Skip to content

Commit 6ec71ba

Browse files
Respect docstring-min-length in docparams extension (#10104) (#10434)
(cherry picked from commit 7f5e996) Co-authored-by: Berker ŞAL <[email protected]>
1 parent fbde890 commit 6ec71ba

File tree

6 files changed

+62
-3
lines changed

6 files changed

+62
-3
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix false positive for `missing-raises-doc` and `missing-yield-doc` when the method length is less than docstring-min-length.
2+
3+
Refs #10104

pylint/extensions/docparams.py

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -209,9 +209,7 @@ def visit_functiondef(self, node: nodes.FunctionDef) -> None:
209209
return
210210

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

217215
self.check_functiondef_params(node, node_doc)
@@ -281,6 +279,10 @@ def visit_raise(self, node: nodes.Raise) -> None:
281279
if not isinstance(func_node, astroid.FunctionDef):
282280
return
283281

282+
# skip functions smaller than 'docstring-min-length'
283+
if self._is_shorter_than_min_length(node):
284+
return
285+
284286
# skip functions that match the 'no-docstring-rgx' config option
285287
no_docstring_rgx = self.linter.config.no_docstring_rgx
286288
if no_docstring_rgx and re.match(no_docstring_rgx, func_node.name):
@@ -364,6 +366,10 @@ def visit_yield(self, node: nodes.Yield | nodes.YieldFrom) -> None:
364366
if self.linter.config.accept_no_yields_doc:
365367
return
366368

369+
# skip functions smaller than 'docstring-min-length'
370+
if self._is_shorter_than_min_length(node):
371+
return
372+
367373
func_node: astroid.FunctionDef = node.frame()
368374

369375
# skip functions that match the 'no-docstring-rgx' config option
@@ -671,6 +677,22 @@ def _add_raise_message(
671677
confidence=HIGH,
672678
)
673679

680+
def _is_shorter_than_min_length(self, node: nodes.FunctionDef) -> bool:
681+
"""Returns true on functions smaller than 'docstring-min-length'.
682+
683+
:param node: Node for a function or method definition in the AST
684+
:type node: :class:`astroid.scoped_nodes.Function`
685+
686+
:rtype: bool
687+
"""
688+
node_line_count = checker_utils.get_node_last_lineno(node) - node.lineno
689+
min_lines = self.linter.config.docstring_min_length
690+
result = -1 < node_line_count < min_lines
691+
assert isinstance(
692+
result, bool
693+
), "Result of int comparison should have been a boolean"
694+
return result
695+
674696

675697
def register(linter: PyLinter) -> None:
676698
linter.register_checker(DocstringParameterChecker(linter))
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
"""Tests for missing-raises-doc for non-specified style docstrings
2+
with accept-no-raise-doc = no and docstring-min-length = 3
3+
"""
4+
# pylint: disable=invalid-name, broad-exception-raised
5+
6+
# Example of a function that is less than 'docstring-min-length' config option
7+
# No error message is emitted.
8+
def test_skip_docstring_min_length():
9+
"""function is too short and is missing raise documentation"""
10+
raise Exception
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[MAIN]
2+
load-plugins = pylint.extensions.docparams
3+
4+
[BASIC]
5+
accept-no-raise-doc=no
6+
docstring-min-length=3
7+
no-docstring-rgx=^$
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
"""Tests for missing-yield-doc for non-specified style docstrings
2+
with accept-no-yields-doc = no and docstring-min-length = 3
3+
"""
4+
# pylint: disable=invalid-name
5+
6+
# Example of a function that is less than 'docstring-min-length' config option
7+
# No error message is emitted.
8+
def test_skip_docstring_min_length():
9+
"""function is too short and is missing yield documentation"""
10+
yield None
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[MAIN]
2+
load-plugins = pylint.extensions.docparams
3+
4+
[BASIC]
5+
accept-no-yields-doc=no
6+
docstring-min-length=3
7+
no-docstring-rgx=^$

0 commit comments

Comments
 (0)