Method: pylint.checkers.variables.VariablesChecker._check_is_unused
Calls: 1454, Exceptions: 8, Paths: 21Back
Path 1: 981 calls (0.67)
'self' (598) 'cls' (52) 'arg' (23) 'arg1' (22) 'kwargs' (18) 'arg2' (15) 'args' (12) 'x' (12) 'y' (12) 'name' (10)
FunctionDef (971) AsyncFunctionDef (10)
AssignName (950) Arguments (31)
set() (981)
set() (979) {'some_attr'} (1) {'initial'} (1)
set() (980) {'mauris'} (1)
1def _check_is_unused(
2 self,
3 name: str,
4 node: nodes.FunctionDef,
5 stmt: nodes.NodeNG,
6 global_names: set[str],
7 nonlocal_names: Iterable[str],
8 comprehension_target_names: Iterable[str],
9 ) -> None:
10 # Ignore some special names specified by user configuration.
11 if self._is_name_ignored(stmt, name):
12 return
13 # Ignore names that were added dynamically to the Function scope
14 if (
15 isinstance(node, nodes.FunctionDef)
16 and name == "__class__"
17 and len(node.locals["__class__"]) == 1
18 and isinstance(node.locals["__class__"][0], nodes.ClassDef)
19 ):
20 return
21
22 # Ignore names imported by the global statement.
23 if isinstance(stmt, (nodes.Global, nodes.Import, nodes.ImportFrom)):
24 # Detect imports, assigned to global statements.
25 if global_names and _import_name_is_global(stmt, global_names):
26 return
27
28 # Ignore names in comprehension targets
29 if name in comprehension_target_names:
30 return
31
32 # Ignore names in string literal type annotation.
33 if name in self._type_annotation_names:
34 return
35
36 argnames = node.argnames()
37 # Care about functions with unknown argument (builtins)
38 if name in argnames:
39 self._check_unused_arguments(name, node, stmt, argnames, nonlocal_names)
40 else:
41 if stmt.parent and isinstance(
42 stmt.parent, (nodes.Assign, nodes.AnnAssign, nodes.Tuple, nodes.For)
43 ):
44 if name in nonlocal_names:
45 return
46
47 qname = asname = None
48 if isinstance(stmt, (nodes.Import, nodes.ImportFrom)):
49 # Need the complete name, which we don't have in .locals.
50 if len(stmt.names) > 1:
51 import_names = next(
52 (names for names in stmt.names if name in names), None
53 )
54 else:
55 import_names = stmt.names[0]
56 if import_names:
57 qname, asname = import_names
58 name = asname or qname
59
60 if _has_locals_call_after_node(stmt, node.scope()):
61 message_name = "possibly-unused-variable"
62 else:
63 if isinstance(stmt, nodes.Import):
64 if asname is not None:
65 msg = f"{qname} imported as {asname}"
66 else:
67 msg = f"import {name}"
68 self.add_message("unused-import", args=msg, node=stmt)
69 return
70 if isinstance(stmt, nodes.ImportFrom):
71 if asname is not None:
72 msg = f"{qname} imported from {stmt.modname} as {asname}"
73 else:
74 msg = f"{name} imported from {stmt.modname}"
75 self.add_message("unused-import", args=msg, node=stmt)
76 return
77 message_name = "unused-variable"
78
79 if isinstance(stmt, nodes.FunctionDef) and stmt.decorators:
80 return
81
82 # Don't check function stubs created only for type information
83 if utils.is_overload_stub(node):
84 return
85
86 # Special case for exception variable
87 if isinstance(stmt.parent, nodes.ExceptHandler) and any(
88 n.name == name for n in stmt.parent.nodes_of_class(nodes.Name)
89 ):
90 return
91
92 self.add_message(message_name, args=name, node=stmt)
Path 2: 255 calls (0.18)
'a' (36) 'c' (24) 'b' (23) 'i' (16) 'var' (12) 'variable' (10) 'd' (9) 'value' (5) 'e' (4) 'f' (4)
FunctionDef (251) AsyncFunctionDef (4)
AssignName (255)
set() (255)
set() (254) {'count'} (1)
set() (235) {'i'} (17) {'m'} (2) {'mauris'} (1)
1def _check_is_unused(
2 self,
3 name: str,
4 node: nodes.FunctionDef,
5 stmt: nodes.NodeNG,
6 global_names: set[str],
7 nonlocal_names: Iterable[str],
8 comprehension_target_names: Iterable[str],
9 ) -> None:
10 # Ignore some special names specified by user configuration.
11 if self._is_name_ignored(stmt, name):
12 return
13 # Ignore names that were added dynamically to the Function scope
14 if (
15 isinstance(node, nodes.FunctionDef)
16 and name == "__class__"
17 and len(node.locals["__class__"]) == 1
18 and isinstance(node.locals["__class__"][0], nodes.ClassDef)
19 ):
20 return
21
22 # Ignore names imported by the global statement.
23 if isinstance(stmt, (nodes.Global, nodes.Import, nodes.ImportFrom)):
24 # Detect imports, assigned to global statements.
25 if global_names and _import_name_is_global(stmt, global_names):
26 return
27
28 # Ignore names in comprehension targets
29 if name in comprehension_target_names:
30 return
31
32 # Ignore names in string literal type annotation.
33 if name in self._type_annotation_names:
34 return
35
36 argnames = node.argnames()
37 # Care about functions with unknown argument (builtins)
38 if name in argnames:
39 self._check_unused_arguments(name, node, stmt, argnames, nonlocal_names)
40 else:
41 if stmt.parent and isinstance(
42 stmt.parent, (nodes.Assign, nodes.AnnAssign, nodes.Tuple, nodes.For)
43 ):
44 if name in nonlocal_names:
45 return
46
47 qname = asname = None
48 if isinstance(stmt, (nodes.Import, nodes.ImportFrom)):
49 # Need the complete name, which we don't have in .locals.
50 if len(stmt.names) > 1:
51 import_names = next(
52 (names for names in stmt.names if name in names), None
53 )
54 else:
55 import_names = stmt.names[0]
56 if import_names:
57 qname, asname = import_names
58 name = asname or qname
59
60 if _has_locals_call_after_node(stmt, node.scope()):
61 message_name = "possibly-unused-variable"
62 else:
63 if isinstance(stmt, nodes.Import):
64 if asname is not None:
65 msg = f"{qname} imported as {asname}"
66 else:
67 msg = f"import {name}"
68 self.add_message("unused-import", args=msg, node=stmt)
69 return
70 if isinstance(stmt, nodes.ImportFrom):
71 if asname is not None:
72 msg = f"{qname} imported from {stmt.modname} as {asname}"
73 else:
74 msg = f"{name} imported from {stmt.modname}"
75 self.add_message("unused-import", args=msg, node=stmt)
76 return
77 message_name = "unused-variable"
78
79 if isinstance(stmt, nodes.FunctionDef) and stmt.decorators:
80 return
81
82 # Don't check function stubs created only for type information
83 if utils.is_overload_stub(node):
84 return
85
86 # Special case for exception variable
87 if isinstance(stmt.parent, nodes.ExceptHandler) and any(
88 n.name == name for n in stmt.parent.nodes_of_class(nodes.Name)
89 ):
90 return
91
92 self.add_message(message_name, args=name, node=stmt)
Path 3: 130 calls (0.09)
'_' (89) '_inner' (5) '_new' (3) '_args' (2) '_kwargs' (2) '_x' (2) '_internal_func' (2) 'ignored_barg' (1) '_abc' (1) '_value' (1)
FunctionDef (128) AsyncFunctionDef (2)
AssignName (113) FunctionDef (10) Arguments (7)
set() (130)
set() (130)
set() (126) {'my_int'} (3) {'key'} (1)
None (130)
1def _check_is_unused(
2 self,
3 name: str,
4 node: nodes.FunctionDef,
5 stmt: nodes.NodeNG,
6 global_names: set[str],
7 nonlocal_names: Iterable[str],
8 comprehension_target_names: Iterable[str],
9 ) -> None:
10 # Ignore some special names specified by user configuration.
11 if self._is_name_ignored(stmt, name):
12 return
13 # Ignore names that were added dynamically to the Function scope
14 if (
15 isinstance(node, nodes.FunctionDef)
16 and name == "__class__"
17 and len(node.locals["__class__"]) == 1
18 and isinstance(node.locals["__class__"][0], nodes.ClassDef)
19 ):
20 return
21
22 # Ignore names imported by the global statement.
23 if isinstance(stmt, (nodes.Global, nodes.Import, nodes.ImportFrom)):
24 # Detect imports, assigned to global statements.
25 if global_names and _import_name_is_global(stmt, global_names):
26 return
27
28 # Ignore names in comprehension targets
29 if name in comprehension_target_names:
30 return
31
32 # Ignore names in string literal type annotation.
33 if name in self._type_annotation_names:
34 return
35
36 argnames = node.argnames()
37 # Care about functions with unknown argument (builtins)
38 if name in argnames:
39 self._check_unused_arguments(name, node, stmt, argnames, nonlocal_names)
40 else:
41 if stmt.parent and isinstance(
42 stmt.parent, (nodes.Assign, nodes.AnnAssign, nodes.Tuple, nodes.For)
43 ):
44 if name in nonlocal_names:
45 return
46
47 qname = asname = None
48 if isinstance(stmt, (nodes.Import, nodes.ImportFrom)):
49 # Need the complete name, which we don't have in .locals.
50 if len(stmt.names) > 1:
51 import_names = next(
52 (names for names in stmt.names if name in names), None
53 )
54 else:
55 import_names = stmt.names[0]
56 if import_names:
57 qname, asname = import_names
58 name = asname or qname
59
60 if _has_locals_call_after_node(stmt, node.scope()):
61 message_name = "possibly-unused-variable"
62 else:
63 if isinstance(stmt, nodes.Import):
64 if asname is not None:
65 msg = f"{qname} imported as {asname}"
66 else:
67 msg = f"import {name}"
68 self.add_message("unused-import", args=msg, node=stmt)
69 return
70 if isinstance(stmt, nodes.ImportFrom):
71 if asname is not None:
72 msg = f"{qname} imported from {stmt.modname} as {asname}"
73 else:
74 msg = f"{name} imported from {stmt.modname}"
75 self.add_message("unused-import", args=msg, node=stmt)
76 return
77 message_name = "unused-variable"
78
79 if isinstance(stmt, nodes.FunctionDef) and stmt.decorators:
80 return
81
82 # Don't check function stubs created only for type information
83 if utils.is_overload_stub(node):
84 return
85
86 # Special case for exception variable
87 if isinstance(stmt.parent, nodes.ExceptHandler) and any(
88 n.name == name for n in stmt.parent.nodes_of_class(nodes.Name)
89 ):
90 return
91
92 self.add_message(message_name, args=name, node=stmt)
Path 4: 20 calls (0.01)
'inner_func' (3) 'stuff' (2) 'MyContextManager' (1) 'fh' (1) 'fp' (1) 'fp2' (1) 'func' (1) 'best' (1) 'inner' (1) 'other_stuff' (1)
FunctionDef (19) AsyncFunctionDef (1)
FunctionDef (12) AssignName (5) ClassDef (2) AsyncFunctionDef (1)
set() (20)
set() (12) {'c', 'a'} (4) {'b', 'c'} (2) {'a'} (1) {'nonlocal_'} (1)
set() (20)
1def _check_is_unused(
2 self,
3 name: str,
4 node: nodes.FunctionDef,
5 stmt: nodes.NodeNG,
6 global_names: set[str],
7 nonlocal_names: Iterable[str],
8 comprehension_target_names: Iterable[str],
9 ) -> None:
10 # Ignore some special names specified by user configuration.
11 if self._is_name_ignored(stmt, name):
12 return
13 # Ignore names that were added dynamically to the Function scope
14 if (
15 isinstance(node, nodes.FunctionDef)
16 and name == "__class__"
17 and len(node.locals["__class__"]) == 1
18 and isinstance(node.locals["__class__"][0], nodes.ClassDef)
19 ):
20 return
21
22 # Ignore names imported by the global statement.
23 if isinstance(stmt, (nodes.Global, nodes.Import, nodes.ImportFrom)):
24 # Detect imports, assigned to global statements.
25 if global_names and _import_name_is_global(stmt, global_names):
26 return
27
28 # Ignore names in comprehension targets
29 if name in comprehension_target_names:
30 return
31
32 # Ignore names in string literal type annotation.
33 if name in self._type_annotation_names:
34 return
35
36 argnames = node.argnames()
37 # Care about functions with unknown argument (builtins)
38 if name in argnames:
39 self._check_unused_arguments(name, node, stmt, argnames, nonlocal_names)
40 else:
41 if stmt.parent and isinstance(
42 stmt.parent, (nodes.Assign, nodes.AnnAssign, nodes.Tuple, nodes.For)
43 ):
44 if name in nonlocal_names:
45 return
46
47 qname = asname = None
48 if isinstance(stmt, (nodes.Import, nodes.ImportFrom)):
49 # Need the complete name, which we don't have in .locals.
50 if len(stmt.names) > 1:
51 import_names = next(
52 (names for names in stmt.names if name in names), None
53 )
54 else:
55 import_names = stmt.names[0]
56 if import_names:
57 qname, asname = import_names
58 name = asname or qname
59
60 if _has_locals_call_after_node(stmt, node.scope()):
61 message_name = "possibly-unused-variable"
62 else:
63 if isinstance(stmt, nodes.Import):
64 if asname is not None:
65 msg = f"{qname} imported as {asname}"
66 else:
67 msg = f"import {name}"
68 self.add_message("unused-import", args=msg, node=stmt)
69 return
70 if isinstance(stmt, nodes.ImportFrom):
71 if asname is not None:
72 msg = f"{qname} imported from {stmt.modname} as {asname}"
73 else:
74 msg = f"{name} imported from {stmt.modname}"
75 self.add_message("unused-import", args=msg, node=stmt)
76 return
77 message_name = "unused-variable"
78
79 if isinstance(stmt, nodes.FunctionDef) and stmt.decorators:
80 return
81
82 # Don't check function stubs created only for type information
83 if utils.is_overload_stub(node):
84 return
85
86 # Special case for exception variable
87 if isinstance(stmt.parent, nodes.ExceptHandler) and any(
88 n.name == name for n in stmt.parent.nodes_of_class(nodes.Name)
89 ):
90 return
91
92 self.add_message(message_name, args=name, node=stmt)
Path 5: 17 calls (0.01)
'a' (3) 'c' (2) 'count' (2) 'some_num' (2) 'myint' (1) 'self' (1) 'i' (1) 'ann' (1) 'assign' (1) 'unrelated' (1)
FunctionDef (17)
AssignName (17)
set() (17)
{'a'} (2) {'c', 'a'} (2) {'ann', 'assign'} (2) {'count'} (2) {'some_num'} (2) {'c'} (1) {'myint'} (1) {'self'} (1) {'i'} (1) {'unrelated'} (1)
set() (17)
None (17)
1def _check_is_unused(
2 self,
3 name: str,
4 node: nodes.FunctionDef,
5 stmt: nodes.NodeNG,
6 global_names: set[str],
7 nonlocal_names: Iterable[str],
8 comprehension_target_names: Iterable[str],
9 ) -> None:
10 # Ignore some special names specified by user configuration.
11 if self._is_name_ignored(stmt, name):
12 return
13 # Ignore names that were added dynamically to the Function scope
14 if (
15 isinstance(node, nodes.FunctionDef)
16 and name == "__class__"
17 and len(node.locals["__class__"]) == 1
18 and isinstance(node.locals["__class__"][0], nodes.ClassDef)
19 ):
20 return
21
22 # Ignore names imported by the global statement.
23 if isinstance(stmt, (nodes.Global, nodes.Import, nodes.ImportFrom)):
24 # Detect imports, assigned to global statements.
25 if global_names and _import_name_is_global(stmt, global_names):
26 return
27
28 # Ignore names in comprehension targets
29 if name in comprehension_target_names:
30 return
31
32 # Ignore names in string literal type annotation.
33 if name in self._type_annotation_names:
34 return
35
36 argnames = node.argnames()
37 # Care about functions with unknown argument (builtins)
38 if name in argnames:
39 self._check_unused_arguments(name, node, stmt, argnames, nonlocal_names)
40 else:
41 if stmt.parent and isinstance(
42 stmt.parent, (nodes.Assign, nodes.AnnAssign, nodes.Tuple, nodes.For)
43 ):
44 if name in nonlocal_names:
45 return
46
47 qname = asname = None
48 if isinstance(stmt, (nodes.Import, nodes.ImportFrom)):
49 # Need the complete name, which we don't have in .locals.
50 if len(stmt.names) > 1:
51 import_names = next(
52 (names for names in stmt.names if name in names), None
53 )
54 else:
55 import_names = stmt.names[0]
56 if import_names:
57 qname, asname = import_names
58 name = asname or qname
59
60 if _has_locals_call_after_node(stmt, node.scope()):
61 message_name = "possibly-unused-variable"
62 else:
63 if isinstance(stmt, nodes.Import):
64 if asname is not None:
65 msg = f"{qname} imported as {asname}"
66 else:
67 msg = f"import {name}"
68 self.add_message("unused-import", args=msg, node=stmt)
69 return
70 if isinstance(stmt, nodes.ImportFrom):
71 if asname is not None:
72 msg = f"{qname} imported from {stmt.modname} as {asname}"
73 else:
74 msg = f"{name} imported from {stmt.modname}"
75 self.add_message("unused-import", args=msg, node=stmt)
76 return
77 message_name = "unused-variable"
78
79 if isinstance(stmt, nodes.FunctionDef) and stmt.decorators:
80 return
81
82 # Don't check function stubs created only for type information
83 if utils.is_overload_stub(node):
84 return
85
86 # Special case for exception variable
87 if isinstance(stmt.parent, nodes.ExceptHandler) and any(
88 n.name == name for n in stmt.parent.nodes_of_class(nodes.Name)
89 ):
90 return
91
92 self.add_message(message_name, args=name, node=stmt)
Path 6: 11 calls (0.01)
'e' (4) 'exc' (2) 'error' (2) 'chest' (1) 'test_redefined_in_except_handler' (1) 'CustomException' (1)
FunctionDef (11)
AssignName (10) FunctionDef (1)
set() (11)
set() (11)
set() (11)
1def _check_is_unused(
2 self,
3 name: str,
4 node: nodes.FunctionDef,
5 stmt: nodes.NodeNG,
6 global_names: set[str],
7 nonlocal_names: Iterable[str],
8 comprehension_target_names: Iterable[str],
9 ) -> None:
10 # Ignore some special names specified by user configuration.
11 if self._is_name_ignored(stmt, name):
12 return
13 # Ignore names that were added dynamically to the Function scope
14 if (
15 isinstance(node, nodes.FunctionDef)
16 and name == "__class__"
17 and len(node.locals["__class__"]) == 1
18 and isinstance(node.locals["__class__"][0], nodes.ClassDef)
19 ):
20 return
21
22 # Ignore names imported by the global statement.
23 if isinstance(stmt, (nodes.Global, nodes.Import, nodes.ImportFrom)):
24 # Detect imports, assigned to global statements.
25 if global_names and _import_name_is_global(stmt, global_names):
26 return
27
28 # Ignore names in comprehension targets
29 if name in comprehension_target_names:
30 return
31
32 # Ignore names in string literal type annotation.
33 if name in self._type_annotation_names:
34 return
35
36 argnames = node.argnames()
37 # Care about functions with unknown argument (builtins)
38 if name in argnames:
39 self._check_unused_arguments(name, node, stmt, argnames, nonlocal_names)
40 else:
41 if stmt.parent and isinstance(
42 stmt.parent, (nodes.Assign, nodes.AnnAssign, nodes.Tuple, nodes.For)
43 ):
44 if name in nonlocal_names:
45 return
46
47 qname = asname = None
48 if isinstance(stmt, (nodes.Import, nodes.ImportFrom)):
49 # Need the complete name, which we don't have in .locals.
50 if len(stmt.names) > 1:
51 import_names = next(
52 (names for names in stmt.names if name in names), None
53 )
54 else:
55 import_names = stmt.names[0]
56 if import_names:
57 qname, asname = import_names
58 name = asname or qname
59
60 if _has_locals_call_after_node(stmt, node.scope()):
61 message_name = "possibly-unused-variable"
62 else:
63 if isinstance(stmt, nodes.Import):
64 if asname is not None:
65 msg = f"{qname} imported as {asname}"
66 else:
67 msg = f"import {name}"
68 self.add_message("unused-import", args=msg, node=stmt)
69 return
70 if isinstance(stmt, nodes.ImportFrom):
71 if asname is not None:
72 msg = f"{qname} imported from {stmt.modname} as {asname}"
73 else:
74 msg = f"{name} imported from {stmt.modname}"
75 self.add_message("unused-import", args=msg, node=stmt)
76 return
77 message_name = "unused-variable"
78
79 if isinstance(stmt, nodes.FunctionDef) and stmt.decorators:
80 return
81
82 # Don't check function stubs created only for type information
83 if utils.is_overload_stub(node):
84 return
85
86 # Special case for exception variable
87 if isinstance(stmt.parent, nodes.ExceptHandler) and any(
88 n.name == name for n in stmt.parent.nodes_of_class(nodes.Name)
89 ):
90 return
91
92 self.add_message(message_name, args=name, node=stmt)
Path 7: 9 calls (0.01)
'symtable' (1) 'trace' (1) 'tabnanny' (1) 'astroid' (1) 'notastroid' (1) 'xml' (1) 'unittest' (1) 'this' (1) 'x' (1)
FunctionDef (9)
Import (9)
set() (8) {'deque', 'OS', 'PATH', 'collections'} (1)
set() (9)
set() (9)
None (9)
1def _check_is_unused(
2 self,
3 name: str,
4 node: nodes.FunctionDef,
5 stmt: nodes.NodeNG,
6 global_names: set[str],
7 nonlocal_names: Iterable[str],
8 comprehension_target_names: Iterable[str],
9 ) -> None:
10 # Ignore some special names specified by user configuration.
11 if self._is_name_ignored(stmt, name):
12 return
13 # Ignore names that were added dynamically to the Function scope
14 if (
15 isinstance(node, nodes.FunctionDef)
16 and name == "__class__"
17 and len(node.locals["__class__"]) == 1
18 and isinstance(node.locals["__class__"][0], nodes.ClassDef)
19 ):
20 return
21
22 # Ignore names imported by the global statement.
23 if isinstance(stmt, (nodes.Global, nodes.Import, nodes.ImportFrom)):
24 # Detect imports, assigned to global statements.
25 if global_names and _import_name_is_global(stmt, global_names):
26 return
27
28 # Ignore names in comprehension targets
29 if name in comprehension_target_names:
30 return
31
32 # Ignore names in string literal type annotation.
33 if name in self._type_annotation_names:
34 return
35
36 argnames = node.argnames()
37 # Care about functions with unknown argument (builtins)
38 if name in argnames:
39 self._check_unused_arguments(name, node, stmt, argnames, nonlocal_names)
40 else:
41 if stmt.parent and isinstance(
42 stmt.parent, (nodes.Assign, nodes.AnnAssign, nodes.Tuple, nodes.For)
43 ):
44 if name in nonlocal_names:
45 return
46
47 qname = asname = None
48 if isinstance(stmt, (nodes.Import, nodes.ImportFrom)):
49 # Need the complete name, which we don't have in .locals.
50 if len(stmt.names) > 1:
51 import_names = next(
52 (names for names in stmt.names if name in names), None
53 )
54 else:
55 import_names = stmt.names[0]
56 if import_names:
57 qname, asname = import_names
58 name = asname or qname
59
60 if _has_locals_call_after_node(stmt, node.scope()):
61 message_name = "possibly-unused-variable"
62 else:
63 if isinstance(stmt, nodes.Import):
64 if asname is not None:
65 msg = f"{qname} imported as {asname}"
66 else:
67 msg = f"import {name}"
68 self.add_message("unused-import", args=msg, node=stmt)
69 return
70 if isinstance(stmt, nodes.ImportFrom):
71 if asname is not None:
72 msg = f"{qname} imported from {stmt.modname} as {asname}"
73 else:
74 msg = f"{name} imported from {stmt.modname}"
75 self.add_message("unused-import", args=msg, node=stmt)
76 return
77 message_name = "unused-variable"
78
79 if isinstance(stmt, nodes.FunctionDef) and stmt.decorators:
80 return
81
82 # Don't check function stubs created only for type information
83 if utils.is_overload_stub(node):
84 return
85
86 # Special case for exception variable
87 if isinstance(stmt.parent, nodes.ExceptHandler) and any(
88 n.name == name for n in stmt.parent.nodes_of_class(nodes.Name)
89 ):
90 return
91
92 self.add_message(message_name, args=name, node=stmt)
Path 8: 6 calls (0.0)
'sys' (1) 'namedtuple' (1) 'OS' (1) 'collections' (1) 'PATH' (1) 'deque' (1)
FunctionDef (6)
Import (3) ImportFrom (3)
{'deque', 'OS', 'PATH', 'collections'} (4) {'sys'} (1) {'namedtuple'} (1)
set() (6)
set() (6)
None (6)
1def _check_is_unused(
2 self,
3 name: str,
4 node: nodes.FunctionDef,
5 stmt: nodes.NodeNG,
6 global_names: set[str],
7 nonlocal_names: Iterable[str],
8 comprehension_target_names: Iterable[str],
9 ) -> None:
10 # Ignore some special names specified by user configuration.
11 if self._is_name_ignored(stmt, name):
12 return
13 # Ignore names that were added dynamically to the Function scope
14 if (
15 isinstance(node, nodes.FunctionDef)
16 and name == "__class__"
17 and len(node.locals["__class__"]) == 1
18 and isinstance(node.locals["__class__"][0], nodes.ClassDef)
19 ):
20 return
21
22 # Ignore names imported by the global statement.
23 if isinstance(stmt, (nodes.Global, nodes.Import, nodes.ImportFrom)):
24 # Detect imports, assigned to global statements.
25 if global_names and _import_name_is_global(stmt, global_names):
26 return
27
28 # Ignore names in comprehension targets
29 if name in comprehension_target_names:
30 return
31
32 # Ignore names in string literal type annotation.
33 if name in self._type_annotation_names:
34 return
35
36 argnames = node.argnames()
37 # Care about functions with unknown argument (builtins)
38 if name in argnames:
39 self._check_unused_arguments(name, node, stmt, argnames, nonlocal_names)
40 else:
41 if stmt.parent and isinstance(
42 stmt.parent, (nodes.Assign, nodes.AnnAssign, nodes.Tuple, nodes.For)
43 ):
44 if name in nonlocal_names:
45 return
46
47 qname = asname = None
48 if isinstance(stmt, (nodes.Import, nodes.ImportFrom)):
49 # Need the complete name, which we don't have in .locals.
50 if len(stmt.names) > 1:
51 import_names = next(
52 (names for names in stmt.names if name in names), None
53 )
54 else:
55 import_names = stmt.names[0]
56 if import_names:
57 qname, asname = import_names
58 name = asname or qname
59
60 if _has_locals_call_after_node(stmt, node.scope()):
61 message_name = "possibly-unused-variable"
62 else:
63 if isinstance(stmt, nodes.Import):
64 if asname is not None:
65 msg = f"{qname} imported as {asname}"
66 else:
67 msg = f"import {name}"
68 self.add_message("unused-import", args=msg, node=stmt)
69 return
70 if isinstance(stmt, nodes.ImportFrom):
71 if asname is not None:
72 msg = f"{qname} imported from {stmt.modname} as {asname}"
73 else:
74 msg = f"{name} imported from {stmt.modname}"
75 self.add_message("unused-import", args=msg, node=stmt)
76 return
77 message_name = "unused-variable"
78
79 if isinstance(stmt, nodes.FunctionDef) and stmt.decorators:
80 return
81
82 # Don't check function stubs created only for type information
83 if utils.is_overload_stub(node):
84 return
85
86 # Special case for exception variable
87 if isinstance(stmt.parent, nodes.ExceptHandler) and any(
88 n.name == name for n in stmt.parent.nodes_of_class(nodes.Name)
89 ):
90 return
91
92 self.add_message(message_name, args=name, node=stmt)
Path 9: 4 calls (0.0)
'defaultdict' (1) 'open' (1) 'namedtuple' (1) 'platform' (1)
FunctionDef (4)
ImportFrom (4)
set() (3) {'deque', 'OS', 'PATH', 'collections'} (1)
set() (4)
set() (4)
None (4)
1def _check_is_unused(
2 self,
3 name: str,
4 node: nodes.FunctionDef,
5 stmt: nodes.NodeNG,
6 global_names: set[str],
7 nonlocal_names: Iterable[str],
8 comprehension_target_names: Iterable[str],
9 ) -> None:
10 # Ignore some special names specified by user configuration.
11 if self._is_name_ignored(stmt, name):
12 return
13 # Ignore names that were added dynamically to the Function scope
14 if (
15 isinstance(node, nodes.FunctionDef)
16 and name == "__class__"
17 and len(node.locals["__class__"]) == 1
18 and isinstance(node.locals["__class__"][0], nodes.ClassDef)
19 ):
20 return
21
22 # Ignore names imported by the global statement.
23 if isinstance(stmt, (nodes.Global, nodes.Import, nodes.ImportFrom)):
24 # Detect imports, assigned to global statements.
25 if global_names and _import_name_is_global(stmt, global_names):
26 return
27
28 # Ignore names in comprehension targets
29 if name in comprehension_target_names:
30 return
31
32 # Ignore names in string literal type annotation.
33 if name in self._type_annotation_names:
34 return
35
36 argnames = node.argnames()
37 # Care about functions with unknown argument (builtins)
38 if name in argnames:
39 self._check_unused_arguments(name, node, stmt, argnames, nonlocal_names)
40 else:
41 if stmt.parent and isinstance(
42 stmt.parent, (nodes.Assign, nodes.AnnAssign, nodes.Tuple, nodes.For)
43 ):
44 if name in nonlocal_names:
45 return
46
47 qname = asname = None
48 if isinstance(stmt, (nodes.Import, nodes.ImportFrom)):
49 # Need the complete name, which we don't have in .locals.
50 if len(stmt.names) > 1:
51 import_names = next(
52 (names for names in stmt.names if name in names), None
53 )
54 else:
55 import_names = stmt.names[0]
56 if import_names:
57 qname, asname = import_names
58 name = asname or qname
59
60 if _has_locals_call_after_node(stmt, node.scope()):
61 message_name = "possibly-unused-variable"
62 else:
63 if isinstance(stmt, nodes.Import):
64 if asname is not None:
65 msg = f"{qname} imported as {asname}"
66 else:
67 msg = f"import {name}"
68 self.add_message("unused-import", args=msg, node=stmt)
69 return
70 if isinstance(stmt, nodes.ImportFrom):
71 if asname is not None:
72 msg = f"{qname} imported from {stmt.modname} as {asname}"
73 else:
74 msg = f"{name} imported from {stmt.modname}"
75 self.add_message("unused-import", args=msg, node=stmt)
76 return
77 message_name = "unused-variable"
78
79 if isinstance(stmt, nodes.FunctionDef) and stmt.decorators:
80 return
81
82 # Don't check function stubs created only for type information
83 if utils.is_overload_stub(node):
84 return
85
86 # Special case for exception variable
87 if isinstance(stmt.parent, nodes.ExceptHandler) and any(
88 n.name == name for n in stmt.parent.nodes_of_class(nodes.Name)
89 ):
90 return
91
92 self.add_message(message_name, args=name, node=stmt)
Path 10: 4 calls (0.0)
'key' (2) 'my_int' (1) 'x' (1)
FunctionDef (4)
AssignName (4)
set() (4)
set() (4)
{'my_int'} (1) {'value', 'key'} (1) {'b', 'a', 'key'} (1) {'x'} (1)
None (4)
1def _check_is_unused(
2 self,
3 name: str,
4 node: nodes.FunctionDef,
5 stmt: nodes.NodeNG,
6 global_names: set[str],
7 nonlocal_names: Iterable[str],
8 comprehension_target_names: Iterable[str],
9 ) -> None:
10 # Ignore some special names specified by user configuration.
11 if self._is_name_ignored(stmt, name):
12 return
13 # Ignore names that were added dynamically to the Function scope
14 if (
15 isinstance(node, nodes.FunctionDef)
16 and name == "__class__"
17 and len(node.locals["__class__"]) == 1
18 and isinstance(node.locals["__class__"][0], nodes.ClassDef)
19 ):
20 return
21
22 # Ignore names imported by the global statement.
23 if isinstance(stmt, (nodes.Global, nodes.Import, nodes.ImportFrom)):
24 # Detect imports, assigned to global statements.
25 if global_names and _import_name_is_global(stmt, global_names):
26 return
27
28 # Ignore names in comprehension targets
29 if name in comprehension_target_names:
30 return
31
32 # Ignore names in string literal type annotation.
33 if name in self._type_annotation_names:
34 return
35
36 argnames = node.argnames()
37 # Care about functions with unknown argument (builtins)
38 if name in argnames:
39 self._check_unused_arguments(name, node, stmt, argnames, nonlocal_names)
40 else:
41 if stmt.parent and isinstance(
42 stmt.parent, (nodes.Assign, nodes.AnnAssign, nodes.Tuple, nodes.For)
43 ):
44 if name in nonlocal_names:
45 return
46
47 qname = asname = None
48 if isinstance(stmt, (nodes.Import, nodes.ImportFrom)):
49 # Need the complete name, which we don't have in .locals.
50 if len(stmt.names) > 1:
51 import_names = next(
52 (names for names in stmt.names if name in names), None
53 )
54 else:
55 import_names = stmt.names[0]
56 if import_names:
57 qname, asname = import_names
58 name = asname or qname
59
60 if _has_locals_call_after_node(stmt, node.scope()):
61 message_name = "possibly-unused-variable"
62 else:
63 if isinstance(stmt, nodes.Import):
64 if asname is not None:
65 msg = f"{qname} imported as {asname}"
66 else:
67 msg = f"import {name}"
68 self.add_message("unused-import", args=msg, node=stmt)
69 return
70 if isinstance(stmt, nodes.ImportFrom):
71 if asname is not None:
72 msg = f"{qname} imported from {stmt.modname} as {asname}"
73 else:
74 msg = f"{name} imported from {stmt.modname}"
75 self.add_message("unused-import", args=msg, node=stmt)
76 return
77 message_name = "unused-variable"
78
79 if isinstance(stmt, nodes.FunctionDef) and stmt.decorators:
80 return
81
82 # Don't check function stubs created only for type information
83 if utils.is_overload_stub(node):
84 return
85
86 # Special case for exception variable
87 if isinstance(stmt.parent, nodes.ExceptHandler) and any(
88 n.name == name for n in stmt.parent.nodes_of_class(nodes.Name)
89 ):
90 return
91
92 self.add_message(message_name, args=name, node=stmt)
Path 11: 3 calls (0.0)
'thyme' (1) 'sql' (1) 'RE' (1)
FunctionDef (3)
Import (3)
set() (2) {'deque', 'OS', 'PATH', 'collections'} (1)
set() (3)
set() (3)
None (3)
1def _check_is_unused(
2 self,
3 name: str,
4 node: nodes.FunctionDef,
5 stmt: nodes.NodeNG,
6 global_names: set[str],
7 nonlocal_names: Iterable[str],
8 comprehension_target_names: Iterable[str],
9 ) -> None:
10 # Ignore some special names specified by user configuration.
11 if self._is_name_ignored(stmt, name):
12 return
13 # Ignore names that were added dynamically to the Function scope
14 if (
15 isinstance(node, nodes.FunctionDef)
16 and name == "__class__"
17 and len(node.locals["__class__"]) == 1
18 and isinstance(node.locals["__class__"][0], nodes.ClassDef)
19 ):
20 return
21
22 # Ignore names imported by the global statement.
23 if isinstance(stmt, (nodes.Global, nodes.Import, nodes.ImportFrom)):
24 # Detect imports, assigned to global statements.
25 if global_names and _import_name_is_global(stmt, global_names):
26 return
27
28 # Ignore names in comprehension targets
29 if name in comprehension_target_names:
30 return
31
32 # Ignore names in string literal type annotation.
33 if name in self._type_annotation_names:
34 return
35
36 argnames = node.argnames()
37 # Care about functions with unknown argument (builtins)
38 if name in argnames:
39 self._check_unused_arguments(name, node, stmt, argnames, nonlocal_names)
40 else:
41 if stmt.parent and isinstance(
42 stmt.parent, (nodes.Assign, nodes.AnnAssign, nodes.Tuple, nodes.For)
43 ):
44 if name in nonlocal_names:
45 return
46
47 qname = asname = None
48 if isinstance(stmt, (nodes.Import, nodes.ImportFrom)):
49 # Need the complete name, which we don't have in .locals.
50 if len(stmt.names) > 1:
51 import_names = next(
52 (names for names in stmt.names if name in names), None
53 )
54 else:
55 import_names = stmt.names[0]
56 if import_names:
57 qname, asname = import_names
58 name = asname or qname
59
60 if _has_locals_call_after_node(stmt, node.scope()):
61 message_name = "possibly-unused-variable"
62 else:
63 if isinstance(stmt, nodes.Import):
64 if asname is not None:
65 msg = f"{qname} imported as {asname}"
66 else:
67 msg = f"import {name}"
68 self.add_message("unused-import", args=msg, node=stmt)
69 return
70 if isinstance(stmt, nodes.ImportFrom):
71 if asname is not None:
72 msg = f"{qname} imported from {stmt.modname} as {asname}"
73 else:
74 msg = f"{name} imported from {stmt.modname}"
75 self.add_message("unused-import", args=msg, node=stmt)
76 return
77 message_name = "unused-variable"
78
79 if isinstance(stmt, nodes.FunctionDef) and stmt.decorators:
80 return
81
82 # Don't check function stubs created only for type information
83 if utils.is_overload_stub(node):
84 return
85
86 # Special case for exception variable
87 if isinstance(stmt.parent, nodes.ExceptHandler) and any(
88 n.name == name for n in stmt.parent.nodes_of_class(nodes.Name)
89 ):
90 return
91
92 self.add_message(message_name, args=name, node=stmt)
Path 12: 2 calls (0.0)
'os' (1) 'sys' (1)
FunctionDef (2)
Import (2)
set() (2)
set() (2)
set() (2)
None (2)
GeneratorExit (2)
1def _check_is_unused(
2 self,
3 name: str,
4 node: nodes.FunctionDef,
5 stmt: nodes.NodeNG,
6 global_names: set[str],
7 nonlocal_names: Iterable[str],
8 comprehension_target_names: Iterable[str],
9 ) -> None:
10 # Ignore some special names specified by user configuration.
11 if self._is_name_ignored(stmt, name):
12 return
13 # Ignore names that were added dynamically to the Function scope
14 if (
15 isinstance(node, nodes.FunctionDef)
16 and name == "__class__"
17 and len(node.locals["__class__"]) == 1
18 and isinstance(node.locals["__class__"][0], nodes.ClassDef)
19 ):
20 return
21
22 # Ignore names imported by the global statement.
23 if isinstance(stmt, (nodes.Global, nodes.Import, nodes.ImportFrom)):
24 # Detect imports, assigned to global statements.
25 if global_names and _import_name_is_global(stmt, global_names):
26 return
27
28 # Ignore names in comprehension targets
29 if name in comprehension_target_names:
30 return
31
32 # Ignore names in string literal type annotation.
33 if name in self._type_annotation_names:
34 return
35
36 argnames = node.argnames()
37 # Care about functions with unknown argument (builtins)
38 if name in argnames:
39 self._check_unused_arguments(name, node, stmt, argnames, nonlocal_names)
40 else:
41 if stmt.parent and isinstance(
42 stmt.parent, (nodes.Assign, nodes.AnnAssign, nodes.Tuple, nodes.For)
43 ):
44 if name in nonlocal_names:
45 return
46
47 qname = asname = None
48 if isinstance(stmt, (nodes.Import, nodes.ImportFrom)):
49 # Need the complete name, which we don't have in .locals.
50 if len(stmt.names) > 1:
51 import_names = next(
52 (names for names in stmt.names if name in names), None
53 )
54 else:
55 import_names = stmt.names[0]
56 if import_names:
57 qname, asname = import_names
58 name = asname or qname
59
60 if _has_locals_call_after_node(stmt, node.scope()):
61 message_name = "possibly-unused-variable"
62 else:
63 if isinstance(stmt, nodes.Import):
64 if asname is not None:
65 msg = f"{qname} imported as {asname}"
66 else:
67 msg = f"import {name}"
68 self.add_message("unused-import", args=msg, node=stmt)
69 return
70 if isinstance(stmt, nodes.ImportFrom):
71 if asname is not None:
72 msg = f"{qname} imported from {stmt.modname} as {asname}"
73 else:
74 msg = f"{name} imported from {stmt.modname}"
75 self.add_message("unused-import", args=msg, node=stmt)
76 return
77 message_name = "unused-variable"
78
79 if isinstance(stmt, nodes.FunctionDef) and stmt.decorators:
80 return
81
82 # Don't check function stubs created only for type information
83 if utils.is_overload_stub(node):
84 return
85
86 # Special case for exception variable
87 if isinstance(stmt.parent, nodes.ExceptHandler) and any(
88 n.name == name for n in stmt.parent.nodes_of_class(nodes.Name)
89 ):
90 return
91
92 self.add_message(message_name, args=name, node=stmt)
Path 13: 2 calls (0.0)
'rand' (1) 'sock' (1)
FunctionDef (2)
Import (2)
set() (2)
set() (2)
set() (2)
None (2)
GeneratorExit (2)
1def _check_is_unused(
2 self,
3 name: str,
4 node: nodes.FunctionDef,
5 stmt: nodes.NodeNG,
6 global_names: set[str],
7 nonlocal_names: Iterable[str],
8 comprehension_target_names: Iterable[str],
9 ) -> None:
10 # Ignore some special names specified by user configuration.
11 if self._is_name_ignored(stmt, name):
12 return
13 # Ignore names that were added dynamically to the Function scope
14 if (
15 isinstance(node, nodes.FunctionDef)
16 and name == "__class__"
17 and len(node.locals["__class__"]) == 1
18 and isinstance(node.locals["__class__"][0], nodes.ClassDef)
19 ):
20 return
21
22 # Ignore names imported by the global statement.
23 if isinstance(stmt, (nodes.Global, nodes.Import, nodes.ImportFrom)):
24 # Detect imports, assigned to global statements.
25 if global_names and _import_name_is_global(stmt, global_names):
26 return
27
28 # Ignore names in comprehension targets
29 if name in comprehension_target_names:
30 return
31
32 # Ignore names in string literal type annotation.
33 if name in self._type_annotation_names:
34 return
35
36 argnames = node.argnames()
37 # Care about functions with unknown argument (builtins)
38 if name in argnames:
39 self._check_unused_arguments(name, node, stmt, argnames, nonlocal_names)
40 else:
41 if stmt.parent and isinstance(
42 stmt.parent, (nodes.Assign, nodes.AnnAssign, nodes.Tuple, nodes.For)
43 ):
44 if name in nonlocal_names:
45 return
46
47 qname = asname = None
48 if isinstance(stmt, (nodes.Import, nodes.ImportFrom)):
49 # Need the complete name, which we don't have in .locals.
50 if len(stmt.names) > 1:
51 import_names = next(
52 (names for names in stmt.names if name in names), None
53 )
54 else:
55 import_names = stmt.names[0]
56 if import_names:
57 qname, asname = import_names
58 name = asname or qname
59
60 if _has_locals_call_after_node(stmt, node.scope()):
61 message_name = "possibly-unused-variable"
62 else:
63 if isinstance(stmt, nodes.Import):
64 if asname is not None:
65 msg = f"{qname} imported as {asname}"
66 else:
67 msg = f"import {name}"
68 self.add_message("unused-import", args=msg, node=stmt)
69 return
70 if isinstance(stmt, nodes.ImportFrom):
71 if asname is not None:
72 msg = f"{qname} imported from {stmt.modname} as {asname}"
73 else:
74 msg = f"{name} imported from {stmt.modname}"
75 self.add_message("unused-import", args=msg, node=stmt)
76 return
77 message_name = "unused-variable"
78
79 if isinstance(stmt, nodes.FunctionDef) and stmt.decorators:
80 return
81
82 # Don't check function stubs created only for type information
83 if utils.is_overload_stub(node):
84 return
85
86 # Special case for exception variable
87 if isinstance(stmt.parent, nodes.ExceptHandler) and any(
88 n.name == name for n in stmt.parent.nodes_of_class(nodes.Name)
89 ):
90 return
91
92 self.add_message(message_name, args=name, node=stmt)
Path 14: 2 calls (0.0)
'sign' (1) 'cosplay' (1)
FunctionDef (2)
ImportFrom (2)
set() (2)
set() (2)
set() (2)
None (2)
GeneratorExit (2)
1def _check_is_unused(
2 self,
3 name: str,
4 node: nodes.FunctionDef,
5 stmt: nodes.NodeNG,
6 global_names: set[str],
7 nonlocal_names: Iterable[str],
8 comprehension_target_names: Iterable[str],
9 ) -> None:
10 # Ignore some special names specified by user configuration.
11 if self._is_name_ignored(stmt, name):
12 return
13 # Ignore names that were added dynamically to the Function scope
14 if (
15 isinstance(node, nodes.FunctionDef)
16 and name == "__class__"
17 and len(node.locals["__class__"]) == 1
18 and isinstance(node.locals["__class__"][0], nodes.ClassDef)
19 ):
20 return
21
22 # Ignore names imported by the global statement.
23 if isinstance(stmt, (nodes.Global, nodes.Import, nodes.ImportFrom)):
24 # Detect imports, assigned to global statements.
25 if global_names and _import_name_is_global(stmt, global_names):
26 return
27
28 # Ignore names in comprehension targets
29 if name in comprehension_target_names:
30 return
31
32 # Ignore names in string literal type annotation.
33 if name in self._type_annotation_names:
34 return
35
36 argnames = node.argnames()
37 # Care about functions with unknown argument (builtins)
38 if name in argnames:
39 self._check_unused_arguments(name, node, stmt, argnames, nonlocal_names)
40 else:
41 if stmt.parent and isinstance(
42 stmt.parent, (nodes.Assign, nodes.AnnAssign, nodes.Tuple, nodes.For)
43 ):
44 if name in nonlocal_names:
45 return
46
47 qname = asname = None
48 if isinstance(stmt, (nodes.Import, nodes.ImportFrom)):
49 # Need the complete name, which we don't have in .locals.
50 if len(stmt.names) > 1:
51 import_names = next(
52 (names for names in stmt.names if name in names), None
53 )
54 else:
55 import_names = stmt.names[0]
56 if import_names:
57 qname, asname = import_names
58 name = asname or qname
59
60 if _has_locals_call_after_node(stmt, node.scope()):
61 message_name = "possibly-unused-variable"
62 else:
63 if isinstance(stmt, nodes.Import):
64 if asname is not None:
65 msg = f"{qname} imported as {asname}"
66 else:
67 msg = f"import {name}"
68 self.add_message("unused-import", args=msg, node=stmt)
69 return
70 if isinstance(stmt, nodes.ImportFrom):
71 if asname is not None:
72 msg = f"{qname} imported from {stmt.modname} as {asname}"
73 else:
74 msg = f"{name} imported from {stmt.modname}"
75 self.add_message("unused-import", args=msg, node=stmt)
76 return
77 message_name = "unused-variable"
78
79 if isinstance(stmt, nodes.FunctionDef) and stmt.decorators:
80 return
81
82 # Don't check function stubs created only for type information
83 if utils.is_overload_stub(node):
84 return
85
86 # Special case for exception variable
87 if isinstance(stmt.parent, nodes.ExceptHandler) and any(
88 n.name == name for n in stmt.parent.nodes_of_class(nodes.Name)
89 ):
90 return
91
92 self.add_message(message_name, args=name, node=stmt)
Path 15: 2 calls (0.0)
'abc' (1) 'VERSION' (1)
FunctionDef (2)
ImportFrom (2)
set() (1) {'deque', 'OS', 'PATH', 'collections'} (1)
set() (2)
set() (2)
None (2)
1def _check_is_unused(
2 self,
3 name: str,
4 node: nodes.FunctionDef,
5 stmt: nodes.NodeNG,
6 global_names: set[str],
7 nonlocal_names: Iterable[str],
8 comprehension_target_names: Iterable[str],
9 ) -> None:
10 # Ignore some special names specified by user configuration.
11 if self._is_name_ignored(stmt, name):
12 return
13 # Ignore names that were added dynamically to the Function scope
14 if (
15 isinstance(node, nodes.FunctionDef)
16 and name == "__class__"
17 and len(node.locals["__class__"]) == 1
18 and isinstance(node.locals["__class__"][0], nodes.ClassDef)
19 ):
20 return
21
22 # Ignore names imported by the global statement.
23 if isinstance(stmt, (nodes.Global, nodes.Import, nodes.ImportFrom)):
24 # Detect imports, assigned to global statements.
25 if global_names and _import_name_is_global(stmt, global_names):
26 return
27
28 # Ignore names in comprehension targets
29 if name in comprehension_target_names:
30 return
31
32 # Ignore names in string literal type annotation.
33 if name in self._type_annotation_names:
34 return
35
36 argnames = node.argnames()
37 # Care about functions with unknown argument (builtins)
38 if name in argnames:
39 self._check_unused_arguments(name, node, stmt, argnames, nonlocal_names)
40 else:
41 if stmt.parent and isinstance(
42 stmt.parent, (nodes.Assign, nodes.AnnAssign, nodes.Tuple, nodes.For)
43 ):
44 if name in nonlocal_names:
45 return
46
47 qname = asname = None
48 if isinstance(stmt, (nodes.Import, nodes.ImportFrom)):
49 # Need the complete name, which we don't have in .locals.
50 if len(stmt.names) > 1:
51 import_names = next(
52 (names for names in stmt.names if name in names), None
53 )
54 else:
55 import_names = stmt.names[0]
56 if import_names:
57 qname, asname = import_names
58 name = asname or qname
59
60 if _has_locals_call_after_node(stmt, node.scope()):
61 message_name = "possibly-unused-variable"
62 else:
63 if isinstance(stmt, nodes.Import):
64 if asname is not None:
65 msg = f"{qname} imported as {asname}"
66 else:
67 msg = f"import {name}"
68 self.add_message("unused-import", args=msg, node=stmt)
69 return
70 if isinstance(stmt, nodes.ImportFrom):
71 if asname is not None:
72 msg = f"{qname} imported from {stmt.modname} as {asname}"
73 else:
74 msg = f"{name} imported from {stmt.modname}"
75 self.add_message("unused-import", args=msg, node=stmt)
76 return
77 message_name = "unused-variable"
78
79 if isinstance(stmt, nodes.FunctionDef) and stmt.decorators:
80 return
81
82 # Don't check function stubs created only for type information
83 if utils.is_overload_stub(node):
84 return
85
86 # Special case for exception variable
87 if isinstance(stmt.parent, nodes.ExceptHandler) and any(
88 n.name == name for n in stmt.parent.nodes_of_class(nodes.Name)
89 ):
90 return
91
92 self.add_message(message_name, args=name, node=stmt)
Path 16: 1 calls (0.0)
'bar' (1)
FunctionDef (1)
FunctionDef (1)
set() (1)
set() (1)
set() (1)
None (1)
1def _check_is_unused(
2 self,
3 name: str,
4 node: nodes.FunctionDef,
5 stmt: nodes.NodeNG,
6 global_names: set[str],
7 nonlocal_names: Iterable[str],
8 comprehension_target_names: Iterable[str],
9 ) -> None:
10 # Ignore some special names specified by user configuration.
11 if self._is_name_ignored(stmt, name):
12 return
13 # Ignore names that were added dynamically to the Function scope
14 if (
15 isinstance(node, nodes.FunctionDef)
16 and name == "__class__"
17 and len(node.locals["__class__"]) == 1
18 and isinstance(node.locals["__class__"][0], nodes.ClassDef)
19 ):
20 return
21
22 # Ignore names imported by the global statement.
23 if isinstance(stmt, (nodes.Global, nodes.Import, nodes.ImportFrom)):
24 # Detect imports, assigned to global statements.
25 if global_names and _import_name_is_global(stmt, global_names):
26 return
27
28 # Ignore names in comprehension targets
29 if name in comprehension_target_names:
30 return
31
32 # Ignore names in string literal type annotation.
33 if name in self._type_annotation_names:
34 return
35
36 argnames = node.argnames()
37 # Care about functions with unknown argument (builtins)
38 if name in argnames:
39 self._check_unused_arguments(name, node, stmt, argnames, nonlocal_names)
40 else:
41 if stmt.parent and isinstance(
42 stmt.parent, (nodes.Assign, nodes.AnnAssign, nodes.Tuple, nodes.For)
43 ):
44 if name in nonlocal_names:
45 return
46
47 qname = asname = None
48 if isinstance(stmt, (nodes.Import, nodes.ImportFrom)):
49 # Need the complete name, which we don't have in .locals.
50 if len(stmt.names) > 1:
51 import_names = next(
52 (names for names in stmt.names if name in names), None
53 )
54 else:
55 import_names = stmt.names[0]
56 if import_names:
57 qname, asname = import_names
58 name = asname or qname
59
60 if _has_locals_call_after_node(stmt, node.scope()):
61 message_name = "possibly-unused-variable"
62 else:
63 if isinstance(stmt, nodes.Import):
64 if asname is not None:
65 msg = f"{qname} imported as {asname}"
66 else:
67 msg = f"import {name}"
68 self.add_message("unused-import", args=msg, node=stmt)
69 return
70 if isinstance(stmt, nodes.ImportFrom):
71 if asname is not None:
72 msg = f"{qname} imported from {stmt.modname} as {asname}"
73 else:
74 msg = f"{name} imported from {stmt.modname}"
75 self.add_message("unused-import", args=msg, node=stmt)
76 return
77 message_name = "unused-variable"
78
79 if isinstance(stmt, nodes.FunctionDef) and stmt.decorators:
80 return
81
82 # Don't check function stubs created only for type information
83 if utils.is_overload_stub(node):
84 return
85
86 # Special case for exception variable
87 if isinstance(stmt.parent, nodes.ExceptHandler) and any(
88 n.name == name for n in stmt.parent.nodes_of_class(nodes.Name)
89 ):
90 return
91
92 self.add_message(message_name, args=name, node=stmt)
Path 17: 1 calls (0.0)
'Example' (1)
FunctionDef (1)
AssignName (1)
set() (1)
set() (1)
set() (1)
None (1)
1def _check_is_unused(
2 self,
3 name: str,
4 node: nodes.FunctionDef,
5 stmt: nodes.NodeNG,
6 global_names: set[str],
7 nonlocal_names: Iterable[str],
8 comprehension_target_names: Iterable[str],
9 ) -> None:
10 # Ignore some special names specified by user configuration.
11 if self._is_name_ignored(stmt, name):
12 return
13 # Ignore names that were added dynamically to the Function scope
14 if (
15 isinstance(node, nodes.FunctionDef)
16 and name == "__class__"
17 and len(node.locals["__class__"]) == 1
18 and isinstance(node.locals["__class__"][0], nodes.ClassDef)
19 ):
20 return
21
22 # Ignore names imported by the global statement.
23 if isinstance(stmt, (nodes.Global, nodes.Import, nodes.ImportFrom)):
24 # Detect imports, assigned to global statements.
25 if global_names and _import_name_is_global(stmt, global_names):
26 return
27
28 # Ignore names in comprehension targets
29 if name in comprehension_target_names:
30 return
31
32 # Ignore names in string literal type annotation.
33 if name in self._type_annotation_names:
34 return
35
36 argnames = node.argnames()
37 # Care about functions with unknown argument (builtins)
38 if name in argnames:
39 self._check_unused_arguments(name, node, stmt, argnames, nonlocal_names)
40 else:
41 if stmt.parent and isinstance(
42 stmt.parent, (nodes.Assign, nodes.AnnAssign, nodes.Tuple, nodes.For)
43 ):
44 if name in nonlocal_names:
45 return
46
47 qname = asname = None
48 if isinstance(stmt, (nodes.Import, nodes.ImportFrom)):
49 # Need the complete name, which we don't have in .locals.
50 if len(stmt.names) > 1:
51 import_names = next(
52 (names for names in stmt.names if name in names), None
53 )
54 else:
55 import_names = stmt.names[0]
56 if import_names:
57 qname, asname = import_names
58 name = asname or qname
59
60 if _has_locals_call_after_node(stmt, node.scope()):
61 message_name = "possibly-unused-variable"
62 else:
63 if isinstance(stmt, nodes.Import):
64 if asname is not None:
65 msg = f"{qname} imported as {asname}"
66 else:
67 msg = f"import {name}"
68 self.add_message("unused-import", args=msg, node=stmt)
69 return
70 if isinstance(stmt, nodes.ImportFrom):
71 if asname is not None:
72 msg = f"{qname} imported from {stmt.modname} as {asname}"
73 else:
74 msg = f"{name} imported from {stmt.modname}"
75 self.add_message("unused-import", args=msg, node=stmt)
76 return
77 message_name = "unused-variable"
78
79 if isinstance(stmt, nodes.FunctionDef) and stmt.decorators:
80 return
81
82 # Don't check function stubs created only for type information
83 if utils.is_overload_stub(node):
84 return
85
86 # Special case for exception variable
87 if isinstance(stmt.parent, nodes.ExceptHandler) and any(
88 n.name == name for n in stmt.parent.nodes_of_class(nodes.Name)
89 ):
90 return
91
92 self.add_message(message_name, args=name, node=stmt)
Path 18: 1 calls (0.0)
'__class__' (1)
FunctionDef (1)
AssignName (1)
set() (1)
set() (1)
set() (1)
1def _check_is_unused(
2 self,
3 name: str,
4 node: nodes.FunctionDef,
5 stmt: nodes.NodeNG,
6 global_names: set[str],
7 nonlocal_names: Iterable[str],
8 comprehension_target_names: Iterable[str],
9 ) -> None:
10 # Ignore some special names specified by user configuration.
11 if self._is_name_ignored(stmt, name):
12 return
13 # Ignore names that were added dynamically to the Function scope
14 if (
15 isinstance(node, nodes.FunctionDef)
16 and name == "__class__"
17 and len(node.locals["__class__"]) == 1
18 and isinstance(node.locals["__class__"][0], nodes.ClassDef)
19 ):
20 return
21
22 # Ignore names imported by the global statement.
23 if isinstance(stmt, (nodes.Global, nodes.Import, nodes.ImportFrom)):
24 # Detect imports, assigned to global statements.
25 if global_names and _import_name_is_global(stmt, global_names):
26 return
27
28 # Ignore names in comprehension targets
29 if name in comprehension_target_names:
30 return
31
32 # Ignore names in string literal type annotation.
33 if name in self._type_annotation_names:
34 return
35
36 argnames = node.argnames()
37 # Care about functions with unknown argument (builtins)
38 if name in argnames:
39 self._check_unused_arguments(name, node, stmt, argnames, nonlocal_names)
40 else:
41 if stmt.parent and isinstance(
42 stmt.parent, (nodes.Assign, nodes.AnnAssign, nodes.Tuple, nodes.For)
43 ):
44 if name in nonlocal_names:
45 return
46
47 qname = asname = None
48 if isinstance(stmt, (nodes.Import, nodes.ImportFrom)):
49 # Need the complete name, which we don't have in .locals.
50 if len(stmt.names) > 1:
51 import_names = next(
52 (names for names in stmt.names if name in names), None
53 )
54 else:
55 import_names = stmt.names[0]
56 if import_names:
57 qname, asname = import_names
58 name = asname or qname
59
60 if _has_locals_call_after_node(stmt, node.scope()):
61 message_name = "possibly-unused-variable"
62 else:
63 if isinstance(stmt, nodes.Import):
64 if asname is not None:
65 msg = f"{qname} imported as {asname}"
66 else:
67 msg = f"import {name}"
68 self.add_message("unused-import", args=msg, node=stmt)
69 return
70 if isinstance(stmt, nodes.ImportFrom):
71 if asname is not None:
72 msg = f"{qname} imported from {stmt.modname} as {asname}"
73 else:
74 msg = f"{name} imported from {stmt.modname}"
75 self.add_message("unused-import", args=msg, node=stmt)
76 return
77 message_name = "unused-variable"
78
79 if isinstance(stmt, nodes.FunctionDef) and stmt.decorators:
80 return
81
82 # Don't check function stubs created only for type information
83 if utils.is_overload_stub(node):
84 return
85
86 # Special case for exception variable
87 if isinstance(stmt.parent, nodes.ExceptHandler) and any(
88 n.name == name for n in stmt.parent.nodes_of_class(nodes.Name)
89 ):
90 return
91
92 self.add_message(message_name, args=name, node=stmt)
Path 19: 1 calls (0.0)
'value' (1)
FunctionDef (1)
AssignName (1)
set() (1)
set() (1)
set() (1)
1def _check_is_unused(
2 self,
3 name: str,
4 node: nodes.FunctionDef,
5 stmt: nodes.NodeNG,
6 global_names: set[str],
7 nonlocal_names: Iterable[str],
8 comprehension_target_names: Iterable[str],
9 ) -> None:
10 # Ignore some special names specified by user configuration.
11 if self._is_name_ignored(stmt, name):
12 return
13 # Ignore names that were added dynamically to the Function scope
14 if (
15 isinstance(node, nodes.FunctionDef)
16 and name == "__class__"
17 and len(node.locals["__class__"]) == 1
18 and isinstance(node.locals["__class__"][0], nodes.ClassDef)
19 ):
20 return
21
22 # Ignore names imported by the global statement.
23 if isinstance(stmt, (nodes.Global, nodes.Import, nodes.ImportFrom)):
24 # Detect imports, assigned to global statements.
25 if global_names and _import_name_is_global(stmt, global_names):
26 return
27
28 # Ignore names in comprehension targets
29 if name in comprehension_target_names:
30 return
31
32 # Ignore names in string literal type annotation.
33 if name in self._type_annotation_names:
34 return
35
36 argnames = node.argnames()
37 # Care about functions with unknown argument (builtins)
38 if name in argnames:
39 self._check_unused_arguments(name, node, stmt, argnames, nonlocal_names)
40 else:
41 if stmt.parent and isinstance(
42 stmt.parent, (nodes.Assign, nodes.AnnAssign, nodes.Tuple, nodes.For)
43 ):
44 if name in nonlocal_names:
45 return
46
47 qname = asname = None
48 if isinstance(stmt, (nodes.Import, nodes.ImportFrom)):
49 # Need the complete name, which we don't have in .locals.
50 if len(stmt.names) > 1:
51 import_names = next(
52 (names for names in stmt.names if name in names), None
53 )
54 else:
55 import_names = stmt.names[0]
56 if import_names:
57 qname, asname = import_names
58 name = asname or qname
59
60 if _has_locals_call_after_node(stmt, node.scope()):
61 message_name = "possibly-unused-variable"
62 else:
63 if isinstance(stmt, nodes.Import):
64 if asname is not None:
65 msg = f"{qname} imported as {asname}"
66 else:
67 msg = f"import {name}"
68 self.add_message("unused-import", args=msg, node=stmt)
69 return
70 if isinstance(stmt, nodes.ImportFrom):
71 if asname is not None:
72 msg = f"{qname} imported from {stmt.modname} as {asname}"
73 else:
74 msg = f"{name} imported from {stmt.modname}"
75 self.add_message("unused-import", args=msg, node=stmt)
76 return
77 message_name = "unused-variable"
78
79 if isinstance(stmt, nodes.FunctionDef) and stmt.decorators:
80 return
81
82 # Don't check function stubs created only for type information
83 if utils.is_overload_stub(node):
84 return
85
86 # Special case for exception variable
87 if isinstance(stmt.parent, nodes.ExceptHandler) and any(
88 n.name == name for n in stmt.parent.nodes_of_class(nodes.Name)
89 ):
90 return
91
92 self.add_message(message_name, args=name, node=stmt)
Path 20: 1 calls (0.0)
'hexdigits' (1)
FunctionDef (1)
ImportFrom (1)
set() (1)
set() (1)
set() (1)
None (1)
GeneratorExit (1)
1def _check_is_unused(
2 self,
3 name: str,
4 node: nodes.FunctionDef,
5 stmt: nodes.NodeNG,
6 global_names: set[str],
7 nonlocal_names: Iterable[str],
8 comprehension_target_names: Iterable[str],
9 ) -> None:
10 # Ignore some special names specified by user configuration.
11 if self._is_name_ignored(stmt, name):
12 return
13 # Ignore names that were added dynamically to the Function scope
14 if (
15 isinstance(node, nodes.FunctionDef)
16 and name == "__class__"
17 and len(node.locals["__class__"]) == 1
18 and isinstance(node.locals["__class__"][0], nodes.ClassDef)
19 ):
20 return
21
22 # Ignore names imported by the global statement.
23 if isinstance(stmt, (nodes.Global, nodes.Import, nodes.ImportFrom)):
24 # Detect imports, assigned to global statements.
25 if global_names and _import_name_is_global(stmt, global_names):
26 return
27
28 # Ignore names in comprehension targets
29 if name in comprehension_target_names:
30 return
31
32 # Ignore names in string literal type annotation.
33 if name in self._type_annotation_names:
34 return
35
36 argnames = node.argnames()
37 # Care about functions with unknown argument (builtins)
38 if name in argnames:
39 self._check_unused_arguments(name, node, stmt, argnames, nonlocal_names)
40 else:
41 if stmt.parent and isinstance(
42 stmt.parent, (nodes.Assign, nodes.AnnAssign, nodes.Tuple, nodes.For)
43 ):
44 if name in nonlocal_names:
45 return
46
47 qname = asname = None
48 if isinstance(stmt, (nodes.Import, nodes.ImportFrom)):
49 # Need the complete name, which we don't have in .locals.
50 if len(stmt.names) > 1:
51 import_names = next(
52 (names for names in stmt.names if name in names), None
53 )
54 else:
55 import_names = stmt.names[0]
56 if import_names:
57 qname, asname = import_names
58 name = asname or qname
59
60 if _has_locals_call_after_node(stmt, node.scope()):
61 message_name = "possibly-unused-variable"
62 else:
63 if isinstance(stmt, nodes.Import):
64 if asname is not None:
65 msg = f"{qname} imported as {asname}"
66 else:
67 msg = f"import {name}"
68 self.add_message("unused-import", args=msg, node=stmt)
69 return
70 if isinstance(stmt, nodes.ImportFrom):
71 if asname is not None:
72 msg = f"{qname} imported from {stmt.modname} as {asname}"
73 else:
74 msg = f"{name} imported from {stmt.modname}"
75 self.add_message("unused-import", args=msg, node=stmt)
76 return
77 message_name = "unused-variable"
78
79 if isinstance(stmt, nodes.FunctionDef) and stmt.decorators:
80 return
81
82 # Don't check function stubs created only for type information
83 if utils.is_overload_stub(node):
84 return
85
86 # Special case for exception variable
87 if isinstance(stmt.parent, nodes.ExceptHandler) and any(
88 n.name == name for n in stmt.parent.nodes_of_class(nodes.Name)
89 ):
90 return
91
92 self.add_message(message_name, args=name, node=stmt)
Path 21: 1 calls (0.0)
'e' (1)
FunctionDef (1)
AssignName (1)
set() (1)
set() (1)
set() (1)
None (1)
GeneratorExit (1)
1def _check_is_unused(
2 self,
3 name: str,
4 node: nodes.FunctionDef,
5 stmt: nodes.NodeNG,
6 global_names: set[str],
7 nonlocal_names: Iterable[str],
8 comprehension_target_names: Iterable[str],
9 ) -> None:
10 # Ignore some special names specified by user configuration.
11 if self._is_name_ignored(stmt, name):
12 return
13 # Ignore names that were added dynamically to the Function scope
14 if (
15 isinstance(node, nodes.FunctionDef)
16 and name == "__class__"
17 and len(node.locals["__class__"]) == 1
18 and isinstance(node.locals["__class__"][0], nodes.ClassDef)
19 ):
20 return
21
22 # Ignore names imported by the global statement.
23 if isinstance(stmt, (nodes.Global, nodes.Import, nodes.ImportFrom)):
24 # Detect imports, assigned to global statements.
25 if global_names and _import_name_is_global(stmt, global_names):
26 return
27
28 # Ignore names in comprehension targets
29 if name in comprehension_target_names:
30 return
31
32 # Ignore names in string literal type annotation.
33 if name in self._type_annotation_names:
34 return
35
36 argnames = node.argnames()
37 # Care about functions with unknown argument (builtins)
38 if name in argnames:
39 self._check_unused_arguments(name, node, stmt, argnames, nonlocal_names)
40 else:
41 if stmt.parent and isinstance(
42 stmt.parent, (nodes.Assign, nodes.AnnAssign, nodes.Tuple, nodes.For)
43 ):
44 if name in nonlocal_names:
45 return
46
47 qname = asname = None
48 if isinstance(stmt, (nodes.Import, nodes.ImportFrom)):
49 # Need the complete name, which we don't have in .locals.
50 if len(stmt.names) > 1:
51 import_names = next(
52 (names for names in stmt.names if name in names), None
53 )
54 else:
55 import_names = stmt.names[0]
56 if import_names:
57 qname, asname = import_names
58 name = asname or qname
59
60 if _has_locals_call_after_node(stmt, node.scope()):
61 message_name = "possibly-unused-variable"
62 else:
63 if isinstance(stmt, nodes.Import):
64 if asname is not None:
65 msg = f"{qname} imported as {asname}"
66 else:
67 msg = f"import {name}"
68 self.add_message("unused-import", args=msg, node=stmt)
69 return
70 if isinstance(stmt, nodes.ImportFrom):
71 if asname is not None:
72 msg = f"{qname} imported from {stmt.modname} as {asname}"
73 else:
74 msg = f"{name} imported from {stmt.modname}"
75 self.add_message("unused-import", args=msg, node=stmt)
76 return
77 message_name = "unused-variable"
78
79 if isinstance(stmt, nodes.FunctionDef) and stmt.decorators:
80 return
81
82 # Don't check function stubs created only for type information
83 if utils.is_overload_stub(node):
84 return
85
86 # Special case for exception variable
87 if isinstance(stmt.parent, nodes.ExceptHandler) and any(
88 n.name == name for n in stmt.parent.nodes_of_class(nodes.Name)
89 ):
90 return
91
92 self.add_message(message_name, args=name, node=stmt)