-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ignored-parents option to design checker (#4758)
* Add ignored-parents option to design checker This allows users to specify classes to ignore while counting parent classes. Partially closes #3057 Co-authored-by: Pierre Sassoulas <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
- Loading branch information
1 parent
67f4056
commit 24d03e9
Showing
9 changed files
with
139 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# Copyright (c) 2021 Rebecca Turner <[email protected]> | ||
|
||
# Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html | ||
# For details: https://github.com/PyCQA/pylint/blob/main/LICENSE | ||
|
||
|
||
import astroid | ||
|
||
from pylint.checkers import design_analysis | ||
from pylint.testutils import CheckerTestCase, set_config | ||
|
||
|
||
class TestDesignChecker(CheckerTestCase): | ||
|
||
CHECKER_CLASS = design_analysis.MisdesignChecker | ||
|
||
@set_config( | ||
ignored_parents=(".Dddd",), | ||
max_parents=1, | ||
) | ||
def test_too_many_ancestors_ignored_parents_are_skipped(self): | ||
"""Make sure that classes listed in ``ignored-parents`` aren't counted | ||
by the too-many-ancestors message. | ||
""" | ||
|
||
node = astroid.extract_node( | ||
""" | ||
class Aaaa(object): | ||
pass | ||
class Bbbb(Aaaa): | ||
pass | ||
class Cccc(Bbbb): | ||
pass | ||
class Dddd(Cccc): | ||
pass | ||
class Eeee(Dddd): | ||
pass | ||
""" | ||
) | ||
with self.assertNoMessages(): | ||
self.checker.visit_classdef(node) |
40 changes: 40 additions & 0 deletions
40
tests/functional/t/too/too_many_ancestors_ignored_parents.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# pylint: disable=missing-docstring, too-few-public-methods, invalid-name | ||
|
||
# Inheritance diagram: | ||
# F | ||
# / | ||
# D E | ||
# \/ | ||
# B C | ||
# \/ | ||
# A | ||
# | ||
# Once `E` is pruned from the tree, we have: | ||
# D | ||
# \ | ||
# B C | ||
# \/ | ||
# A | ||
# | ||
# By setting `max-parents=2`, we're able to check that tree-pruning works | ||
# correctly; in the new diagram, `B` has only 1 parent, so it doesn't raise a | ||
# message, and `A` has 3, so it does raise a message with the specific number | ||
# of parents. | ||
|
||
class F: | ||
"""0 parents""" | ||
|
||
class E(F): | ||
"""1 parent""" | ||
|
||
class D: | ||
"""0 parents""" | ||
|
||
class B(D, E): | ||
"""3 parents""" | ||
|
||
class C: | ||
"""0 parents""" | ||
|
||
class A(B, C): # [too-many-ancestors] | ||
"""5 parents""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[testoptions] | ||
max-parents=2 | ||
ignored-parents=functional.t.too.too_many_ancestors_ignored_parents.E |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
too-many-ancestors:39:0:A:Too many ancestors (3/2) |