Skip to content

Add configuration option exclude-too-few-public-methods #5191

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

Merged
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
2 changes: 2 additions & 0 deletions CONTRIBUTORS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -562,3 +562,5 @@ contributors:
* Youngsoo Sung: contributor

* Arianna Yang: contributor

* Mike Fiedler (miketheman): contributor
5 changes: 5 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ Release date: TBA
* 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

* Added configuration option ``exclude-too-few-public-methods`` to allow excluding
classes from the ``min-public-methods`` checker.

Closes #3370


What's New in Pylint 2.11.2?
============================
Expand Down
5 changes: 5 additions & 0 deletions doc/whatsnew/2.12.rst
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ New checkers

Closes #4774

* Added configuration option ``exclude-too-few-public-methods`` to allow excluding
classes from the ``min-public-methods`` checker.

Closes #3370


Removed checkers
================
Expand Down
22 changes: 22 additions & 0 deletions pylint/checkers/design_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,16 @@ class MisdesignChecker(BaseChecker):
"statement (see R0916).",
},
),
(
"exclude-too-few-public-methods",
{
"default": [],
"type": "regexp_csv",
"metavar": "<pattern>[,<pattern>...]",
"help": "List of regular expressions of class ancestor names "
"to ignore when counting public methods (see R0903)",
},
),
)

def __init__(self, linter=None):
Expand All @@ -416,6 +426,9 @@ def open(self):
self._returns = []
self._branches = defaultdict(int)
self._stmts = []
self._exclude_too_few_public_methods = utils.get_global_option(
self, "exclude-too-few-public-methods", default=[]
)

def _inc_all_stmts(self, amount):
for i, _ in enumerate(self._stmts):
Expand Down Expand Up @@ -472,6 +485,15 @@ def leave_classdef(self, node: nodes.ClassDef) -> None:
args=(my_methods, self.config.max_public_methods),
)

# Stop here if the class is excluded via configuration.
if node.type == "class" and self._exclude_too_few_public_methods:
for ancestor in node.ancestors():
if any(
pattern.match(ancestor.qname())
for pattern in self._exclude_too_few_public_methods
):
return

# Stop here for exception, metaclass, interface classes and other
# classes for which we don't need to count the methods.
if node.type != "class" or _is_exempt_from_public_methods(node):
Expand Down
2 changes: 1 addition & 1 deletion pylint/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
"ignored-argument-names",
"mixin-class-rgx",
]
GLOBAL_OPTION_PATTERN_LIST = Literal["ignore-paths"]
GLOBAL_OPTION_PATTERN_LIST = Literal["exclude-too-few-public-methods", "ignore-paths"]
GLOBAL_OPTION_TUPLE_INT = Literal["py-version"]
GLOBAL_OPTION_NAMES = Union[
GLOBAL_OPTION_BOOL,
Expand Down
3 changes: 3 additions & 0 deletions pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,9 @@ min-public-methods=2
# Maximum number of public methods for a class (see R0904).
max-public-methods=25

# List of regular expressions of class ancestor names to
# ignore when counting public methods (see R0903).
exclude-too-few-public-methods=

[CLASSES]

Expand Down
18 changes: 18 additions & 0 deletions tests/checkers/unittest_design.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

from pylint.checkers import design_analysis
from pylint.testutils import CheckerTestCase, set_config
from pylint.utils.utils import get_global_option


class TestDesignChecker(CheckerTestCase):
Expand Down Expand Up @@ -42,3 +43,20 @@ class Eeee(Dddd):
)
with self.assertNoMessages():
self.checker.visit_classdef(node)

@set_config(exclude_too_few_public_methods="toml.*")
def test_exclude_too_few_methods_with_value(self) -> None:
"""Test exclude-too-few-public-methods option with value"""
options = get_global_option(self.checker, "exclude-too-few-public-methods")

assert any(i.match("toml") for i in options)
assert any(i.match("toml.*") for i in options)
assert any(i.match("toml.TomlEncoder") for i in options)

def test_ignore_paths_with_no_value(self) -> None:
"""Test exclude-too-few-public-methods option with no value.
Compare against actual list to see if validator works."""
options = get_global_option(self.checker, "exclude-too-few-public-methods")

# pylint: disable-next=use-implicit-booleaness-not-comparison
assert options == []
14 changes: 14 additions & 0 deletions tests/functional/t/too/too_few_public_methods_excluded.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# pylint: disable=missing-docstring
from json import JSONEncoder

class Control: # [too-few-public-methods]
...


class MyJsonEncoder(JSONEncoder):
...

class InheritedInModule(Control):
"""This class inherits from a class that doesn't have enough mehods,
and its parent is excluded via config, so it doesn't raise."""
...
4 changes: 4 additions & 0 deletions tests/functional/t/too/too_few_public_methods_excluded.rc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[testoptions]
min-public-methods=10 # to combat inherited methods

exclude-too-few-public-methods=json.*,^.*Control$
1 change: 1 addition & 0 deletions tests/functional/t/too/too_few_public_methods_excluded.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
too-few-public-methods:4:0:Control:Too few public methods (0/10):HIGH