Method: pylint.extensions.emptystring.CompareToEmptyStringChecker.visit_compare
Calls: 6, Exceptions: 0, Paths: 2Back
Path 1: 4 calls (0.67)
Compare (4)
1@utils.only_required_for_messages("compare-to-empty-string")
2 def visit_compare(self, node: nodes.Compare) -> None:
3 """Checks for comparisons to empty string.
4
5 Most of the time you should use the fact that empty strings are false.
6 An exception to this rule is when an empty string value is allowed in the program
7 and has a different meaning than None!
8 """
9 _operators = {"!=", "==", "is not", "is"}
10 # note: astroid.Compare has the left most operand in node.left while the rest
11 # are a list of tuples in node.ops the format of the tuple is
12 # ('compare operator sign', node) here we squash everything into `ops`
13 # to make it easier for processing later
14 ops: list[tuple[str, nodes.NodeNG | None]] = [("", node.left)]
15 ops.extend(node.ops)
16 iter_ops = iter(ops)
17 ops = list(itertools.chain(*iter_ops)) # type: ignore[arg-type]
18 for ops_idx in range(len(ops) - 2):
19 op_1: nodes.NodeNG | None = ops[ops_idx]
20 op_2: str = ops[ops_idx + 1] # type: ignore[assignment]
21 op_3: nodes.NodeNG | None = ops[ops_idx + 2]
22 error_detected = False
23 if op_1 is None or op_3 is None or op_2 not in _operators:
24 continue
25 node_name = ""
26 # x ?? ""
27 if utils.is_empty_str_literal(op_1):
28 error_detected = True
29 node_name = op_3.as_string()
30 # '' ?? X
31 elif utils.is_empty_str_literal(op_3):
32 error_detected = True
33 node_name = op_1.as_string()
34 if error_detected:
35 suggestion = f"not {node_name}" if op_2 in {"==", "is"} else node_name
36 self.add_message(
37 "compare-to-empty-string",
38 args=(node.as_string(), suggestion),
39 node=node,
40 confidence=HIGH,
41 )
Path 2: 2 calls (0.33)
Compare (2)
1@utils.only_required_for_messages("compare-to-empty-string")
2 def visit_compare(self, node: nodes.Compare) -> None:
3 """Checks for comparisons to empty string.
4
5 Most of the time you should use the fact that empty strings are false.
6 An exception to this rule is when an empty string value is allowed in the program
7 and has a different meaning than None!
8 """
9 _operators = {"!=", "==", "is not", "is"}
10 # note: astroid.Compare has the left most operand in node.left while the rest
11 # are a list of tuples in node.ops the format of the tuple is
12 # ('compare operator sign', node) here we squash everything into `ops`
13 # to make it easier for processing later
14 ops: list[tuple[str, nodes.NodeNG | None]] = [("", node.left)]
15 ops.extend(node.ops)
16 iter_ops = iter(ops)
17 ops = list(itertools.chain(*iter_ops)) # type: ignore[arg-type]
18 for ops_idx in range(len(ops) - 2):
19 op_1: nodes.NodeNG | None = ops[ops_idx]
20 op_2: str = ops[ops_idx + 1] # type: ignore[assignment]
21 op_3: nodes.NodeNG | None = ops[ops_idx + 2]
22 error_detected = False
23 if op_1 is None or op_3 is None or op_2 not in _operators:
24 continue
25 node_name = ""
26 # x ?? ""
27 if utils.is_empty_str_literal(op_1):
28 error_detected = True
29 node_name = op_3.as_string()
30 # '' ?? X
31 elif utils.is_empty_str_literal(op_3):
32 error_detected = True
33 node_name = op_1.as_string()
34 if error_detected:
35 suggestion = f"not {node_name}" if op_2 in {"==", "is"} else node_name
36 self.add_message(
37 "compare-to-empty-string",
38 args=(node.as_string(), suggestion),
39 node=node,
40 confidence=HIGH,
41 )