for_any_all.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. # Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  2. # For details: https://github.com/pylint-dev/pylint/blob/main/LICENSE
  3. # Copyright (c) https://github.com/pylint-dev/pylint/blob/main/CONTRIBUTORS.txt
  4. """Check for use of for loops that only check for a condition."""
  5. from __future__ import annotations
  6. from typing import TYPE_CHECKING
  7. from astroid import nodes
  8. from pylint.checkers import BaseChecker
  9. from pylint.checkers.utils import (
  10. assigned_bool,
  11. only_required_for_messages,
  12. returns_bool,
  13. )
  14. from pylint.interfaces import HIGH
  15. if TYPE_CHECKING:
  16. from pylint.lint.pylinter import PyLinter
  17. class ConsiderUsingAnyOrAllChecker(BaseChecker):
  18. name = "consider-using-any-or-all"
  19. msgs = {
  20. "C0501": (
  21. "`for` loop could be `%s`",
  22. "consider-using-any-or-all",
  23. "A for loop that checks for a condition and return a bool can be replaced with any or all.",
  24. )
  25. }
  26. @only_required_for_messages("consider-using-any-or-all")
  27. def visit_for(self, node: nodes.For) -> None:
  28. if len(node.body) != 1: # Only If node with no Else
  29. return
  30. if not isinstance(node.body[0], nodes.If):
  31. return
  32. if_children = list(node.body[0].get_children())
  33. if any(isinstance(child, nodes.If) for child in if_children):
  34. # an if node within the if-children indicates an elif clause,
  35. # suggesting complex logic.
  36. return
  37. node_after_loop = node.next_sibling()
  38. if self._assigned_reassigned_returned(node, if_children, node_after_loop):
  39. final_return_bool = node_after_loop.value.name
  40. suggested_string = self._build_suggested_string(node, final_return_bool)
  41. self.add_message(
  42. "consider-using-any-or-all",
  43. node=node,
  44. args=suggested_string,
  45. confidence=HIGH,
  46. )
  47. return
  48. if self._if_statement_returns_bool(if_children, node_after_loop):
  49. final_return_bool = node_after_loop.value.value
  50. suggested_string = self._build_suggested_string(node, final_return_bool)
  51. self.add_message(
  52. "consider-using-any-or-all",
  53. node=node,
  54. args=suggested_string,
  55. confidence=HIGH,
  56. )
  57. return
  58. @staticmethod
  59. def _if_statement_returns_bool(
  60. if_children: list[nodes.NodeNG], node_after_loop: nodes.NodeNG
  61. ) -> bool:
  62. """Detect for-loop, if-statement, return pattern:
  63. Ex:
  64. def any_uneven(items):
  65. for item in items:
  66. if not item % 2 == 0:
  67. return True
  68. return False
  69. """
  70. if not len(if_children) == 2:
  71. # The If node has only a comparison and return
  72. return False
  73. if not returns_bool(if_children[1]):
  74. return False
  75. # Check for terminating boolean return right after the loop
  76. return returns_bool(node_after_loop)
  77. @staticmethod
  78. def _assigned_reassigned_returned(
  79. node: nodes.For, if_children: list[nodes.NodeNG], node_after_loop: nodes.NodeNG
  80. ) -> bool:
  81. """Detect boolean-assign, for-loop, re-assign, return pattern:
  82. Ex:
  83. def check_lines(lines, max_chars):
  84. long_line = False
  85. for line in lines:
  86. if len(line) > max_chars:
  87. long_line = True
  88. # no elif / else statement
  89. return long_line
  90. """
  91. node_before_loop = node.previous_sibling()
  92. if not assigned_bool(node_before_loop):
  93. # node before loop isn't assigning to boolean
  94. return False
  95. assign_children = [x for x in if_children if isinstance(x, nodes.Assign)]
  96. if not assign_children:
  97. # if-nodes inside loop aren't assignments
  98. return False
  99. # We only care for the first assign node of the if-children. Otherwise it breaks the pattern.
  100. first_target = assign_children[0].targets[0]
  101. target_before_loop = node_before_loop.targets[0]
  102. if not (
  103. isinstance(first_target, nodes.AssignName)
  104. and isinstance(target_before_loop, nodes.AssignName)
  105. ):
  106. return False
  107. node_before_loop_name = node_before_loop.targets[0].name
  108. return (
  109. first_target.name == node_before_loop_name
  110. and isinstance(node_after_loop, nodes.Return)
  111. and isinstance(node_after_loop.value, nodes.Name)
  112. and node_after_loop.value.name == node_before_loop_name
  113. )
  114. @staticmethod
  115. def _build_suggested_string(node: nodes.For, final_return_bool: bool) -> str:
  116. """When a nodes.For node can be rewritten as an any/all statement, return a
  117. suggestion for that statement.
  118. 'final_return_bool' is the boolean literal returned after the for loop if all
  119. conditions fail.
  120. """
  121. loop_var = node.target.as_string()
  122. loop_iter = node.iter.as_string()
  123. test_node = next(node.body[0].get_children())
  124. if isinstance(test_node, nodes.UnaryOp) and test_node.op == "not":
  125. # The condition is negated. Advance the node to the operand and modify the suggestion
  126. test_node = test_node.operand
  127. suggested_function = "all" if final_return_bool else "not all"
  128. else:
  129. suggested_function = "not any" if final_return_bool else "any"
  130. test = test_node.as_string()
  131. return f"{suggested_function}({test} for {loop_var} in {loop_iter})"
  132. def register(linter: PyLinter) -> None:
  133. linter.register_checker(ConsiderUsingAnyOrAllChecker(linter))