summaryrefslogtreecommitdiff
path: root/pylint
diff options
context:
space:
mode:
authorArianna Y <92831762+areveny@users.noreply.github.com>2021-10-25 02:09:43 -0500
committerGitHub <noreply@github.com>2021-10-25 09:09:43 +0200
commited3449fee063d91f050c6b733030d3b3d7ad719f (patch)
tree3d5d640e824a1e3c9efc99b5281ce17ac3c4d575 /pylint
parentbfc8d32273102495ee1cfd8daa14d9b6693b11f7 (diff)
downloadpylint-git-ed3449fee063d91f050c6b733030d3b3d7ad719f.tar.gz
Add extension checker that suggests any/all statements from for loops (#5196)
* Add extension checker that suggests any/all statements from for loops * Suggest all/not all: If there are negated conditions, we can suggest an all/not all statement to move the 'not' out of the comprehension and call it only once. Co-authored-by: Pierre Sassoulas <pierre.sassoulas@gmail.com>
Diffstat (limited to 'pylint')
-rw-r--r--pylint/checkers/utils.py9
-rw-r--r--pylint/extensions/for_any_all.py73
2 files changed, 82 insertions, 0 deletions
diff --git a/pylint/checkers/utils.py b/pylint/checkers/utils.py
index 43157d0f9..1d198e87e 100644
--- a/pylint/checkers/utils.py
+++ b/pylint/checkers/utils.py
@@ -1638,3 +1638,12 @@ def is_empty_str_literal(node: Optional[nodes.NodeNG]) -> bool:
return (
isinstance(node, nodes.Const) and isinstance(node.value, str) and not node.value
)
+
+
+def returns_bool(node: nodes.NodeNG) -> bool:
+ """Returns true if a node is a return that returns a constant boolean"""
+ return (
+ isinstance(node, nodes.Return)
+ and isinstance(node.value, nodes.Const)
+ and node.value.value in (True, False)
+ )
diff --git a/pylint/extensions/for_any_all.py b/pylint/extensions/for_any_all.py
new file mode 100644
index 000000000..9c7274095
--- /dev/null
+++ b/pylint/extensions/for_any_all.py
@@ -0,0 +1,73 @@
+"""Check for use of for loops that only check for a condition."""
+from typing import TYPE_CHECKING
+
+from astroid import nodes
+
+from pylint.checkers import BaseChecker
+from pylint.checkers.utils import check_messages, returns_bool
+from pylint.interfaces import IAstroidChecker
+
+if TYPE_CHECKING:
+ from pylint.lint.pylinter import PyLinter
+
+
+class ConsiderUsingAnyOrAllChecker(BaseChecker):
+
+ __implements__ = (IAstroidChecker,)
+ name = "consider-using-any-or-all"
+ msgs = {
+ "C0501": (
+ "`for` loop could be '%s'",
+ "consider-using-any-or-all",
+ "A for loop that checks for a condition and return a bool can be replaced with any or all.",
+ )
+ }
+
+ @check_messages("consider-using-any-or-all")
+ def visit_for(self, node: nodes.For) -> None:
+ if len(node.body) != 1: # Only If node with no Else
+ return
+ if not isinstance(node.body[0], nodes.If):
+ return
+
+ if_children = list(node.body[0].get_children())
+ if not len(if_children) == 2: # The If node has only a comparison and return
+ return
+ if not returns_bool(if_children[1]):
+ return
+
+ # Check for terminating boolean return right after the loop
+ node_after_loop = node.next_sibling()
+ if returns_bool(node_after_loop):
+ final_return_bool = node_after_loop.value.value
+ suggested_string = self._build_suggested_string(node, final_return_bool)
+ self.add_message(
+ "consider-using-any-or-all", node=node, args=suggested_string
+ )
+
+ @staticmethod
+ def _build_suggested_string(node: nodes.For, final_return_bool: bool) -> str:
+ """When a nodes.For node can be rewritten as an any/all statement, return a suggestion for that statement
+ final_return_bool is the boolean literal returned after the for loop if all conditions fail
+ """
+ loop_var = node.target.as_string()
+ loop_iter = node.iter.as_string()
+ test_node = next(node.body[0].get_children())
+
+ if isinstance(test_node, nodes.UnaryOp) and test_node.op == "not":
+ # The condition is negated. Advance the node to the operand and modify the suggestion
+ test_node = test_node.operand
+ suggested_function = "all" if final_return_bool else "not all"
+ else:
+ suggested_function = "not any" if final_return_bool else "any"
+
+ test = test_node.as_string()
+ return f"{suggested_function}({test} for {loop_var} in {loop_iter})"
+
+
+def register(linter: "PyLinter") -> None:
+ """Required method to auto register this checker.
+
+ :param linter: Main interface object for Pylint plugins
+ """
+ linter.register_checker(ConsiderUsingAnyOrAllChecker(linter))