Method: pylint.checkers.strings.StringFormatChecker._check_new_format
Calls: 152, Exceptions: 11, Paths: 20Back
Path 1: 41 calls (0.27)
Call (41)
BoundMethod (41)
1def _check_new_format(self, node: nodes.Call, func: bases.BoundMethod) -> None:
2 """Check the new string formatting."""
3 # Skip format nodes which don't have an explicit string on the
4 # left side of the format operation.
5 # We do this because our inference engine can't properly handle
6 # redefinition of the original string.
7 # Note that there may not be any left side at all, if the format method
8 # has been assigned to another variable. See issue 351. For example:
9 #
10 # fmt = 'some string {}'.format
11 # fmt('arg')
12 if isinstance(node.func, nodes.Attribute) and not isinstance(
13 node.func.expr, nodes.Const
14 ):
15 return
16 if node.starargs or node.kwargs:
17 return
18 try:
19 strnode = next(func.bound.infer())
20 except astroid.InferenceError:
21 return
22 if not (isinstance(strnode, nodes.Const) and isinstance(strnode.value, str)):
23 return
24 try:
25 call_site = astroid.arguments.CallSite.from_call(node)
26 except astroid.InferenceError:
27 return
28
29 try:
30 fields, num_args, manual_pos = utils.parse_format_method_string(
31 strnode.value
32 )
33 except utils.IncompleteFormatString:
34 self.add_message("bad-format-string", node=node)
35 return
36
37 positional_arguments = call_site.positional_arguments
38 named_arguments = call_site.keyword_arguments
39 named_fields = {field[0] for field in fields if isinstance(field[0], str)}
40 if num_args and manual_pos:
41 self.add_message("format-combined-specification", node=node)
42 return
43
44 check_args = False
45 # Consider "{[0]} {[1]}" as num_args.
46 num_args += sum(1 for field in named_fields if not field)
47 if named_fields:
48 for field in named_fields:
49 if field and field not in named_arguments:
50 self.add_message(
51 "missing-format-argument-key", node=node, args=(field,)
52 )
53 for field in named_arguments:
54 if field not in named_fields:
55 self.add_message(
56 "unused-format-string-argument", node=node, args=(field,)
57 )
58 # num_args can be 0 if manual_pos is not.
59 num_args = num_args or manual_pos
60 if positional_arguments or num_args:
61 empty = not all(field for field in named_fields)
62 if named_arguments or empty:
63 # Verify the required number of positional arguments
64 # only if the .format got at least one keyword argument.
65 # This means that the format strings accepts both
66 # positional and named fields and we should warn
67 # when one of them is missing or is extra.
68 check_args = True
69 else:
70 check_args = True
71 if check_args:
72 # num_args can be 0 if manual_pos is not.
73 num_args = num_args or manual_pos
74 if not num_args:
75 self.add_message("format-string-without-interpolation", node=node)
76 return
77 if len(positional_arguments) > num_args:
78 self.add_message("too-many-format-args", node=node)
79 elif len(positional_arguments) < num_args:
80 self.add_message("too-few-format-args", node=node)
81
82 self._detect_vacuous_formatting(node, positional_arguments)
83 self._check_new_format_specifiers(node, fields, named_arguments)
Path 2: 33 calls (0.22)
Call (33)
BoundMethod (33)
1def _check_new_format(self, node: nodes.Call, func: bases.BoundMethod) -> None:
2 """Check the new string formatting."""
3 # Skip format nodes which don't have an explicit string on the
4 # left side of the format operation.
5 # We do this because our inference engine can't properly handle
6 # redefinition of the original string.
7 # Note that there may not be any left side at all, if the format method
8 # has been assigned to another variable. See issue 351. For example:
9 #
10 # fmt = 'some string {}'.format
11 # fmt('arg')
12 if isinstance(node.func, nodes.Attribute) and not isinstance(
13 node.func.expr, nodes.Const
14 ):
15 return
16 if node.starargs or node.kwargs:
17 return
18 try:
19 strnode = next(func.bound.infer())
20 except astroid.InferenceError:
21 return
22 if not (isinstance(strnode, nodes.Const) and isinstance(strnode.value, str)):
23 return
24 try:
25 call_site = astroid.arguments.CallSite.from_call(node)
26 except astroid.InferenceError:
27 return
28
29 try:
30 fields, num_args, manual_pos = utils.parse_format_method_string(
31 strnode.value
32 )
33 except utils.IncompleteFormatString:
34 self.add_message("bad-format-string", node=node)
35 return
36
37 positional_arguments = call_site.positional_arguments
38 named_arguments = call_site.keyword_arguments
39 named_fields = {field[0] for field in fields if isinstance(field[0], str)}
40 if num_args and manual_pos:
41 self.add_message("format-combined-specification", node=node)
42 return
43
44 check_args = False
45 # Consider "{[0]} {[1]}" as num_args.
46 num_args += sum(1 for field in named_fields if not field)
47 if named_fields:
48 for field in named_fields:
49 if field and field not in named_arguments:
50 self.add_message(
51 "missing-format-argument-key", node=node, args=(field,)
52 )
53 for field in named_arguments:
54 if field not in named_fields:
55 self.add_message(
56 "unused-format-string-argument", node=node, args=(field,)
57 )
58 # num_args can be 0 if manual_pos is not.
59 num_args = num_args or manual_pos
60 if positional_arguments or num_args:
61 empty = not all(field for field in named_fields)
62 if named_arguments or empty:
63 # Verify the required number of positional arguments
64 # only if the .format got at least one keyword argument.
65 # This means that the format strings accepts both
66 # positional and named fields and we should warn
67 # when one of them is missing or is extra.
68 check_args = True
69 else:
70 check_args = True
71 if check_args:
72 # num_args can be 0 if manual_pos is not.
73 num_args = num_args or manual_pos
74 if not num_args:
75 self.add_message("format-string-without-interpolation", node=node)
76 return
77 if len(positional_arguments) > num_args:
78 self.add_message("too-many-format-args", node=node)
79 elif len(positional_arguments) < num_args:
80 self.add_message("too-few-format-args", node=node)
81
82 self._detect_vacuous_formatting(node, positional_arguments)
83 self._check_new_format_specifiers(node, fields, named_arguments)
Path 3: 32 calls (0.21)
Call (32)
BoundMethod (32)
None (32)
1def _check_new_format(self, node: nodes.Call, func: bases.BoundMethod) -> None:
2 """Check the new string formatting."""
3 # Skip format nodes which don't have an explicit string on the
4 # left side of the format operation.
5 # We do this because our inference engine can't properly handle
6 # redefinition of the original string.
7 # Note that there may not be any left side at all, if the format method
8 # has been assigned to another variable. See issue 351. For example:
9 #
10 # fmt = 'some string {}'.format
11 # fmt('arg')
12 if isinstance(node.func, nodes.Attribute) and not isinstance(
13 node.func.expr, nodes.Const
14 ):
15 return
16 if node.starargs or node.kwargs:
17 return
18 try:
19 strnode = next(func.bound.infer())
20 except astroid.InferenceError:
21 return
22 if not (isinstance(strnode, nodes.Const) and isinstance(strnode.value, str)):
23 return
24 try:
25 call_site = astroid.arguments.CallSite.from_call(node)
26 except astroid.InferenceError:
27 return
28
29 try:
30 fields, num_args, manual_pos = utils.parse_format_method_string(
31 strnode.value
32 )
33 except utils.IncompleteFormatString:
34 self.add_message("bad-format-string", node=node)
35 return
36
37 positional_arguments = call_site.positional_arguments
38 named_arguments = call_site.keyword_arguments
39 named_fields = {field[0] for field in fields if isinstance(field[0], str)}
40 if num_args and manual_pos:
41 self.add_message("format-combined-specification", node=node)
42 return
43
44 check_args = False
45 # Consider "{[0]} {[1]}" as num_args.
46 num_args += sum(1 for field in named_fields if not field)
47 if named_fields:
48 for field in named_fields:
49 if field and field not in named_arguments:
50 self.add_message(
51 "missing-format-argument-key", node=node, args=(field,)
52 )
53 for field in named_arguments:
54 if field not in named_fields:
55 self.add_message(
56 "unused-format-string-argument", node=node, args=(field,)
57 )
58 # num_args can be 0 if manual_pos is not.
59 num_args = num_args or manual_pos
60 if positional_arguments or num_args:
61 empty = not all(field for field in named_fields)
62 if named_arguments or empty:
63 # Verify the required number of positional arguments
64 # only if the .format got at least one keyword argument.
65 # This means that the format strings accepts both
66 # positional and named fields and we should warn
67 # when one of them is missing or is extra.
68 check_args = True
69 else:
70 check_args = True
71 if check_args:
72 # num_args can be 0 if manual_pos is not.
73 num_args = num_args or manual_pos
74 if not num_args:
75 self.add_message("format-string-without-interpolation", node=node)
76 return
77 if len(positional_arguments) > num_args:
78 self.add_message("too-many-format-args", node=node)
79 elif len(positional_arguments) < num_args:
80 self.add_message("too-few-format-args", node=node)
81
82 self._detect_vacuous_formatting(node, positional_arguments)
83 self._check_new_format_specifiers(node, fields, named_arguments)
Path 4: 6 calls (0.04)
Call (6)
BoundMethod (6)
1def _check_new_format(self, node: nodes.Call, func: bases.BoundMethod) -> None:
2 """Check the new string formatting."""
3 # Skip format nodes which don't have an explicit string on the
4 # left side of the format operation.
5 # We do this because our inference engine can't properly handle
6 # redefinition of the original string.
7 # Note that there may not be any left side at all, if the format method
8 # has been assigned to another variable. See issue 351. For example:
9 #
10 # fmt = 'some string {}'.format
11 # fmt('arg')
12 if isinstance(node.func, nodes.Attribute) and not isinstance(
13 node.func.expr, nodes.Const
14 ):
15 return
16 if node.starargs or node.kwargs:
17 return
18 try:
19 strnode = next(func.bound.infer())
20 except astroid.InferenceError:
21 return
22 if not (isinstance(strnode, nodes.Const) and isinstance(strnode.value, str)):
23 return
24 try:
25 call_site = astroid.arguments.CallSite.from_call(node)
26 except astroid.InferenceError:
27 return
28
29 try:
30 fields, num_args, manual_pos = utils.parse_format_method_string(
31 strnode.value
32 )
33 except utils.IncompleteFormatString:
34 self.add_message("bad-format-string", node=node)
35 return
36
37 positional_arguments = call_site.positional_arguments
38 named_arguments = call_site.keyword_arguments
39 named_fields = {field[0] for field in fields if isinstance(field[0], str)}
40 if num_args and manual_pos:
41 self.add_message("format-combined-specification", node=node)
42 return
43
44 check_args = False
45 # Consider "{[0]} {[1]}" as num_args.
46 num_args += sum(1 for field in named_fields if not field)
47 if named_fields:
48 for field in named_fields:
49 if field and field not in named_arguments:
50 self.add_message(
51 "missing-format-argument-key", node=node, args=(field,)
52 )
53 for field in named_arguments:
54 if field not in named_fields:
55 self.add_message(
56 "unused-format-string-argument", node=node, args=(field,)
57 )
58 # num_args can be 0 if manual_pos is not.
59 num_args = num_args or manual_pos
60 if positional_arguments or num_args:
61 empty = not all(field for field in named_fields)
62 if named_arguments or empty:
63 # Verify the required number of positional arguments
64 # only if the .format got at least one keyword argument.
65 # This means that the format strings accepts both
66 # positional and named fields and we should warn
67 # when one of them is missing or is extra.
68 check_args = True
69 else:
70 check_args = True
71 if check_args:
72 # num_args can be 0 if manual_pos is not.
73 num_args = num_args or manual_pos
74 if not num_args:
75 self.add_message("format-string-without-interpolation", node=node)
76 return
77 if len(positional_arguments) > num_args:
78 self.add_message("too-many-format-args", node=node)
79 elif len(positional_arguments) < num_args:
80 self.add_message("too-few-format-args", node=node)
81
82 self._detect_vacuous_formatting(node, positional_arguments)
83 self._check_new_format_specifiers(node, fields, named_arguments)
Path 5: 5 calls (0.03)
Call (5)
BoundMethod (5)
None (5)
1def _check_new_format(self, node: nodes.Call, func: bases.BoundMethod) -> None:
2 """Check the new string formatting."""
3 # Skip format nodes which don't have an explicit string on the
4 # left side of the format operation.
5 # We do this because our inference engine can't properly handle
6 # redefinition of the original string.
7 # Note that there may not be any left side at all, if the format method
8 # has been assigned to another variable. See issue 351. For example:
9 #
10 # fmt = 'some string {}'.format
11 # fmt('arg')
12 if isinstance(node.func, nodes.Attribute) and not isinstance(
13 node.func.expr, nodes.Const
14 ):
15 return
16 if node.starargs or node.kwargs:
17 return
18 try:
19 strnode = next(func.bound.infer())
20 except astroid.InferenceError:
21 return
22 if not (isinstance(strnode, nodes.Const) and isinstance(strnode.value, str)):
23 return
24 try:
25 call_site = astroid.arguments.CallSite.from_call(node)
26 except astroid.InferenceError:
27 return
28
29 try:
30 fields, num_args, manual_pos = utils.parse_format_method_string(
31 strnode.value
32 )
33 except utils.IncompleteFormatString:
34 self.add_message("bad-format-string", node=node)
35 return
36
37 positional_arguments = call_site.positional_arguments
38 named_arguments = call_site.keyword_arguments
39 named_fields = {field[0] for field in fields if isinstance(field[0], str)}
40 if num_args and manual_pos:
41 self.add_message("format-combined-specification", node=node)
42 return
43
44 check_args = False
45 # Consider "{[0]} {[1]}" as num_args.
46 num_args += sum(1 for field in named_fields if not field)
47 if named_fields:
48 for field in named_fields:
49 if field and field not in named_arguments:
50 self.add_message(
51 "missing-format-argument-key", node=node, args=(field,)
52 )
53 for field in named_arguments:
54 if field not in named_fields:
55 self.add_message(
56 "unused-format-string-argument", node=node, args=(field,)
57 )
58 # num_args can be 0 if manual_pos is not.
59 num_args = num_args or manual_pos
60 if positional_arguments or num_args:
61 empty = not all(field for field in named_fields)
62 if named_arguments or empty:
63 # Verify the required number of positional arguments
64 # only if the .format got at least one keyword argument.
65 # This means that the format strings accepts both
66 # positional and named fields and we should warn
67 # when one of them is missing or is extra.
68 check_args = True
69 else:
70 check_args = True
71 if check_args:
72 # num_args can be 0 if manual_pos is not.
73 num_args = num_args or manual_pos
74 if not num_args:
75 self.add_message("format-string-without-interpolation", node=node)
76 return
77 if len(positional_arguments) > num_args:
78 self.add_message("too-many-format-args", node=node)
79 elif len(positional_arguments) < num_args:
80 self.add_message("too-few-format-args", node=node)
81
82 self._detect_vacuous_formatting(node, positional_arguments)
83 self._check_new_format_specifiers(node, fields, named_arguments)
Path 6: 5 calls (0.03)
Call (5)
BoundMethod (5)
None (5)
IncompleteFormatString (5)
1def _check_new_format(self, node: nodes.Call, func: bases.BoundMethod) -> None:
2 """Check the new string formatting."""
3 # Skip format nodes which don't have an explicit string on the
4 # left side of the format operation.
5 # We do this because our inference engine can't properly handle
6 # redefinition of the original string.
7 # Note that there may not be any left side at all, if the format method
8 # has been assigned to another variable. See issue 351. For example:
9 #
10 # fmt = 'some string {}'.format
11 # fmt('arg')
12 if isinstance(node.func, nodes.Attribute) and not isinstance(
13 node.func.expr, nodes.Const
14 ):
15 return
16 if node.starargs or node.kwargs:
17 return
18 try:
19 strnode = next(func.bound.infer())
20 except astroid.InferenceError:
21 return
22 if not (isinstance(strnode, nodes.Const) and isinstance(strnode.value, str)):
23 return
24 try:
25 call_site = astroid.arguments.CallSite.from_call(node)
26 except astroid.InferenceError:
27 return
28
29 try:
30 fields, num_args, manual_pos = utils.parse_format_method_string(
31 strnode.value
32 )
33 except utils.IncompleteFormatString:
34 self.add_message("bad-format-string", node=node)
35 return
36
37 positional_arguments = call_site.positional_arguments
38 named_arguments = call_site.keyword_arguments
39 named_fields = {field[0] for field in fields if isinstance(field[0], str)}
40 if num_args and manual_pos:
41 self.add_message("format-combined-specification", node=node)
42 return
43
44 check_args = False
45 # Consider "{[0]} {[1]}" as num_args.
46 num_args += sum(1 for field in named_fields if not field)
47 if named_fields:
48 for field in named_fields:
49 if field and field not in named_arguments:
50 self.add_message(
51 "missing-format-argument-key", node=node, args=(field,)
52 )
53 for field in named_arguments:
54 if field not in named_fields:
55 self.add_message(
56 "unused-format-string-argument", node=node, args=(field,)
57 )
58 # num_args can be 0 if manual_pos is not.
59 num_args = num_args or manual_pos
60 if positional_arguments or num_args:
61 empty = not all(field for field in named_fields)
62 if named_arguments or empty:
63 # Verify the required number of positional arguments
64 # only if the .format got at least one keyword argument.
65 # This means that the format strings accepts both
66 # positional and named fields and we should warn
67 # when one of them is missing or is extra.
68 check_args = True
69 else:
70 check_args = True
71 if check_args:
72 # num_args can be 0 if manual_pos is not.
73 num_args = num_args or manual_pos
74 if not num_args:
75 self.add_message("format-string-without-interpolation", node=node)
76 return
77 if len(positional_arguments) > num_args:
78 self.add_message("too-many-format-args", node=node)
79 elif len(positional_arguments) < num_args:
80 self.add_message("too-few-format-args", node=node)
81
82 self._detect_vacuous_formatting(node, positional_arguments)
83 self._check_new_format_specifiers(node, fields, named_arguments)
Path 7: 5 calls (0.03)
Call (5)
BoundMethod (5)
None (5)
1def _check_new_format(self, node: nodes.Call, func: bases.BoundMethod) -> None:
2 """Check the new string formatting."""
3 # Skip format nodes which don't have an explicit string on the
4 # left side of the format operation.
5 # We do this because our inference engine can't properly handle
6 # redefinition of the original string.
7 # Note that there may not be any left side at all, if the format method
8 # has been assigned to another variable. See issue 351. For example:
9 #
10 # fmt = 'some string {}'.format
11 # fmt('arg')
12 if isinstance(node.func, nodes.Attribute) and not isinstance(
13 node.func.expr, nodes.Const
14 ):
15 return
16 if node.starargs or node.kwargs:
17 return
18 try:
19 strnode = next(func.bound.infer())
20 except astroid.InferenceError:
21 return
22 if not (isinstance(strnode, nodes.Const) and isinstance(strnode.value, str)):
23 return
24 try:
25 call_site = astroid.arguments.CallSite.from_call(node)
26 except astroid.InferenceError:
27 return
28
29 try:
30 fields, num_args, manual_pos = utils.parse_format_method_string(
31 strnode.value
32 )
33 except utils.IncompleteFormatString:
34 self.add_message("bad-format-string", node=node)
35 return
36
37 positional_arguments = call_site.positional_arguments
38 named_arguments = call_site.keyword_arguments
39 named_fields = {field[0] for field in fields if isinstance(field[0], str)}
40 if num_args and manual_pos:
41 self.add_message("format-combined-specification", node=node)
42 return
43
44 check_args = False
45 # Consider "{[0]} {[1]}" as num_args.
46 num_args += sum(1 for field in named_fields if not field)
47 if named_fields:
48 for field in named_fields:
49 if field and field not in named_arguments:
50 self.add_message(
51 "missing-format-argument-key", node=node, args=(field,)
52 )
53 for field in named_arguments:
54 if field not in named_fields:
55 self.add_message(
56 "unused-format-string-argument", node=node, args=(field,)
57 )
58 # num_args can be 0 if manual_pos is not.
59 num_args = num_args or manual_pos
60 if positional_arguments or num_args:
61 empty = not all(field for field in named_fields)
62 if named_arguments or empty:
63 # Verify the required number of positional arguments
64 # only if the .format got at least one keyword argument.
65 # This means that the format strings accepts both
66 # positional and named fields and we should warn
67 # when one of them is missing or is extra.
68 check_args = True
69 else:
70 check_args = True
71 if check_args:
72 # num_args can be 0 if manual_pos is not.
73 num_args = num_args or manual_pos
74 if not num_args:
75 self.add_message("format-string-without-interpolation", node=node)
76 return
77 if len(positional_arguments) > num_args:
78 self.add_message("too-many-format-args", node=node)
79 elif len(positional_arguments) < num_args:
80 self.add_message("too-few-format-args", node=node)
81
82 self._detect_vacuous_formatting(node, positional_arguments)
83 self._check_new_format_specifiers(node, fields, named_arguments)
Path 8: 5 calls (0.03)
Call (5)
BoundMethod (5)
1def _check_new_format(self, node: nodes.Call, func: bases.BoundMethod) -> None:
2 """Check the new string formatting."""
3 # Skip format nodes which don't have an explicit string on the
4 # left side of the format operation.
5 # We do this because our inference engine can't properly handle
6 # redefinition of the original string.
7 # Note that there may not be any left side at all, if the format method
8 # has been assigned to another variable. See issue 351. For example:
9 #
10 # fmt = 'some string {}'.format
11 # fmt('arg')
12 if isinstance(node.func, nodes.Attribute) and not isinstance(
13 node.func.expr, nodes.Const
14 ):
15 return
16 if node.starargs or node.kwargs:
17 return
18 try:
19 strnode = next(func.bound.infer())
20 except astroid.InferenceError:
21 return
22 if not (isinstance(strnode, nodes.Const) and isinstance(strnode.value, str)):
23 return
24 try:
25 call_site = astroid.arguments.CallSite.from_call(node)
26 except astroid.InferenceError:
27 return
28
29 try:
30 fields, num_args, manual_pos = utils.parse_format_method_string(
31 strnode.value
32 )
33 except utils.IncompleteFormatString:
34 self.add_message("bad-format-string", node=node)
35 return
36
37 positional_arguments = call_site.positional_arguments
38 named_arguments = call_site.keyword_arguments
39 named_fields = {field[0] for field in fields if isinstance(field[0], str)}
40 if num_args and manual_pos:
41 self.add_message("format-combined-specification", node=node)
42 return
43
44 check_args = False
45 # Consider "{[0]} {[1]}" as num_args.
46 num_args += sum(1 for field in named_fields if not field)
47 if named_fields:
48 for field in named_fields:
49 if field and field not in named_arguments:
50 self.add_message(
51 "missing-format-argument-key", node=node, args=(field,)
52 )
53 for field in named_arguments:
54 if field not in named_fields:
55 self.add_message(
56 "unused-format-string-argument", node=node, args=(field,)
57 )
58 # num_args can be 0 if manual_pos is not.
59 num_args = num_args or manual_pos
60 if positional_arguments or num_args:
61 empty = not all(field for field in named_fields)
62 if named_arguments or empty:
63 # Verify the required number of positional arguments
64 # only if the .format got at least one keyword argument.
65 # This means that the format strings accepts both
66 # positional and named fields and we should warn
67 # when one of them is missing or is extra.
68 check_args = True
69 else:
70 check_args = True
71 if check_args:
72 # num_args can be 0 if manual_pos is not.
73 num_args = num_args or manual_pos
74 if not num_args:
75 self.add_message("format-string-without-interpolation", node=node)
76 return
77 if len(positional_arguments) > num_args:
78 self.add_message("too-many-format-args", node=node)
79 elif len(positional_arguments) < num_args:
80 self.add_message("too-few-format-args", node=node)
81
82 self._detect_vacuous_formatting(node, positional_arguments)
83 self._check_new_format_specifiers(node, fields, named_arguments)
Path 9: 4 calls (0.03)
Call (4)
BoundMethod (4)
GeneratorExit (4)
1def _check_new_format(self, node: nodes.Call, func: bases.BoundMethod) -> None:
2 """Check the new string formatting."""
3 # Skip format nodes which don't have an explicit string on the
4 # left side of the format operation.
5 # We do this because our inference engine can't properly handle
6 # redefinition of the original string.
7 # Note that there may not be any left side at all, if the format method
8 # has been assigned to another variable. See issue 351. For example:
9 #
10 # fmt = 'some string {}'.format
11 # fmt('arg')
12 if isinstance(node.func, nodes.Attribute) and not isinstance(
13 node.func.expr, nodes.Const
14 ):
15 return
16 if node.starargs or node.kwargs:
17 return
18 try:
19 strnode = next(func.bound.infer())
20 except astroid.InferenceError:
21 return
22 if not (isinstance(strnode, nodes.Const) and isinstance(strnode.value, str)):
23 return
24 try:
25 call_site = astroid.arguments.CallSite.from_call(node)
26 except astroid.InferenceError:
27 return
28
29 try:
30 fields, num_args, manual_pos = utils.parse_format_method_string(
31 strnode.value
32 )
33 except utils.IncompleteFormatString:
34 self.add_message("bad-format-string", node=node)
35 return
36
37 positional_arguments = call_site.positional_arguments
38 named_arguments = call_site.keyword_arguments
39 named_fields = {field[0] for field in fields if isinstance(field[0], str)}
40 if num_args and manual_pos:
41 self.add_message("format-combined-specification", node=node)
42 return
43
44 check_args = False
45 # Consider "{[0]} {[1]}" as num_args.
46 num_args += sum(1 for field in named_fields if not field)
47 if named_fields:
48 for field in named_fields:
49 if field and field not in named_arguments:
50 self.add_message(
51 "missing-format-argument-key", node=node, args=(field,)
52 )
53 for field in named_arguments:
54 if field not in named_fields:
55 self.add_message(
56 "unused-format-string-argument", node=node, args=(field,)
57 )
58 # num_args can be 0 if manual_pos is not.
59 num_args = num_args or manual_pos
60 if positional_arguments or num_args:
61 empty = not all(field for field in named_fields)
62 if named_arguments or empty:
63 # Verify the required number of positional arguments
64 # only if the .format got at least one keyword argument.
65 # This means that the format strings accepts both
66 # positional and named fields and we should warn
67 # when one of them is missing or is extra.
68 check_args = True
69 else:
70 check_args = True
71 if check_args:
72 # num_args can be 0 if manual_pos is not.
73 num_args = num_args or manual_pos
74 if not num_args:
75 self.add_message("format-string-without-interpolation", node=node)
76 return
77 if len(positional_arguments) > num_args:
78 self.add_message("too-many-format-args", node=node)
79 elif len(positional_arguments) < num_args:
80 self.add_message("too-few-format-args", node=node)
81
82 self._detect_vacuous_formatting(node, positional_arguments)
83 self._check_new_format_specifiers(node, fields, named_arguments)
Path 10: 4 calls (0.03)
Call (4)
BoundMethod (4)
1def _check_new_format(self, node: nodes.Call, func: bases.BoundMethod) -> None:
2 """Check the new string formatting."""
3 # Skip format nodes which don't have an explicit string on the
4 # left side of the format operation.
5 # We do this because our inference engine can't properly handle
6 # redefinition of the original string.
7 # Note that there may not be any left side at all, if the format method
8 # has been assigned to another variable. See issue 351. For example:
9 #
10 # fmt = 'some string {}'.format
11 # fmt('arg')
12 if isinstance(node.func, nodes.Attribute) and not isinstance(
13 node.func.expr, nodes.Const
14 ):
15 return
16 if node.starargs or node.kwargs:
17 return
18 try:
19 strnode = next(func.bound.infer())
20 except astroid.InferenceError:
21 return
22 if not (isinstance(strnode, nodes.Const) and isinstance(strnode.value, str)):
23 return
24 try:
25 call_site = astroid.arguments.CallSite.from_call(node)
26 except astroid.InferenceError:
27 return
28
29 try:
30 fields, num_args, manual_pos = utils.parse_format_method_string(
31 strnode.value
32 )
33 except utils.IncompleteFormatString:
34 self.add_message("bad-format-string", node=node)
35 return
36
37 positional_arguments = call_site.positional_arguments
38 named_arguments = call_site.keyword_arguments
39 named_fields = {field[0] for field in fields if isinstance(field[0], str)}
40 if num_args and manual_pos:
41 self.add_message("format-combined-specification", node=node)
42 return
43
44 check_args = False
45 # Consider "{[0]} {[1]}" as num_args.
46 num_args += sum(1 for field in named_fields if not field)
47 if named_fields:
48 for field in named_fields:
49 if field and field not in named_arguments:
50 self.add_message(
51 "missing-format-argument-key", node=node, args=(field,)
52 )
53 for field in named_arguments:
54 if field not in named_fields:
55 self.add_message(
56 "unused-format-string-argument", node=node, args=(field,)
57 )
58 # num_args can be 0 if manual_pos is not.
59 num_args = num_args or manual_pos
60 if positional_arguments or num_args:
61 empty = not all(field for field in named_fields)
62 if named_arguments or empty:
63 # Verify the required number of positional arguments
64 # only if the .format got at least one keyword argument.
65 # This means that the format strings accepts both
66 # positional and named fields and we should warn
67 # when one of them is missing or is extra.
68 check_args = True
69 else:
70 check_args = True
71 if check_args:
72 # num_args can be 0 if manual_pos is not.
73 num_args = num_args or manual_pos
74 if not num_args:
75 self.add_message("format-string-without-interpolation", node=node)
76 return
77 if len(positional_arguments) > num_args:
78 self.add_message("too-many-format-args", node=node)
79 elif len(positional_arguments) < num_args:
80 self.add_message("too-few-format-args", node=node)
81
82 self._detect_vacuous_formatting(node, positional_arguments)
83 self._check_new_format_specifiers(node, fields, named_arguments)
Path 11: 2 calls (0.01)
Call (2)
BoundMethod (2)
1def _check_new_format(self, node: nodes.Call, func: bases.BoundMethod) -> None:
2 """Check the new string formatting."""
3 # Skip format nodes which don't have an explicit string on the
4 # left side of the format operation.
5 # We do this because our inference engine can't properly handle
6 # redefinition of the original string.
7 # Note that there may not be any left side at all, if the format method
8 # has been assigned to another variable. See issue 351. For example:
9 #
10 # fmt = 'some string {}'.format
11 # fmt('arg')
12 if isinstance(node.func, nodes.Attribute) and not isinstance(
13 node.func.expr, nodes.Const
14 ):
15 return
16 if node.starargs or node.kwargs:
17 return
18 try:
19 strnode = next(func.bound.infer())
20 except astroid.InferenceError:
21 return
22 if not (isinstance(strnode, nodes.Const) and isinstance(strnode.value, str)):
23 return
24 try:
25 call_site = astroid.arguments.CallSite.from_call(node)
26 except astroid.InferenceError:
27 return
28
29 try:
30 fields, num_args, manual_pos = utils.parse_format_method_string(
31 strnode.value
32 )
33 except utils.IncompleteFormatString:
34 self.add_message("bad-format-string", node=node)
35 return
36
37 positional_arguments = call_site.positional_arguments
38 named_arguments = call_site.keyword_arguments
39 named_fields = {field[0] for field in fields if isinstance(field[0], str)}
40 if num_args and manual_pos:
41 self.add_message("format-combined-specification", node=node)
42 return
43
44 check_args = False
45 # Consider "{[0]} {[1]}" as num_args.
46 num_args += sum(1 for field in named_fields if not field)
47 if named_fields:
48 for field in named_fields:
49 if field and field not in named_arguments:
50 self.add_message(
51 "missing-format-argument-key", node=node, args=(field,)
52 )
53 for field in named_arguments:
54 if field not in named_fields:
55 self.add_message(
56 "unused-format-string-argument", node=node, args=(field,)
57 )
58 # num_args can be 0 if manual_pos is not.
59 num_args = num_args or manual_pos
60 if positional_arguments or num_args:
61 empty = not all(field for field in named_fields)
62 if named_arguments or empty:
63 # Verify the required number of positional arguments
64 # only if the .format got at least one keyword argument.
65 # This means that the format strings accepts both
66 # positional and named fields and we should warn
67 # when one of them is missing or is extra.
68 check_args = True
69 else:
70 check_args = True
71 if check_args:
72 # num_args can be 0 if manual_pos is not.
73 num_args = num_args or manual_pos
74 if not num_args:
75 self.add_message("format-string-without-interpolation", node=node)
76 return
77 if len(positional_arguments) > num_args:
78 self.add_message("too-many-format-args", node=node)
79 elif len(positional_arguments) < num_args:
80 self.add_message("too-few-format-args", node=node)
81
82 self._detect_vacuous_formatting(node, positional_arguments)
83 self._check_new_format_specifiers(node, fields, named_arguments)
Path 12: 2 calls (0.01)
Call (2)
BoundMethod (2)
1def _check_new_format(self, node: nodes.Call, func: bases.BoundMethod) -> None:
2 """Check the new string formatting."""
3 # Skip format nodes which don't have an explicit string on the
4 # left side of the format operation.
5 # We do this because our inference engine can't properly handle
6 # redefinition of the original string.
7 # Note that there may not be any left side at all, if the format method
8 # has been assigned to another variable. See issue 351. For example:
9 #
10 # fmt = 'some string {}'.format
11 # fmt('arg')
12 if isinstance(node.func, nodes.Attribute) and not isinstance(
13 node.func.expr, nodes.Const
14 ):
15 return
16 if node.starargs or node.kwargs:
17 return
18 try:
19 strnode = next(func.bound.infer())
20 except astroid.InferenceError:
21 return
22 if not (isinstance(strnode, nodes.Const) and isinstance(strnode.value, str)):
23 return
24 try:
25 call_site = astroid.arguments.CallSite.from_call(node)
26 except astroid.InferenceError:
27 return
28
29 try:
30 fields, num_args, manual_pos = utils.parse_format_method_string(
31 strnode.value
32 )
33 except utils.IncompleteFormatString:
34 self.add_message("bad-format-string", node=node)
35 return
36
37 positional_arguments = call_site.positional_arguments
38 named_arguments = call_site.keyword_arguments
39 named_fields = {field[0] for field in fields if isinstance(field[0], str)}
40 if num_args and manual_pos:
41 self.add_message("format-combined-specification", node=node)
42 return
43
44 check_args = False
45 # Consider "{[0]} {[1]}" as num_args.
46 num_args += sum(1 for field in named_fields if not field)
47 if named_fields:
48 for field in named_fields:
49 if field and field not in named_arguments:
50 self.add_message(
51 "missing-format-argument-key", node=node, args=(field,)
52 )
53 for field in named_arguments:
54 if field not in named_fields:
55 self.add_message(
56 "unused-format-string-argument", node=node, args=(field,)
57 )
58 # num_args can be 0 if manual_pos is not.
59 num_args = num_args or manual_pos
60 if positional_arguments or num_args:
61 empty = not all(field for field in named_fields)
62 if named_arguments or empty:
63 # Verify the required number of positional arguments
64 # only if the .format got at least one keyword argument.
65 # This means that the format strings accepts both
66 # positional and named fields and we should warn
67 # when one of them is missing or is extra.
68 check_args = True
69 else:
70 check_args = True
71 if check_args:
72 # num_args can be 0 if manual_pos is not.
73 num_args = num_args or manual_pos
74 if not num_args:
75 self.add_message("format-string-without-interpolation", node=node)
76 return
77 if len(positional_arguments) > num_args:
78 self.add_message("too-many-format-args", node=node)
79 elif len(positional_arguments) < num_args:
80 self.add_message("too-few-format-args", node=node)
81
82 self._detect_vacuous_formatting(node, positional_arguments)
83 self._check_new_format_specifiers(node, fields, named_arguments)
Path 13: 1 calls (0.01)
Call (1)
BoundMethod (1)
None (1)
1def _check_new_format(self, node: nodes.Call, func: bases.BoundMethod) -> None:
2 """Check the new string formatting."""
3 # Skip format nodes which don't have an explicit string on the
4 # left side of the format operation.
5 # We do this because our inference engine can't properly handle
6 # redefinition of the original string.
7 # Note that there may not be any left side at all, if the format method
8 # has been assigned to another variable. See issue 351. For example:
9 #
10 # fmt = 'some string {}'.format
11 # fmt('arg')
12 if isinstance(node.func, nodes.Attribute) and not isinstance(
13 node.func.expr, nodes.Const
14 ):
15 return
16 if node.starargs or node.kwargs:
17 return
18 try:
19 strnode = next(func.bound.infer())
20 except astroid.InferenceError:
21 return
22 if not (isinstance(strnode, nodes.Const) and isinstance(strnode.value, str)):
23 return
24 try:
25 call_site = astroid.arguments.CallSite.from_call(node)
26 except astroid.InferenceError:
27 return
28
29 try:
30 fields, num_args, manual_pos = utils.parse_format_method_string(
31 strnode.value
32 )
33 except utils.IncompleteFormatString:
34 self.add_message("bad-format-string", node=node)
35 return
36
37 positional_arguments = call_site.positional_arguments
38 named_arguments = call_site.keyword_arguments
39 named_fields = {field[0] for field in fields if isinstance(field[0], str)}
40 if num_args and manual_pos:
41 self.add_message("format-combined-specification", node=node)
42 return
43
44 check_args = False
45 # Consider "{[0]} {[1]}" as num_args.
46 num_args += sum(1 for field in named_fields if not field)
47 if named_fields:
48 for field in named_fields:
49 if field and field not in named_arguments:
50 self.add_message(
51 "missing-format-argument-key", node=node, args=(field,)
52 )
53 for field in named_arguments:
54 if field not in named_fields:
55 self.add_message(
56 "unused-format-string-argument", node=node, args=(field,)
57 )
58 # num_args can be 0 if manual_pos is not.
59 num_args = num_args or manual_pos
60 if positional_arguments or num_args:
61 empty = not all(field for field in named_fields)
62 if named_arguments or empty:
63 # Verify the required number of positional arguments
64 # only if the .format got at least one keyword argument.
65 # This means that the format strings accepts both
66 # positional and named fields and we should warn
67 # when one of them is missing or is extra.
68 check_args = True
69 else:
70 check_args = True
71 if check_args:
72 # num_args can be 0 if manual_pos is not.
73 num_args = num_args or manual_pos
74 if not num_args:
75 self.add_message("format-string-without-interpolation", node=node)
76 return
77 if len(positional_arguments) > num_args:
78 self.add_message("too-many-format-args", node=node)
79 elif len(positional_arguments) < num_args:
80 self.add_message("too-few-format-args", node=node)
81
82 self._detect_vacuous_formatting(node, positional_arguments)
83 self._check_new_format_specifiers(node, fields, named_arguments)
Path 14: 1 calls (0.01)
Call (1)
BoundMethod (1)
1def _check_new_format(self, node: nodes.Call, func: bases.BoundMethod) -> None:
2 """Check the new string formatting."""
3 # Skip format nodes which don't have an explicit string on the
4 # left side of the format operation.
5 # We do this because our inference engine can't properly handle
6 # redefinition of the original string.
7 # Note that there may not be any left side at all, if the format method
8 # has been assigned to another variable. See issue 351. For example:
9 #
10 # fmt = 'some string {}'.format
11 # fmt('arg')
12 if isinstance(node.func, nodes.Attribute) and not isinstance(
13 node.func.expr, nodes.Const
14 ):
15 return
16 if node.starargs or node.kwargs:
17 return
18 try:
19 strnode = next(func.bound.infer())
20 except astroid.InferenceError:
21 return
22 if not (isinstance(strnode, nodes.Const) and isinstance(strnode.value, str)):
23 return
24 try:
25 call_site = astroid.arguments.CallSite.from_call(node)
26 except astroid.InferenceError:
27 return
28
29 try:
30 fields, num_args, manual_pos = utils.parse_format_method_string(
31 strnode.value
32 )
33 except utils.IncompleteFormatString:
34 self.add_message("bad-format-string", node=node)
35 return
36
37 positional_arguments = call_site.positional_arguments
38 named_arguments = call_site.keyword_arguments
39 named_fields = {field[0] for field in fields if isinstance(field[0], str)}
40 if num_args and manual_pos:
41 self.add_message("format-combined-specification", node=node)
42 return
43
44 check_args = False
45 # Consider "{[0]} {[1]}" as num_args.
46 num_args += sum(1 for field in named_fields if not field)
47 if named_fields:
48 for field in named_fields:
49 if field and field not in named_arguments:
50 self.add_message(
51 "missing-format-argument-key", node=node, args=(field,)
52 )
53 for field in named_arguments:
54 if field not in named_fields:
55 self.add_message(
56 "unused-format-string-argument", node=node, args=(field,)
57 )
58 # num_args can be 0 if manual_pos is not.
59 num_args = num_args or manual_pos
60 if positional_arguments or num_args:
61 empty = not all(field for field in named_fields)
62 if named_arguments or empty:
63 # Verify the required number of positional arguments
64 # only if the .format got at least one keyword argument.
65 # This means that the format strings accepts both
66 # positional and named fields and we should warn
67 # when one of them is missing or is extra.
68 check_args = True
69 else:
70 check_args = True
71 if check_args:
72 # num_args can be 0 if manual_pos is not.
73 num_args = num_args or manual_pos
74 if not num_args:
75 self.add_message("format-string-without-interpolation", node=node)
76 return
77 if len(positional_arguments) > num_args:
78 self.add_message("too-many-format-args", node=node)
79 elif len(positional_arguments) < num_args:
80 self.add_message("too-few-format-args", node=node)
81
82 self._detect_vacuous_formatting(node, positional_arguments)
83 self._check_new_format_specifiers(node, fields, named_arguments)
Path 15: 1 calls (0.01)
Call (1)
BoundMethod (1)
1def _check_new_format(self, node: nodes.Call, func: bases.BoundMethod) -> None:
2 """Check the new string formatting."""
3 # Skip format nodes which don't have an explicit string on the
4 # left side of the format operation.
5 # We do this because our inference engine can't properly handle
6 # redefinition of the original string.
7 # Note that there may not be any left side at all, if the format method
8 # has been assigned to another variable. See issue 351. For example:
9 #
10 # fmt = 'some string {}'.format
11 # fmt('arg')
12 if isinstance(node.func, nodes.Attribute) and not isinstance(
13 node.func.expr, nodes.Const
14 ):
15 return
16 if node.starargs or node.kwargs:
17 return
18 try:
19 strnode = next(func.bound.infer())
20 except astroid.InferenceError:
21 return
22 if not (isinstance(strnode, nodes.Const) and isinstance(strnode.value, str)):
23 return
24 try:
25 call_site = astroid.arguments.CallSite.from_call(node)
26 except astroid.InferenceError:
27 return
28
29 try:
30 fields, num_args, manual_pos = utils.parse_format_method_string(
31 strnode.value
32 )
33 except utils.IncompleteFormatString:
34 self.add_message("bad-format-string", node=node)
35 return
36
37 positional_arguments = call_site.positional_arguments
38 named_arguments = call_site.keyword_arguments
39 named_fields = {field[0] for field in fields if isinstance(field[0], str)}
40 if num_args and manual_pos:
41 self.add_message("format-combined-specification", node=node)
42 return
43
44 check_args = False
45 # Consider "{[0]} {[1]}" as num_args.
46 num_args += sum(1 for field in named_fields if not field)
47 if named_fields:
48 for field in named_fields:
49 if field and field not in named_arguments:
50 self.add_message(
51 "missing-format-argument-key", node=node, args=(field,)
52 )
53 for field in named_arguments:
54 if field not in named_fields:
55 self.add_message(
56 "unused-format-string-argument", node=node, args=(field,)
57 )
58 # num_args can be 0 if manual_pos is not.
59 num_args = num_args or manual_pos
60 if positional_arguments or num_args:
61 empty = not all(field for field in named_fields)
62 if named_arguments or empty:
63 # Verify the required number of positional arguments
64 # only if the .format got at least one keyword argument.
65 # This means that the format strings accepts both
66 # positional and named fields and we should warn
67 # when one of them is missing or is extra.
68 check_args = True
69 else:
70 check_args = True
71 if check_args:
72 # num_args can be 0 if manual_pos is not.
73 num_args = num_args or manual_pos
74 if not num_args:
75 self.add_message("format-string-without-interpolation", node=node)
76 return
77 if len(positional_arguments) > num_args:
78 self.add_message("too-many-format-args", node=node)
79 elif len(positional_arguments) < num_args:
80 self.add_message("too-few-format-args", node=node)
81
82 self._detect_vacuous_formatting(node, positional_arguments)
83 self._check_new_format_specifiers(node, fields, named_arguments)
Path 16: 1 calls (0.01)
Call (1)
BoundMethod (1)
GeneratorExit (1)
1def _check_new_format(self, node: nodes.Call, func: bases.BoundMethod) -> None:
2 """Check the new string formatting."""
3 # Skip format nodes which don't have an explicit string on the
4 # left side of the format operation.
5 # We do this because our inference engine can't properly handle
6 # redefinition of the original string.
7 # Note that there may not be any left side at all, if the format method
8 # has been assigned to another variable. See issue 351. For example:
9 #
10 # fmt = 'some string {}'.format
11 # fmt('arg')
12 if isinstance(node.func, nodes.Attribute) and not isinstance(
13 node.func.expr, nodes.Const
14 ):
15 return
16 if node.starargs or node.kwargs:
17 return
18 try:
19 strnode = next(func.bound.infer())
20 except astroid.InferenceError:
21 return
22 if not (isinstance(strnode, nodes.Const) and isinstance(strnode.value, str)):
23 return
24 try:
25 call_site = astroid.arguments.CallSite.from_call(node)
26 except astroid.InferenceError:
27 return
28
29 try:
30 fields, num_args, manual_pos = utils.parse_format_method_string(
31 strnode.value
32 )
33 except utils.IncompleteFormatString:
34 self.add_message("bad-format-string", node=node)
35 return
36
37 positional_arguments = call_site.positional_arguments
38 named_arguments = call_site.keyword_arguments
39 named_fields = {field[0] for field in fields if isinstance(field[0], str)}
40 if num_args and manual_pos:
41 self.add_message("format-combined-specification", node=node)
42 return
43
44 check_args = False
45 # Consider "{[0]} {[1]}" as num_args.
46 num_args += sum(1 for field in named_fields if not field)
47 if named_fields:
48 for field in named_fields:
49 if field and field not in named_arguments:
50 self.add_message(
51 "missing-format-argument-key", node=node, args=(field,)
52 )
53 for field in named_arguments:
54 if field not in named_fields:
55 self.add_message(
56 "unused-format-string-argument", node=node, args=(field,)
57 )
58 # num_args can be 0 if manual_pos is not.
59 num_args = num_args or manual_pos
60 if positional_arguments or num_args:
61 empty = not all(field for field in named_fields)
62 if named_arguments or empty:
63 # Verify the required number of positional arguments
64 # only if the .format got at least one keyword argument.
65 # This means that the format strings accepts both
66 # positional and named fields and we should warn
67 # when one of them is missing or is extra.
68 check_args = True
69 else:
70 check_args = True
71 if check_args:
72 # num_args can be 0 if manual_pos is not.
73 num_args = num_args or manual_pos
74 if not num_args:
75 self.add_message("format-string-without-interpolation", node=node)
76 return
77 if len(positional_arguments) > num_args:
78 self.add_message("too-many-format-args", node=node)
79 elif len(positional_arguments) < num_args:
80 self.add_message("too-few-format-args", node=node)
81
82 self._detect_vacuous_formatting(node, positional_arguments)
83 self._check_new_format_specifiers(node, fields, named_arguments)
Path 17: 1 calls (0.01)
Call (1)
BoundMethod (1)
GeneratorExit (1)
1def _check_new_format(self, node: nodes.Call, func: bases.BoundMethod) -> None:
2 """Check the new string formatting."""
3 # Skip format nodes which don't have an explicit string on the
4 # left side of the format operation.
5 # We do this because our inference engine can't properly handle
6 # redefinition of the original string.
7 # Note that there may not be any left side at all, if the format method
8 # has been assigned to another variable. See issue 351. For example:
9 #
10 # fmt = 'some string {}'.format
11 # fmt('arg')
12 if isinstance(node.func, nodes.Attribute) and not isinstance(
13 node.func.expr, nodes.Const
14 ):
15 return
16 if node.starargs or node.kwargs:
17 return
18 try:
19 strnode = next(func.bound.infer())
20 except astroid.InferenceError:
21 return
22 if not (isinstance(strnode, nodes.Const) and isinstance(strnode.value, str)):
23 return
24 try:
25 call_site = astroid.arguments.CallSite.from_call(node)
26 except astroid.InferenceError:
27 return
28
29 try:
30 fields, num_args, manual_pos = utils.parse_format_method_string(
31 strnode.value
32 )
33 except utils.IncompleteFormatString:
34 self.add_message("bad-format-string", node=node)
35 return
36
37 positional_arguments = call_site.positional_arguments
38 named_arguments = call_site.keyword_arguments
39 named_fields = {field[0] for field in fields if isinstance(field[0], str)}
40 if num_args and manual_pos:
41 self.add_message("format-combined-specification", node=node)
42 return
43
44 check_args = False
45 # Consider "{[0]} {[1]}" as num_args.
46 num_args += sum(1 for field in named_fields if not field)
47 if named_fields:
48 for field in named_fields:
49 if field and field not in named_arguments:
50 self.add_message(
51 "missing-format-argument-key", node=node, args=(field,)
52 )
53 for field in named_arguments:
54 if field not in named_fields:
55 self.add_message(
56 "unused-format-string-argument", node=node, args=(field,)
57 )
58 # num_args can be 0 if manual_pos is not.
59 num_args = num_args or manual_pos
60 if positional_arguments or num_args:
61 empty = not all(field for field in named_fields)
62 if named_arguments or empty:
63 # Verify the required number of positional arguments
64 # only if the .format got at least one keyword argument.
65 # This means that the format strings accepts both
66 # positional and named fields and we should warn
67 # when one of them is missing or is extra.
68 check_args = True
69 else:
70 check_args = True
71 if check_args:
72 # num_args can be 0 if manual_pos is not.
73 num_args = num_args or manual_pos
74 if not num_args:
75 self.add_message("format-string-without-interpolation", node=node)
76 return
77 if len(positional_arguments) > num_args:
78 self.add_message("too-many-format-args", node=node)
79 elif len(positional_arguments) < num_args:
80 self.add_message("too-few-format-args", node=node)
81
82 self._detect_vacuous_formatting(node, positional_arguments)
83 self._check_new_format_specifiers(node, fields, named_arguments)
Path 18: 1 calls (0.01)
Call (1)
BoundMethod (1)
1def _check_new_format(self, node: nodes.Call, func: bases.BoundMethod) -> None:
2 """Check the new string formatting."""
3 # Skip format nodes which don't have an explicit string on the
4 # left side of the format operation.
5 # We do this because our inference engine can't properly handle
6 # redefinition of the original string.
7 # Note that there may not be any left side at all, if the format method
8 # has been assigned to another variable. See issue 351. For example:
9 #
10 # fmt = 'some string {}'.format
11 # fmt('arg')
12 if isinstance(node.func, nodes.Attribute) and not isinstance(
13 node.func.expr, nodes.Const
14 ):
15 return
16 if node.starargs or node.kwargs:
17 return
18 try:
19 strnode = next(func.bound.infer())
20 except astroid.InferenceError:
21 return
22 if not (isinstance(strnode, nodes.Const) and isinstance(strnode.value, str)):
23 return
24 try:
25 call_site = astroid.arguments.CallSite.from_call(node)
26 except astroid.InferenceError:
27 return
28
29 try:
30 fields, num_args, manual_pos = utils.parse_format_method_string(
31 strnode.value
32 )
33 except utils.IncompleteFormatString:
34 self.add_message("bad-format-string", node=node)
35 return
36
37 positional_arguments = call_site.positional_arguments
38 named_arguments = call_site.keyword_arguments
39 named_fields = {field[0] for field in fields if isinstance(field[0], str)}
40 if num_args and manual_pos:
41 self.add_message("format-combined-specification", node=node)
42 return
43
44 check_args = False
45 # Consider "{[0]} {[1]}" as num_args.
46 num_args += sum(1 for field in named_fields if not field)
47 if named_fields:
48 for field in named_fields:
49 if field and field not in named_arguments:
50 self.add_message(
51 "missing-format-argument-key", node=node, args=(field,)
52 )
53 for field in named_arguments:
54 if field not in named_fields:
55 self.add_message(
56 "unused-format-string-argument", node=node, args=(field,)
57 )
58 # num_args can be 0 if manual_pos is not.
59 num_args = num_args or manual_pos
60 if positional_arguments or num_args:
61 empty = not all(field for field in named_fields)
62 if named_arguments or empty:
63 # Verify the required number of positional arguments
64 # only if the .format got at least one keyword argument.
65 # This means that the format strings accepts both
66 # positional and named fields and we should warn
67 # when one of them is missing or is extra.
68 check_args = True
69 else:
70 check_args = True
71 if check_args:
72 # num_args can be 0 if manual_pos is not.
73 num_args = num_args or manual_pos
74 if not num_args:
75 self.add_message("format-string-without-interpolation", node=node)
76 return
77 if len(positional_arguments) > num_args:
78 self.add_message("too-many-format-args", node=node)
79 elif len(positional_arguments) < num_args:
80 self.add_message("too-few-format-args", node=node)
81
82 self._detect_vacuous_formatting(node, positional_arguments)
83 self._check_new_format_specifiers(node, fields, named_arguments)
Path 19: 1 calls (0.01)
Call (1)
BoundMethod (1)
1def _check_new_format(self, node: nodes.Call, func: bases.BoundMethod) -> None:
2 """Check the new string formatting."""
3 # Skip format nodes which don't have an explicit string on the
4 # left side of the format operation.
5 # We do this because our inference engine can't properly handle
6 # redefinition of the original string.
7 # Note that there may not be any left side at all, if the format method
8 # has been assigned to another variable. See issue 351. For example:
9 #
10 # fmt = 'some string {}'.format
11 # fmt('arg')
12 if isinstance(node.func, nodes.Attribute) and not isinstance(
13 node.func.expr, nodes.Const
14 ):
15 return
16 if node.starargs or node.kwargs:
17 return
18 try:
19 strnode = next(func.bound.infer())
20 except astroid.InferenceError:
21 return
22 if not (isinstance(strnode, nodes.Const) and isinstance(strnode.value, str)):
23 return
24 try:
25 call_site = astroid.arguments.CallSite.from_call(node)
26 except astroid.InferenceError:
27 return
28
29 try:
30 fields, num_args, manual_pos = utils.parse_format_method_string(
31 strnode.value
32 )
33 except utils.IncompleteFormatString:
34 self.add_message("bad-format-string", node=node)
35 return
36
37 positional_arguments = call_site.positional_arguments
38 named_arguments = call_site.keyword_arguments
39 named_fields = {field[0] for field in fields if isinstance(field[0], str)}
40 if num_args and manual_pos:
41 self.add_message("format-combined-specification", node=node)
42 return
43
44 check_args = False
45 # Consider "{[0]} {[1]}" as num_args.
46 num_args += sum(1 for field in named_fields if not field)
47 if named_fields:
48 for field in named_fields:
49 if field and field not in named_arguments:
50 self.add_message(
51 "missing-format-argument-key", node=node, args=(field,)
52 )
53 for field in named_arguments:
54 if field not in named_fields:
55 self.add_message(
56 "unused-format-string-argument", node=node, args=(field,)
57 )
58 # num_args can be 0 if manual_pos is not.
59 num_args = num_args or manual_pos
60 if positional_arguments or num_args:
61 empty = not all(field for field in named_fields)
62 if named_arguments or empty:
63 # Verify the required number of positional arguments
64 # only if the .format got at least one keyword argument.
65 # This means that the format strings accepts both
66 # positional and named fields and we should warn
67 # when one of them is missing or is extra.
68 check_args = True
69 else:
70 check_args = True
71 if check_args:
72 # num_args can be 0 if manual_pos is not.
73 num_args = num_args or manual_pos
74 if not num_args:
75 self.add_message("format-string-without-interpolation", node=node)
76 return
77 if len(positional_arguments) > num_args:
78 self.add_message("too-many-format-args", node=node)
79 elif len(positional_arguments) < num_args:
80 self.add_message("too-few-format-args", node=node)
81
82 self._detect_vacuous_formatting(node, positional_arguments)
83 self._check_new_format_specifiers(node, fields, named_arguments)
Path 20: 1 calls (0.01)
Call (1)
BoundMethod (1)
1def _check_new_format(self, node: nodes.Call, func: bases.BoundMethod) -> None:
2 """Check the new string formatting."""
3 # Skip format nodes which don't have an explicit string on the
4 # left side of the format operation.
5 # We do this because our inference engine can't properly handle
6 # redefinition of the original string.
7 # Note that there may not be any left side at all, if the format method
8 # has been assigned to another variable. See issue 351. For example:
9 #
10 # fmt = 'some string {}'.format
11 # fmt('arg')
12 if isinstance(node.func, nodes.Attribute) and not isinstance(
13 node.func.expr, nodes.Const
14 ):
15 return
16 if node.starargs or node.kwargs:
17 return
18 try:
19 strnode = next(func.bound.infer())
20 except astroid.InferenceError:
21 return
22 if not (isinstance(strnode, nodes.Const) and isinstance(strnode.value, str)):
23 return
24 try:
25 call_site = astroid.arguments.CallSite.from_call(node)
26 except astroid.InferenceError:
27 return
28
29 try:
30 fields, num_args, manual_pos = utils.parse_format_method_string(
31 strnode.value
32 )
33 except utils.IncompleteFormatString:
34 self.add_message("bad-format-string", node=node)
35 return
36
37 positional_arguments = call_site.positional_arguments
38 named_arguments = call_site.keyword_arguments
39 named_fields = {field[0] for field in fields if isinstance(field[0], str)}
40 if num_args and manual_pos:
41 self.add_message("format-combined-specification", node=node)
42 return
43
44 check_args = False
45 # Consider "{[0]} {[1]}" as num_args.
46 num_args += sum(1 for field in named_fields if not field)
47 if named_fields:
48 for field in named_fields:
49 if field and field not in named_arguments:
50 self.add_message(
51 "missing-format-argument-key", node=node, args=(field,)
52 )
53 for field in named_arguments:
54 if field not in named_fields:
55 self.add_message(
56 "unused-format-string-argument", node=node, args=(field,)
57 )
58 # num_args can be 0 if manual_pos is not.
59 num_args = num_args or manual_pos
60 if positional_arguments or num_args:
61 empty = not all(field for field in named_fields)
62 if named_arguments or empty:
63 # Verify the required number of positional arguments
64 # only if the .format got at least one keyword argument.
65 # This means that the format strings accepts both
66 # positional and named fields and we should warn
67 # when one of them is missing or is extra.
68 check_args = True
69 else:
70 check_args = True
71 if check_args:
72 # num_args can be 0 if manual_pos is not.
73 num_args = num_args or manual_pos
74 if not num_args:
75 self.add_message("format-string-without-interpolation", node=node)
76 return
77 if len(positional_arguments) > num_args:
78 self.add_message("too-many-format-args", node=node)
79 elif len(positional_arguments) < num_args:
80 self.add_message("too-few-format-args", node=node)
81
82 self._detect_vacuous_formatting(node, positional_arguments)
83 self._check_new_format_specifiers(node, fields, named_arguments)