summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniƫl van Noord <13665637+DanielNoord@users.noreply.github.com>2021-10-17 10:40:51 +0200
committerGitHub <noreply@github.com>2021-10-17 10:40:51 +0200
commitd3b27d8aeeb27441405d4183feb7b18a91925093 (patch)
tree56b4e256bc0f3acb8c5a8cfeae68166dfe1d1fe2
parent97048ded8e7528e927f7f7818beb3d5d79662b03 (diff)
downloadpylint-git-d3b27d8aeeb27441405d4183feb7b18a91925093.tar.gz
Fix ``missing-function-docstring`` not checking ``__init__`` (#5147)
* Fix ``missing-function-docstring`` not checking ``__init__`` * Ignore ``object``
-rw-r--r--ChangeLog3
-rw-r--r--pylint/checkers/base.py2
-rw-r--r--pylintrc2
-rw-r--r--tests/checkers/unittest_base.py46
4 files changed, 52 insertions, 1 deletions
diff --git a/ChangeLog b/ChangeLog
index 333dd722c..f9eba4227 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -65,6 +65,9 @@ Release date: TBA
Closes #3031
+* Fix ``missing-function-docstring`` not being able to check ``__init__`` and other
+ magic methods even if the ``no-docstring-rgx`` setting was set to do so
+
What's New in Pylint 2.11.2?
============================
diff --git a/pylint/checkers/base.py b/pylint/checkers/base.py
index 5507bde74..699f2dae1 100644
--- a/pylint/checkers/base.py
+++ b/pylint/checkers/base.py
@@ -2192,6 +2192,8 @@ class DocStringChecker(_BasicChecker):
)
# check if node is from a method overridden by its ancestor
for ancestor in node.parent.frame().ancestors():
+ if ancestor.qname() == "builtins.object":
+ continue
if node.name in ancestor and isinstance(
ancestor[node.name], nodes.FunctionDef
):
diff --git a/pylintrc b/pylintrc
index 376fb828e..b4a4e9fac 100644
--- a/pylintrc
+++ b/pylintrc
@@ -257,7 +257,7 @@ method-rgx=[a-z_][a-z0-9_]{2,}$
method-name-hint=[a-z_][a-z0-9_]{2,}$
# Regular expression which should only match function or class names that do
-# not require a docstring.
+# not require a docstring. Use ^(?!__init__$)_ to also check __init__.
no-docstring-rgx=__.*__
# Minimum line length for functions/classes that require docstrings, shorter
diff --git a/tests/checkers/unittest_base.py b/tests/checkers/unittest_base.py
index 48b48a450..616a0a8e7 100644
--- a/tests/checkers/unittest_base.py
+++ b/tests/checkers/unittest_base.py
@@ -58,6 +58,21 @@ class TestDocstring(CheckerTestCase):
with self.assertAddsMessages(message):
self.checker.visit_module(module)
+ @set_config(no_docstring_rgx=re.compile("^(?!__init__$)_"))
+ def test_empty_docstring_on_init(self) -> None:
+ node = astroid.extract_node(
+ """
+ #pylint disable=missing-module-docstring, too-few-public-methods,
+ class MyClass:
+ def __init__(self, my_param: int) -> None:
+ '''
+ '''
+ """
+ )
+ message = MessageTest("empty-docstring", node=node.body[0], args=("method",))
+ with self.assertAddsMessages(message):
+ self.checker.visit_functiondef(node.body[0])
+
def test_empty_docstring_function(self) -> None:
func = astroid.extract_node(
"""
@@ -116,6 +131,37 @@ class TestDocstring(CheckerTestCase):
with self.assertNoMessages():
self.checker.visit_functiondef(func)
+ @set_config(no_docstring_rgx=re.compile("^(?!__init__$)_"))
+ def test_missing_docstring_on_init(self) -> None:
+ node = astroid.extract_node(
+ """
+ #pylint disable=missing-module-docstring, too-few-public-methods,
+ class MyClass:
+ def __init__(self, my_param: int) -> None:
+ pass
+ """
+ )
+ message = MessageTest("missing-function-docstring", node=node.body[0])
+ with self.assertAddsMessages(message):
+ self.checker.visit_functiondef(node.body[0])
+
+ @set_config(no_docstring_rgx=re.compile("^(?!__eq__$)_"))
+ def test_missing_docstring_on_inherited_magic(self) -> None:
+ module = astroid.parse(
+ """
+ #pylint disable=missing-module-docstring, too-few-public-methods,
+ class MyClass:
+ pass
+
+ class Child(MyClass):
+ def __eq__(self, other):
+ return True
+ """
+ )
+ message = MessageTest("missing-function-docstring", node=module.body[1].body[0])
+ with self.assertAddsMessages(message):
+ self.checker.visit_functiondef(module.body[1].body[0])
+
def test_class_no_docstring(self) -> None:
klass = astroid.extract_node(
"""