Method: pylint.checkers.base.comparison_checker.ComparisonChecker._check_singleton_comparison
Calls: 516, Exceptions: 0, Paths: 7Back
Path 1: 490 calls (0.95)
Name (303) Attribute (55) Call (40) Const (37) Subscript (16) BinOp (15) List (8) NamedExpr (6) Tuple (3) Dict (3)
Const (318) Name (88) Attribute (27) Dict (15) List (14) Call (12) Tuple (4) BinOp (3) UnaryOp (3) Subscript (2)
Compare (490)
False (429) True (61)
None (490)
1def _check_singleton_comparison(
2 self,
3 left_value: nodes.NodeNG,
4 right_value: nodes.NodeNG,
5 root_node: nodes.Compare,
6 checking_for_absence: bool = False,
7 ) -> None:
8 """Check if == or != is being used to compare a singleton value."""
9
10 if utils.is_singleton_const(left_value):
11 singleton, other_value = left_value.value, right_value
12 elif utils.is_singleton_const(right_value):
13 singleton, other_value = right_value.value, left_value
14 else:
15 return
16
17 singleton_comparison_example = {False: "'{} is {}'", True: "'{} is not {}'"}
18
19 # True/False singletons have a special-cased message in case the user is
20 # mistakenly using == or != to check for truthiness
21 if singleton in {True, False}:
22 suggestion_template = (
23 "{} if checking for the singleton value {}, or {} if testing for {}"
24 )
25 truthiness_example = {False: "not {}", True: "{}"}
26 truthiness_phrase = {True: "truthiness", False: "falsiness"}
27
28 # Looks for comparisons like x == True or x != False
29 checking_truthiness = singleton is not checking_for_absence
30
31 suggestion = suggestion_template.format(
32 singleton_comparison_example[checking_for_absence].format(
33 left_value.as_string(), right_value.as_string()
34 ),
35 singleton,
36 (
37 "'bool({})'"
38 if not utils.is_test_condition(root_node) and checking_truthiness
39 else "'{}'"
40 ).format(
41 truthiness_example[checking_truthiness].format(
42 other_value.as_string()
43 )
44 ),
45 truthiness_phrase[checking_truthiness],
46 )
47 else:
48 suggestion = singleton_comparison_example[checking_for_absence].format(
49 left_value.as_string(), right_value.as_string()
50 )
51 self.add_message(
52 "singleton-comparison",
53 node=root_node,
54 args=(f"'{root_node.as_string()}'", suggestion),
55 )
Path 2: 12 calls (0.02)
Name (12)
Const (12)
Compare (12)
False (8) True (4)
1def _check_singleton_comparison(
2 self,
3 left_value: nodes.NodeNG,
4 right_value: nodes.NodeNG,
5 root_node: nodes.Compare,
6 checking_for_absence: bool = False,
7 ) -> None:
8 """Check if == or != is being used to compare a singleton value."""
9
10 if utils.is_singleton_const(left_value):
11 singleton, other_value = left_value.value, right_value
12 elif utils.is_singleton_const(right_value):
13 singleton, other_value = right_value.value, left_value
14 else:
15 return
16
17 singleton_comparison_example = {False: "'{} is {}'", True: "'{} is not {}'"}
18
19 # True/False singletons have a special-cased message in case the user is
20 # mistakenly using == or != to check for truthiness
21 if singleton in {True, False}:
22 suggestion_template = (
23 "{} if checking for the singleton value {}, or {} if testing for {}"
24 )
25 truthiness_example = {False: "not {}", True: "{}"}
26 truthiness_phrase = {True: "truthiness", False: "falsiness"}
27
28 # Looks for comparisons like x == True or x != False
29 checking_truthiness = singleton is not checking_for_absence
30
31 suggestion = suggestion_template.format(
32 singleton_comparison_example[checking_for_absence].format(
33 left_value.as_string(), right_value.as_string()
34 ),
35 singleton,
36 (
37 "'bool({})'"
38 if not utils.is_test_condition(root_node) and checking_truthiness
39 else "'{}'"
40 ).format(
41 truthiness_example[checking_truthiness].format(
42 other_value.as_string()
43 )
44 ),
45 truthiness_phrase[checking_truthiness],
46 )
47 else:
48 suggestion = singleton_comparison_example[checking_for_absence].format(
49 left_value.as_string(), right_value.as_string()
50 )
51 self.add_message(
52 "singleton-comparison",
53 node=root_node,
54 args=(f"'{root_node.as_string()}'", suggestion),
55 )
Path 3: 4 calls (0.01)
Name (4)
Const (4)
Compare (4)
False (3) True (1)
1def _check_singleton_comparison(
2 self,
3 left_value: nodes.NodeNG,
4 right_value: nodes.NodeNG,
5 root_node: nodes.Compare,
6 checking_for_absence: bool = False,
7 ) -> None:
8 """Check if == or != is being used to compare a singleton value."""
9
10 if utils.is_singleton_const(left_value):
11 singleton, other_value = left_value.value, right_value
12 elif utils.is_singleton_const(right_value):
13 singleton, other_value = right_value.value, left_value
14 else:
15 return
16
17 singleton_comparison_example = {False: "'{} is {}'", True: "'{} is not {}'"}
18
19 # True/False singletons have a special-cased message in case the user is
20 # mistakenly using == or != to check for truthiness
21 if singleton in {True, False}:
22 suggestion_template = (
23 "{} if checking for the singleton value {}, or {} if testing for {}"
24 )
25 truthiness_example = {False: "not {}", True: "{}"}
26 truthiness_phrase = {True: "truthiness", False: "falsiness"}
27
28 # Looks for comparisons like x == True or x != False
29 checking_truthiness = singleton is not checking_for_absence
30
31 suggestion = suggestion_template.format(
32 singleton_comparison_example[checking_for_absence].format(
33 left_value.as_string(), right_value.as_string()
34 ),
35 singleton,
36 (
37 "'bool({})'"
38 if not utils.is_test_condition(root_node) and checking_truthiness
39 else "'{}'"
40 ).format(
41 truthiness_example[checking_truthiness].format(
42 other_value.as_string()
43 )
44 ),
45 truthiness_phrase[checking_truthiness],
46 )
47 else:
48 suggestion = singleton_comparison_example[checking_for_absence].format(
49 left_value.as_string(), right_value.as_string()
50 )
51 self.add_message(
52 "singleton-comparison",
53 node=root_node,
54 args=(f"'{root_node.as_string()}'", suggestion),
55 )
Path 4: 3 calls (0.01)
Name (3)
Const (3)
Compare (3)
False (2) True (1)
1def _check_singleton_comparison(
2 self,
3 left_value: nodes.NodeNG,
4 right_value: nodes.NodeNG,
5 root_node: nodes.Compare,
6 checking_for_absence: bool = False,
7 ) -> None:
8 """Check if == or != is being used to compare a singleton value."""
9
10 if utils.is_singleton_const(left_value):
11 singleton, other_value = left_value.value, right_value
12 elif utils.is_singleton_const(right_value):
13 singleton, other_value = right_value.value, left_value
14 else:
15 return
16
17 singleton_comparison_example = {False: "'{} is {}'", True: "'{} is not {}'"}
18
19 # True/False singletons have a special-cased message in case the user is
20 # mistakenly using == or != to check for truthiness
21 if singleton in {True, False}:
22 suggestion_template = (
23 "{} if checking for the singleton value {}, or {} if testing for {}"
24 )
25 truthiness_example = {False: "not {}", True: "{}"}
26 truthiness_phrase = {True: "truthiness", False: "falsiness"}
27
28 # Looks for comparisons like x == True or x != False
29 checking_truthiness = singleton is not checking_for_absence
30
31 suggestion = suggestion_template.format(
32 singleton_comparison_example[checking_for_absence].format(
33 left_value.as_string(), right_value.as_string()
34 ),
35 singleton,
36 (
37 "'bool({})'"
38 if not utils.is_test_condition(root_node) and checking_truthiness
39 else "'{}'"
40 ).format(
41 truthiness_example[checking_truthiness].format(
42 other_value.as_string()
43 )
44 ),
45 truthiness_phrase[checking_truthiness],
46 )
47 else:
48 suggestion = singleton_comparison_example[checking_for_absence].format(
49 left_value.as_string(), right_value.as_string()
50 )
51 self.add_message(
52 "singleton-comparison",
53 node=root_node,
54 args=(f"'{root_node.as_string()}'", suggestion),
55 )
Path 5: 3 calls (0.01)
Const (3)
Const (2) Name (1)
Compare (3)
False (3)
1def _check_singleton_comparison(
2 self,
3 left_value: nodes.NodeNG,
4 right_value: nodes.NodeNG,
5 root_node: nodes.Compare,
6 checking_for_absence: bool = False,
7 ) -> None:
8 """Check if == or != is being used to compare a singleton value."""
9
10 if utils.is_singleton_const(left_value):
11 singleton, other_value = left_value.value, right_value
12 elif utils.is_singleton_const(right_value):
13 singleton, other_value = right_value.value, left_value
14 else:
15 return
16
17 singleton_comparison_example = {False: "'{} is {}'", True: "'{} is not {}'"}
18
19 # True/False singletons have a special-cased message in case the user is
20 # mistakenly using == or != to check for truthiness
21 if singleton in {True, False}:
22 suggestion_template = (
23 "{} if checking for the singleton value {}, or {} if testing for {}"
24 )
25 truthiness_example = {False: "not {}", True: "{}"}
26 truthiness_phrase = {True: "truthiness", False: "falsiness"}
27
28 # Looks for comparisons like x == True or x != False
29 checking_truthiness = singleton is not checking_for_absence
30
31 suggestion = suggestion_template.format(
32 singleton_comparison_example[checking_for_absence].format(
33 left_value.as_string(), right_value.as_string()
34 ),
35 singleton,
36 (
37 "'bool({})'"
38 if not utils.is_test_condition(root_node) and checking_truthiness
39 else "'{}'"
40 ).format(
41 truthiness_example[checking_truthiness].format(
42 other_value.as_string()
43 )
44 ),
45 truthiness_phrase[checking_truthiness],
46 )
47 else:
48 suggestion = singleton_comparison_example[checking_for_absence].format(
49 left_value.as_string(), right_value.as_string()
50 )
51 self.add_message(
52 "singleton-comparison",
53 node=root_node,
54 args=(f"'{root_node.as_string()}'", suggestion),
55 )
Path 6: 2 calls (0.0)
Const (2)
Const (1) Name (1)
Compare (2)
False (2)
1def _check_singleton_comparison(
2 self,
3 left_value: nodes.NodeNG,
4 right_value: nodes.NodeNG,
5 root_node: nodes.Compare,
6 checking_for_absence: bool = False,
7 ) -> None:
8 """Check if == or != is being used to compare a singleton value."""
9
10 if utils.is_singleton_const(left_value):
11 singleton, other_value = left_value.value, right_value
12 elif utils.is_singleton_const(right_value):
13 singleton, other_value = right_value.value, left_value
14 else:
15 return
16
17 singleton_comparison_example = {False: "'{} is {}'", True: "'{} is not {}'"}
18
19 # True/False singletons have a special-cased message in case the user is
20 # mistakenly using == or != to check for truthiness
21 if singleton in {True, False}:
22 suggestion_template = (
23 "{} if checking for the singleton value {}, or {} if testing for {}"
24 )
25 truthiness_example = {False: "not {}", True: "{}"}
26 truthiness_phrase = {True: "truthiness", False: "falsiness"}
27
28 # Looks for comparisons like x == True or x != False
29 checking_truthiness = singleton is not checking_for_absence
30
31 suggestion = suggestion_template.format(
32 singleton_comparison_example[checking_for_absence].format(
33 left_value.as_string(), right_value.as_string()
34 ),
35 singleton,
36 (
37 "'bool({})'"
38 if not utils.is_test_condition(root_node) and checking_truthiness
39 else "'{}'"
40 ).format(
41 truthiness_example[checking_truthiness].format(
42 other_value.as_string()
43 )
44 ),
45 truthiness_phrase[checking_truthiness],
46 )
47 else:
48 suggestion = singleton_comparison_example[checking_for_absence].format(
49 left_value.as_string(), right_value.as_string()
50 )
51 self.add_message(
52 "singleton-comparison",
53 node=root_node,
54 args=(f"'{root_node.as_string()}'", suggestion),
55 )
Path 7: 2 calls (0.0)
Const (2)
Const (1) Name (1)
Compare (2)
False (2)
1def _check_singleton_comparison(
2 self,
3 left_value: nodes.NodeNG,
4 right_value: nodes.NodeNG,
5 root_node: nodes.Compare,
6 checking_for_absence: bool = False,
7 ) -> None:
8 """Check if == or != is being used to compare a singleton value."""
9
10 if utils.is_singleton_const(left_value):
11 singleton, other_value = left_value.value, right_value
12 elif utils.is_singleton_const(right_value):
13 singleton, other_value = right_value.value, left_value
14 else:
15 return
16
17 singleton_comparison_example = {False: "'{} is {}'", True: "'{} is not {}'"}
18
19 # True/False singletons have a special-cased message in case the user is
20 # mistakenly using == or != to check for truthiness
21 if singleton in {True, False}:
22 suggestion_template = (
23 "{} if checking for the singleton value {}, or {} if testing for {}"
24 )
25 truthiness_example = {False: "not {}", True: "{}"}
26 truthiness_phrase = {True: "truthiness", False: "falsiness"}
27
28 # Looks for comparisons like x == True or x != False
29 checking_truthiness = singleton is not checking_for_absence
30
31 suggestion = suggestion_template.format(
32 singleton_comparison_example[checking_for_absence].format(
33 left_value.as_string(), right_value.as_string()
34 ),
35 singleton,
36 (
37 "'bool({})'"
38 if not utils.is_test_condition(root_node) and checking_truthiness
39 else "'{}'"
40 ).format(
41 truthiness_example[checking_truthiness].format(
42 other_value.as_string()
43 )
44 ),
45 truthiness_phrase[checking_truthiness],
46 )
47 else:
48 suggestion = singleton_comparison_example[checking_for_absence].format(
49 left_value.as_string(), right_value.as_string()
50 )
51 self.add_message(
52 "singleton-comparison",
53 node=root_node,
54 args=(f"'{root_node.as_string()}'", suggestion),
55 )