Method: pylint.extensions.code_style.CodeStyleChecker._check_consider_using_assignment_expr
Calls: 38, Exceptions: 0, Paths: 9Back
Path 1: 13 calls (0.34)
If (13)
1def _check_consider_using_assignment_expr(self, node: nodes.If) -> None:
2 """Check if an assignment expression (walrus operator) can be used.
3
4 For example if an assignment is directly followed by an if statement:
5 >>> x = 2
6 >>> if x:
7 >>> ...
8
9 Can be replaced by:
10 >>> if (x := 2):
11 >>> ...
12
13 Note: Assignment expressions were added in Python 3.8
14 """
15 # Check if `node.test` contains a `Name` node
16 node_name: nodes.Name | None = None
17 if isinstance(node.test, nodes.Name):
18 node_name = node.test
19 elif (
20 isinstance(node.test, nodes.UnaryOp)
21 and node.test.op == "not"
22 and isinstance(node.test.operand, nodes.Name)
23 ):
24 node_name = node.test.operand
25 elif (
26 isinstance(node.test, nodes.Compare)
27 and isinstance(node.test.left, nodes.Name)
28 and len(node.test.ops) == 1
29 ):
30 node_name = node.test.left
31 else:
32 return
33
34 # Make sure the previous node is an assignment to the same name
35 # used in `node.test`. Furthermore, ignore if assignment spans multiple lines.
36 prev_sibling = node.previous_sibling()
37 if CodeStyleChecker._check_prev_sibling_to_if_stmt(
38 prev_sibling, node_name.name
39 ):
40 # Check if match statement would be a better fit.
41 # I.e. multiple ifs that test the same name.
42 if CodeStyleChecker._check_ignore_assignment_expr_suggestion(
43 node, node_name.name
44 ):
45 return
46
47 # Build suggestion string. Check length of suggestion
48 # does not exceed max-line-length-suggestions
49 test_str = node.test.as_string().replace(
50 node_name.name,
51 f"({node_name.name} := {prev_sibling.value.as_string()})",
52 1,
53 )
54 suggestion = f"if {test_str}:"
55 if (
56 node.col_offset is not None
57 and len(suggestion) + node.col_offset > self._max_length
58 or len(suggestion) > self._max_length
59 ):
60 return
61
62 self.add_message(
63 "consider-using-assignment-expr",
64 node=node_name,
65 args=(suggestion,),
66 )
Path 2: 6 calls (0.16)
If (6)
1def _check_consider_using_assignment_expr(self, node: nodes.If) -> None:
2 """Check if an assignment expression (walrus operator) can be used.
3
4 For example if an assignment is directly followed by an if statement:
5 >>> x = 2
6 >>> if x:
7 >>> ...
8
9 Can be replaced by:
10 >>> if (x := 2):
11 >>> ...
12
13 Note: Assignment expressions were added in Python 3.8
14 """
15 # Check if `node.test` contains a `Name` node
16 node_name: nodes.Name | None = None
17 if isinstance(node.test, nodes.Name):
18 node_name = node.test
19 elif (
20 isinstance(node.test, nodes.UnaryOp)
21 and node.test.op == "not"
22 and isinstance(node.test.operand, nodes.Name)
23 ):
24 node_name = node.test.operand
25 elif (
26 isinstance(node.test, nodes.Compare)
27 and isinstance(node.test.left, nodes.Name)
28 and len(node.test.ops) == 1
29 ):
30 node_name = node.test.left
31 else:
32 return
33
34 # Make sure the previous node is an assignment to the same name
35 # used in `node.test`. Furthermore, ignore if assignment spans multiple lines.
36 prev_sibling = node.previous_sibling()
37 if CodeStyleChecker._check_prev_sibling_to_if_stmt(
38 prev_sibling, node_name.name
39 ):
40 # Check if match statement would be a better fit.
41 # I.e. multiple ifs that test the same name.
42 if CodeStyleChecker._check_ignore_assignment_expr_suggestion(
43 node, node_name.name
44 ):
45 return
46
47 # Build suggestion string. Check length of suggestion
48 # does not exceed max-line-length-suggestions
49 test_str = node.test.as_string().replace(
50 node_name.name,
51 f"({node_name.name} := {prev_sibling.value.as_string()})",
52 1,
53 )
54 suggestion = f"if {test_str}:"
55 if (
56 node.col_offset is not None
57 and len(suggestion) + node.col_offset > self._max_length
58 or len(suggestion) > self._max_length
59 ):
60 return
61
62 self.add_message(
63 "consider-using-assignment-expr",
64 node=node_name,
65 args=(suggestion,),
66 )
Path 3: 5 calls (0.13)
If (5)
1def _check_consider_using_assignment_expr(self, node: nodes.If) -> None:
2 """Check if an assignment expression (walrus operator) can be used.
3
4 For example if an assignment is directly followed by an if statement:
5 >>> x = 2
6 >>> if x:
7 >>> ...
8
9 Can be replaced by:
10 >>> if (x := 2):
11 >>> ...
12
13 Note: Assignment expressions were added in Python 3.8
14 """
15 # Check if `node.test` contains a `Name` node
16 node_name: nodes.Name | None = None
17 if isinstance(node.test, nodes.Name):
18 node_name = node.test
19 elif (
20 isinstance(node.test, nodes.UnaryOp)
21 and node.test.op == "not"
22 and isinstance(node.test.operand, nodes.Name)
23 ):
24 node_name = node.test.operand
25 elif (
26 isinstance(node.test, nodes.Compare)
27 and isinstance(node.test.left, nodes.Name)
28 and len(node.test.ops) == 1
29 ):
30 node_name = node.test.left
31 else:
32 return
33
34 # Make sure the previous node is an assignment to the same name
35 # used in `node.test`. Furthermore, ignore if assignment spans multiple lines.
36 prev_sibling = node.previous_sibling()
37 if CodeStyleChecker._check_prev_sibling_to_if_stmt(
38 prev_sibling, node_name.name
39 ):
40 # Check if match statement would be a better fit.
41 # I.e. multiple ifs that test the same name.
42 if CodeStyleChecker._check_ignore_assignment_expr_suggestion(
43 node, node_name.name
44 ):
45 return
46
47 # Build suggestion string. Check length of suggestion
48 # does not exceed max-line-length-suggestions
49 test_str = node.test.as_string().replace(
50 node_name.name,
51 f"({node_name.name} := {prev_sibling.value.as_string()})",
52 1,
53 )
54 suggestion = f"if {test_str}:"
55 if (
56 node.col_offset is not None
57 and len(suggestion) + node.col_offset > self._max_length
58 or len(suggestion) > self._max_length
59 ):
60 return
61
62 self.add_message(
63 "consider-using-assignment-expr",
64 node=node_name,
65 args=(suggestion,),
66 )
Path 4: 4 calls (0.11)
If (4)
1def _check_consider_using_assignment_expr(self, node: nodes.If) -> None:
2 """Check if an assignment expression (walrus operator) can be used.
3
4 For example if an assignment is directly followed by an if statement:
5 >>> x = 2
6 >>> if x:
7 >>> ...
8
9 Can be replaced by:
10 >>> if (x := 2):
11 >>> ...
12
13 Note: Assignment expressions were added in Python 3.8
14 """
15 # Check if `node.test` contains a `Name` node
16 node_name: nodes.Name | None = None
17 if isinstance(node.test, nodes.Name):
18 node_name = node.test
19 elif (
20 isinstance(node.test, nodes.UnaryOp)
21 and node.test.op == "not"
22 and isinstance(node.test.operand, nodes.Name)
23 ):
24 node_name = node.test.operand
25 elif (
26 isinstance(node.test, nodes.Compare)
27 and isinstance(node.test.left, nodes.Name)
28 and len(node.test.ops) == 1
29 ):
30 node_name = node.test.left
31 else:
32 return
33
34 # Make sure the previous node is an assignment to the same name
35 # used in `node.test`. Furthermore, ignore if assignment spans multiple lines.
36 prev_sibling = node.previous_sibling()
37 if CodeStyleChecker._check_prev_sibling_to_if_stmt(
38 prev_sibling, node_name.name
39 ):
40 # Check if match statement would be a better fit.
41 # I.e. multiple ifs that test the same name.
42 if CodeStyleChecker._check_ignore_assignment_expr_suggestion(
43 node, node_name.name
44 ):
45 return
46
47 # Build suggestion string. Check length of suggestion
48 # does not exceed max-line-length-suggestions
49 test_str = node.test.as_string().replace(
50 node_name.name,
51 f"({node_name.name} := {prev_sibling.value.as_string()})",
52 1,
53 )
54 suggestion = f"if {test_str}:"
55 if (
56 node.col_offset is not None
57 and len(suggestion) + node.col_offset > self._max_length
58 or len(suggestion) > self._max_length
59 ):
60 return
61
62 self.add_message(
63 "consider-using-assignment-expr",
64 node=node_name,
65 args=(suggestion,),
66 )
Path 5: 4 calls (0.11)
If (4)
None (4)
1def _check_consider_using_assignment_expr(self, node: nodes.If) -> None:
2 """Check if an assignment expression (walrus operator) can be used.
3
4 For example if an assignment is directly followed by an if statement:
5 >>> x = 2
6 >>> if x:
7 >>> ...
8
9 Can be replaced by:
10 >>> if (x := 2):
11 >>> ...
12
13 Note: Assignment expressions were added in Python 3.8
14 """
15 # Check if `node.test` contains a `Name` node
16 node_name: nodes.Name | None = None
17 if isinstance(node.test, nodes.Name):
18 node_name = node.test
19 elif (
20 isinstance(node.test, nodes.UnaryOp)
21 and node.test.op == "not"
22 and isinstance(node.test.operand, nodes.Name)
23 ):
24 node_name = node.test.operand
25 elif (
26 isinstance(node.test, nodes.Compare)
27 and isinstance(node.test.left, nodes.Name)
28 and len(node.test.ops) == 1
29 ):
30 node_name = node.test.left
31 else:
32 return
33
34 # Make sure the previous node is an assignment to the same name
35 # used in `node.test`. Furthermore, ignore if assignment spans multiple lines.
36 prev_sibling = node.previous_sibling()
37 if CodeStyleChecker._check_prev_sibling_to_if_stmt(
38 prev_sibling, node_name.name
39 ):
40 # Check if match statement would be a better fit.
41 # I.e. multiple ifs that test the same name.
42 if CodeStyleChecker._check_ignore_assignment_expr_suggestion(
43 node, node_name.name
44 ):
45 return
46
47 # Build suggestion string. Check length of suggestion
48 # does not exceed max-line-length-suggestions
49 test_str = node.test.as_string().replace(
50 node_name.name,
51 f"({node_name.name} := {prev_sibling.value.as_string()})",
52 1,
53 )
54 suggestion = f"if {test_str}:"
55 if (
56 node.col_offset is not None
57 and len(suggestion) + node.col_offset > self._max_length
58 or len(suggestion) > self._max_length
59 ):
60 return
61
62 self.add_message(
63 "consider-using-assignment-expr",
64 node=node_name,
65 args=(suggestion,),
66 )
Path 6: 3 calls (0.08)
If (3)
None (3)
1def _check_consider_using_assignment_expr(self, node: nodes.If) -> None:
2 """Check if an assignment expression (walrus operator) can be used.
3
4 For example if an assignment is directly followed by an if statement:
5 >>> x = 2
6 >>> if x:
7 >>> ...
8
9 Can be replaced by:
10 >>> if (x := 2):
11 >>> ...
12
13 Note: Assignment expressions were added in Python 3.8
14 """
15 # Check if `node.test` contains a `Name` node
16 node_name: nodes.Name | None = None
17 if isinstance(node.test, nodes.Name):
18 node_name = node.test
19 elif (
20 isinstance(node.test, nodes.UnaryOp)
21 and node.test.op == "not"
22 and isinstance(node.test.operand, nodes.Name)
23 ):
24 node_name = node.test.operand
25 elif (
26 isinstance(node.test, nodes.Compare)
27 and isinstance(node.test.left, nodes.Name)
28 and len(node.test.ops) == 1
29 ):
30 node_name = node.test.left
31 else:
32 return
33
34 # Make sure the previous node is an assignment to the same name
35 # used in `node.test`. Furthermore, ignore if assignment spans multiple lines.
36 prev_sibling = node.previous_sibling()
37 if CodeStyleChecker._check_prev_sibling_to_if_stmt(
38 prev_sibling, node_name.name
39 ):
40 # Check if match statement would be a better fit.
41 # I.e. multiple ifs that test the same name.
42 if CodeStyleChecker._check_ignore_assignment_expr_suggestion(
43 node, node_name.name
44 ):
45 return
46
47 # Build suggestion string. Check length of suggestion
48 # does not exceed max-line-length-suggestions
49 test_str = node.test.as_string().replace(
50 node_name.name,
51 f"({node_name.name} := {prev_sibling.value.as_string()})",
52 1,
53 )
54 suggestion = f"if {test_str}:"
55 if (
56 node.col_offset is not None
57 and len(suggestion) + node.col_offset > self._max_length
58 or len(suggestion) > self._max_length
59 ):
60 return
61
62 self.add_message(
63 "consider-using-assignment-expr",
64 node=node_name,
65 args=(suggestion,),
66 )
Path 7: 1 calls (0.03)
If (1)
None (1)
1def _check_consider_using_assignment_expr(self, node: nodes.If) -> None:
2 """Check if an assignment expression (walrus operator) can be used.
3
4 For example if an assignment is directly followed by an if statement:
5 >>> x = 2
6 >>> if x:
7 >>> ...
8
9 Can be replaced by:
10 >>> if (x := 2):
11 >>> ...
12
13 Note: Assignment expressions were added in Python 3.8
14 """
15 # Check if `node.test` contains a `Name` node
16 node_name: nodes.Name | None = None
17 if isinstance(node.test, nodes.Name):
18 node_name = node.test
19 elif (
20 isinstance(node.test, nodes.UnaryOp)
21 and node.test.op == "not"
22 and isinstance(node.test.operand, nodes.Name)
23 ):
24 node_name = node.test.operand
25 elif (
26 isinstance(node.test, nodes.Compare)
27 and isinstance(node.test.left, nodes.Name)
28 and len(node.test.ops) == 1
29 ):
30 node_name = node.test.left
31 else:
32 return
33
34 # Make sure the previous node is an assignment to the same name
35 # used in `node.test`. Furthermore, ignore if assignment spans multiple lines.
36 prev_sibling = node.previous_sibling()
37 if CodeStyleChecker._check_prev_sibling_to_if_stmt(
38 prev_sibling, node_name.name
39 ):
40 # Check if match statement would be a better fit.
41 # I.e. multiple ifs that test the same name.
42 if CodeStyleChecker._check_ignore_assignment_expr_suggestion(
43 node, node_name.name
44 ):
45 return
46
47 # Build suggestion string. Check length of suggestion
48 # does not exceed max-line-length-suggestions
49 test_str = node.test.as_string().replace(
50 node_name.name,
51 f"({node_name.name} := {prev_sibling.value.as_string()})",
52 1,
53 )
54 suggestion = f"if {test_str}:"
55 if (
56 node.col_offset is not None
57 and len(suggestion) + node.col_offset > self._max_length
58 or len(suggestion) > self._max_length
59 ):
60 return
61
62 self.add_message(
63 "consider-using-assignment-expr",
64 node=node_name,
65 args=(suggestion,),
66 )
Path 8: 1 calls (0.03)
If (1)
None (1)
1def _check_consider_using_assignment_expr(self, node: nodes.If) -> None:
2 """Check if an assignment expression (walrus operator) can be used.
3
4 For example if an assignment is directly followed by an if statement:
5 >>> x = 2
6 >>> if x:
7 >>> ...
8
9 Can be replaced by:
10 >>> if (x := 2):
11 >>> ...
12
13 Note: Assignment expressions were added in Python 3.8
14 """
15 # Check if `node.test` contains a `Name` node
16 node_name: nodes.Name | None = None
17 if isinstance(node.test, nodes.Name):
18 node_name = node.test
19 elif (
20 isinstance(node.test, nodes.UnaryOp)
21 and node.test.op == "not"
22 and isinstance(node.test.operand, nodes.Name)
23 ):
24 node_name = node.test.operand
25 elif (
26 isinstance(node.test, nodes.Compare)
27 and isinstance(node.test.left, nodes.Name)
28 and len(node.test.ops) == 1
29 ):
30 node_name = node.test.left
31 else:
32 return
33
34 # Make sure the previous node is an assignment to the same name
35 # used in `node.test`. Furthermore, ignore if assignment spans multiple lines.
36 prev_sibling = node.previous_sibling()
37 if CodeStyleChecker._check_prev_sibling_to_if_stmt(
38 prev_sibling, node_name.name
39 ):
40 # Check if match statement would be a better fit.
41 # I.e. multiple ifs that test the same name.
42 if CodeStyleChecker._check_ignore_assignment_expr_suggestion(
43 node, node_name.name
44 ):
45 return
46
47 # Build suggestion string. Check length of suggestion
48 # does not exceed max-line-length-suggestions
49 test_str = node.test.as_string().replace(
50 node_name.name,
51 f"({node_name.name} := {prev_sibling.value.as_string()})",
52 1,
53 )
54 suggestion = f"if {test_str}:"
55 if (
56 node.col_offset is not None
57 and len(suggestion) + node.col_offset > self._max_length
58 or len(suggestion) > self._max_length
59 ):
60 return
61
62 self.add_message(
63 "consider-using-assignment-expr",
64 node=node_name,
65 args=(suggestion,),
66 )
Path 9: 1 calls (0.03)
If (1)
1def _check_consider_using_assignment_expr(self, node: nodes.If) -> None:
2 """Check if an assignment expression (walrus operator) can be used.
3
4 For example if an assignment is directly followed by an if statement:
5 >>> x = 2
6 >>> if x:
7 >>> ...
8
9 Can be replaced by:
10 >>> if (x := 2):
11 >>> ...
12
13 Note: Assignment expressions were added in Python 3.8
14 """
15 # Check if `node.test` contains a `Name` node
16 node_name: nodes.Name | None = None
17 if isinstance(node.test, nodes.Name):
18 node_name = node.test
19 elif (
20 isinstance(node.test, nodes.UnaryOp)
21 and node.test.op == "not"
22 and isinstance(node.test.operand, nodes.Name)
23 ):
24 node_name = node.test.operand
25 elif (
26 isinstance(node.test, nodes.Compare)
27 and isinstance(node.test.left, nodes.Name)
28 and len(node.test.ops) == 1
29 ):
30 node_name = node.test.left
31 else:
32 return
33
34 # Make sure the previous node is an assignment to the same name
35 # used in `node.test`. Furthermore, ignore if assignment spans multiple lines.
36 prev_sibling = node.previous_sibling()
37 if CodeStyleChecker._check_prev_sibling_to_if_stmt(
38 prev_sibling, node_name.name
39 ):
40 # Check if match statement would be a better fit.
41 # I.e. multiple ifs that test the same name.
42 if CodeStyleChecker._check_ignore_assignment_expr_suggestion(
43 node, node_name.name
44 ):
45 return
46
47 # Build suggestion string. Check length of suggestion
48 # does not exceed max-line-length-suggestions
49 test_str = node.test.as_string().replace(
50 node_name.name,
51 f"({node_name.name} := {prev_sibling.value.as_string()})",
52 1,
53 )
54 suggestion = f"if {test_str}:"
55 if (
56 node.col_offset is not None
57 and len(suggestion) + node.col_offset > self._max_length
58 or len(suggestion) > self._max_length
59 ):
60 return
61
62 self.add_message(
63 "consider-using-assignment-expr",
64 node=node_name,
65 args=(suggestion,),
66 )