Method: pylint.extensions.comparetozero.CompareToZeroChecker.visit_compare
Calls: 14, Exceptions: 0, Paths: 5Back
Path 1: 8 calls (0.57)
Compare (8)
1@utils.only_required_for_messages("compare-to-zero")
2 def visit_compare(self, node: nodes.Compare) -> None:
3 # pylint: disable=duplicate-code
4 _operators = ["!=", "==", "is not", "is"]
5 # note: astroid.Compare has the left most operand in node.left
6 # while the rest are a list of tuples in node.ops
7 # the format of the tuple is ('compare operator sign', node)
8 # here we squash everything into `ops` to make it easier for processing later
9 ops: list[tuple[str, nodes.NodeNG]] = [("", node.left)]
10 ops.extend(node.ops)
11 iter_ops = iter(ops)
12 all_ops = list(itertools.chain(*iter_ops))
13
14 for ops_idx in range(len(all_ops) - 2):
15 op_1 = all_ops[ops_idx]
16 op_2 = all_ops[ops_idx + 1]
17 op_3 = all_ops[ops_idx + 2]
18 error_detected = False
19
20 # 0 ?? X
21 if _is_constant_zero(op_1) and op_2 in _operators:
22 error_detected = True
23 op = op_3
24 # X ?? 0
25 elif op_2 in _operators and _is_constant_zero(op_3):
26 error_detected = True
27 op = op_1
28
29 if error_detected:
30 original = f"{op_1.as_string()} {op_2} {op_3.as_string()}"
31 suggestion = (
32 op.as_string()
33 if op_2 in {"!=", "is not"}
34 else f"not {op.as_string()}"
35 )
36 self.add_message(
37 "compare-to-zero",
38 args=(original, suggestion),
39 node=node,
40 confidence=HIGH,
41 )
Path 2: 2 calls (0.14)
Compare (2)
1@utils.only_required_for_messages("compare-to-zero")
2 def visit_compare(self, node: nodes.Compare) -> None:
3 # pylint: disable=duplicate-code
4 _operators = ["!=", "==", "is not", "is"]
5 # note: astroid.Compare has the left most operand in node.left
6 # while the rest are a list of tuples in node.ops
7 # the format of the tuple is ('compare operator sign', node)
8 # here we squash everything into `ops` to make it easier for processing later
9 ops: list[tuple[str, nodes.NodeNG]] = [("", node.left)]
10 ops.extend(node.ops)
11 iter_ops = iter(ops)
12 all_ops = list(itertools.chain(*iter_ops))
13
14 for ops_idx in range(len(all_ops) - 2):
15 op_1 = all_ops[ops_idx]
16 op_2 = all_ops[ops_idx + 1]
17 op_3 = all_ops[ops_idx + 2]
18 error_detected = False
19
20 # 0 ?? X
21 if _is_constant_zero(op_1) and op_2 in _operators:
22 error_detected = True
23 op = op_3
24 # X ?? 0
25 elif op_2 in _operators and _is_constant_zero(op_3):
26 error_detected = True
27 op = op_1
28
29 if error_detected:
30 original = f"{op_1.as_string()} {op_2} {op_3.as_string()}"
31 suggestion = (
32 op.as_string()
33 if op_2 in {"!=", "is not"}
34 else f"not {op.as_string()}"
35 )
36 self.add_message(
37 "compare-to-zero",
38 args=(original, suggestion),
39 node=node,
40 confidence=HIGH,
41 )
Path 3: 2 calls (0.14)
Compare (2)
1@utils.only_required_for_messages("compare-to-zero")
2 def visit_compare(self, node: nodes.Compare) -> None:
3 # pylint: disable=duplicate-code
4 _operators = ["!=", "==", "is not", "is"]
5 # note: astroid.Compare has the left most operand in node.left
6 # while the rest are a list of tuples in node.ops
7 # the format of the tuple is ('compare operator sign', node)
8 # here we squash everything into `ops` to make it easier for processing later
9 ops: list[tuple[str, nodes.NodeNG]] = [("", node.left)]
10 ops.extend(node.ops)
11 iter_ops = iter(ops)
12 all_ops = list(itertools.chain(*iter_ops))
13
14 for ops_idx in range(len(all_ops) - 2):
15 op_1 = all_ops[ops_idx]
16 op_2 = all_ops[ops_idx + 1]
17 op_3 = all_ops[ops_idx + 2]
18 error_detected = False
19
20 # 0 ?? X
21 if _is_constant_zero(op_1) and op_2 in _operators:
22 error_detected = True
23 op = op_3
24 # X ?? 0
25 elif op_2 in _operators and _is_constant_zero(op_3):
26 error_detected = True
27 op = op_1
28
29 if error_detected:
30 original = f"{op_1.as_string()} {op_2} {op_3.as_string()}"
31 suggestion = (
32 op.as_string()
33 if op_2 in {"!=", "is not"}
34 else f"not {op.as_string()}"
35 )
36 self.add_message(
37 "compare-to-zero",
38 args=(original, suggestion),
39 node=node,
40 confidence=HIGH,
41 )
Path 4: 1 calls (0.07)
Compare (1)
1@utils.only_required_for_messages("compare-to-zero")
2 def visit_compare(self, node: nodes.Compare) -> None:
3 # pylint: disable=duplicate-code
4 _operators = ["!=", "==", "is not", "is"]
5 # note: astroid.Compare has the left most operand in node.left
6 # while the rest are a list of tuples in node.ops
7 # the format of the tuple is ('compare operator sign', node)
8 # here we squash everything into `ops` to make it easier for processing later
9 ops: list[tuple[str, nodes.NodeNG]] = [("", node.left)]
10 ops.extend(node.ops)
11 iter_ops = iter(ops)
12 all_ops = list(itertools.chain(*iter_ops))
13
14 for ops_idx in range(len(all_ops) - 2):
15 op_1 = all_ops[ops_idx]
16 op_2 = all_ops[ops_idx + 1]
17 op_3 = all_ops[ops_idx + 2]
18 error_detected = False
19
20 # 0 ?? X
21 if _is_constant_zero(op_1) and op_2 in _operators:
22 error_detected = True
23 op = op_3
24 # X ?? 0
25 elif op_2 in _operators and _is_constant_zero(op_3):
26 error_detected = True
27 op = op_1
28
29 if error_detected:
30 original = f"{op_1.as_string()} {op_2} {op_3.as_string()}"
31 suggestion = (
32 op.as_string()
33 if op_2 in {"!=", "is not"}
34 else f"not {op.as_string()}"
35 )
36 self.add_message(
37 "compare-to-zero",
38 args=(original, suggestion),
39 node=node,
40 confidence=HIGH,
41 )
Path 5: 1 calls (0.07)
Compare (1)
1@utils.only_required_for_messages("compare-to-zero")
2 def visit_compare(self, node: nodes.Compare) -> None:
3 # pylint: disable=duplicate-code
4 _operators = ["!=", "==", "is not", "is"]
5 # note: astroid.Compare has the left most operand in node.left
6 # while the rest are a list of tuples in node.ops
7 # the format of the tuple is ('compare operator sign', node)
8 # here we squash everything into `ops` to make it easier for processing later
9 ops: list[tuple[str, nodes.NodeNG]] = [("", node.left)]
10 ops.extend(node.ops)
11 iter_ops = iter(ops)
12 all_ops = list(itertools.chain(*iter_ops))
13
14 for ops_idx in range(len(all_ops) - 2):
15 op_1 = all_ops[ops_idx]
16 op_2 = all_ops[ops_idx + 1]
17 op_3 = all_ops[ops_idx + 2]
18 error_detected = False
19
20 # 0 ?? X
21 if _is_constant_zero(op_1) and op_2 in _operators:
22 error_detected = True
23 op = op_3
24 # X ?? 0
25 elif op_2 in _operators and _is_constant_zero(op_3):
26 error_detected = True
27 op = op_1
28
29 if error_detected:
30 original = f"{op_1.as_string()} {op_2} {op_3.as_string()}"
31 suggestion = (
32 op.as_string()
33 if op_2 in {"!=", "is not"}
34 else f"not {op.as_string()}"
35 )
36 self.add_message(
37 "compare-to-zero",
38 args=(original, suggestion),
39 node=node,
40 confidence=HIGH,
41 )