Skip to content

Commit 1e1fe19

Browse files
ethan-lebaPierre-Sassoulas
authored andcommitted
Move len-checker helpers to utils file
1 parent e602fda commit 1e1fe19

File tree

3 files changed

+26
-29
lines changed

3 files changed

+26
-29
lines changed

pylint/checkers/refactoring/len_checker.py

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,12 @@
22

33
# Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
44
# For details: https://github.com/PyCQA/pylint/blob/master/COPYING
5-
from typing import Optional
6-
75
import astroid
86

97
from pylint import checkers, interfaces
108
from pylint.checkers import utils
119

1210

13-
def _is_call_of_name(node: astroid.node_classes.NodeNG, name: str) -> bool:
14-
"""Checks if node is a function call with the given name"""
15-
return (
16-
isinstance(node, astroid.Call)
17-
and isinstance(node.func, astroid.Name)
18-
and node.func.name == name
19-
)
20-
21-
22-
def _is_test_condition(
23-
node: astroid.node_classes.NodeNG,
24-
parent: Optional[astroid.node_classes.NodeNG] = None,
25-
) -> bool:
26-
"""Returns true if the given node is being tested for truthiness"""
27-
parent = parent or node.parent
28-
if isinstance(parent, (astroid.While, astroid.If, astroid.IfExp, astroid.Assert)):
29-
return node is parent.test or parent.test.parent_of(node)
30-
if isinstance(parent, astroid.Comprehension):
31-
return node in parent.ifs
32-
return _is_call_of_name(parent, "bool") and parent.parent_of(node)
33-
34-
3511
class LenChecker(checkers.BaseChecker):
3612
"""Checks for incorrect usage of len() inside conditions.
3713
Pep8 states:
@@ -79,7 +55,7 @@ def visit_call(self, node):
7955
# a len(S) call is used inside a test condition
8056
# could be if, while, assert or if expression statement
8157
# e.g. `if len(S):`
82-
if _is_call_of_name(node, "len"):
58+
if utils.is_call_of_name(node, "len"):
8359
# the len() call could also be nested together with other
8460
# boolean operations, e.g. `if z or len(x):`
8561
parent = node.parent
@@ -88,7 +64,7 @@ def visit_call(self, node):
8864

8965
# we're finally out of any nested boolean operations so check if
9066
# this len() call is part of a test condition
91-
if _is_test_condition(node, parent):
67+
if utils.is_test_condition(node, parent):
9268
self.add_message("len-as-condition", node=node)
9369

9470
@utils.check_messages("len-as-condition")
@@ -99,6 +75,6 @@ def visit_unaryop(self, node):
9975
if (
10076
isinstance(node, astroid.UnaryOp)
10177
and node.op == "not"
102-
and _is_call_of_name(node.operand, "len")
78+
and utils.is_call_of_name(node.operand, "len")
10379
):
10480
self.add_message("len-as-condition", node=node)

pylint/checkers/refactoring/refactoring_checker.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
from pylint import checkers, interfaces
1717
from pylint import utils as lint_utils
1818
from pylint.checkers import utils
19-
from pylint.checkers.refactoring.len_checker import _is_test_condition
2019
from pylint.checkers.utils import node_frame_class
2120

2221
KNOWN_INFINITE_ITERATORS = {"itertools.count"}
@@ -1003,7 +1002,7 @@ def _check_simplifiable_condition(self, node):
10031002
10041003
Variables will not be simplified, even in the value can be inferred,
10051004
and expressions like '3 + 4' will remain expanded."""
1006-
if not _is_test_condition(node):
1005+
if not utils.is_test_condition(node):
10071006
return
10081007

10091008
self._can_simplify_bool_op = False

pylint/checkers/utils.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1314,3 +1314,25 @@ def is_protocol_class(cls: astroid.node_classes.NodeNG) -> bool:
13141314
# Use .ancestors() since not all protocol classes can have
13151315
# their mro deduced.
13161316
return any(parent.qname() in TYPING_PROTOCOLS for parent in cls.ancestors())
1317+
1318+
1319+
def is_call_of_name(node: astroid.node_classes.NodeNG, name: str) -> bool:
1320+
"""Checks if node is a function call with the given name"""
1321+
return (
1322+
isinstance(node, astroid.Call)
1323+
and isinstance(node.func, astroid.Name)
1324+
and node.func.name == name
1325+
)
1326+
1327+
1328+
def is_test_condition(
1329+
node: astroid.node_classes.NodeNG,
1330+
parent: Optional[astroid.node_classes.NodeNG] = None,
1331+
) -> bool:
1332+
"""Returns true if the given node is being tested for truthiness"""
1333+
parent = parent or node.parent
1334+
if isinstance(parent, (astroid.While, astroid.If, astroid.IfExp, astroid.Assert)):
1335+
return node is parent.test or parent.test.parent_of(node)
1336+
if isinstance(parent, astroid.Comprehension):
1337+
return node in parent.ifs
1338+
return is_call_of_name(parent, "bool") and parent.parent_of(node)

0 commit comments

Comments
 (0)