Method: pylint.checkers.utils.is_defined_before
Calls: 582, Exceptions: 0, Paths: 11Back
Path 1: 302 calls (0.52)
Name (302)
True (302)
1def is_defined_before(var_node: nodes.Name) -> bool:
2 """Check if the given variable node is defined before.
3
4 Verify that the variable node is defined by a parent node
5 (e.g. if or with) earlier than `var_node`, or is defined by a
6 (list, set, dict, or generator comprehension, lambda)
7 or in a previous sibling node on the same line
8 (statement_defining ; statement_using).
9 """
10 varname = var_node.name
11 for parent in var_node.node_ancestors():
12 defnode = defnode_in_scope(var_node, varname, parent)
13 if defnode is None:
14 continue
15 defnode_scope = defnode.scope()
16 if isinstance(defnode_scope, COMP_NODE_TYPES + (nodes.Lambda,)):
17 # Avoid the case where var_node_scope is a nested function
18 # FunctionDef is a Lambda until https://github.com/PyCQA/astroid/issues/291
19 if isinstance(defnode_scope, nodes.FunctionDef):
20 var_node_scope = var_node.scope()
21 if var_node_scope is not defnode_scope and isinstance(
22 var_node_scope, nodes.FunctionDef
23 ):
24 return False
25 return True
26 if defnode.lineno < var_node.lineno:
27 return True
28 # `defnode` and `var_node` on the same line
29 for defnode_anc in defnode.node_ancestors():
30 if defnode_anc.lineno != var_node.lineno:
31 continue
32 if isinstance(
33 defnode_anc,
34 (
35 nodes.For,
36 nodes.While,
37 nodes.With,
38 nodes.TryExcept,
39 nodes.TryFinally,
40 nodes.ExceptHandler,
41 ),
42 ):
43 return True
44 # possibly multiple statements on the same line using semicolon separator
45 stmt = var_node.statement(future=True)
46 _node = stmt.previous_sibling()
47 lineno = stmt.fromlineno
48 while _node and _node.fromlineno == lineno:
49 for assign_node in _node.nodes_of_class(nodes.AssignName):
50 if assign_node.name == varname:
51 return True
52 for imp_node in _node.nodes_of_class((nodes.ImportFrom, nodes.Import)):
53 if varname in [name[1] or name[0] for name in imp_node.names]:
54 return True
55 _node = _node.previous_sibling()
56 return False
Path 2: 184 calls (0.32)
Name (184)
True (184)
1def is_defined_before(var_node: nodes.Name) -> bool:
2 """Check if the given variable node is defined before.
3
4 Verify that the variable node is defined by a parent node
5 (e.g. if or with) earlier than `var_node`, or is defined by a
6 (list, set, dict, or generator comprehension, lambda)
7 or in a previous sibling node on the same line
8 (statement_defining ; statement_using).
9 """
10 varname = var_node.name
11 for parent in var_node.node_ancestors():
12 defnode = defnode_in_scope(var_node, varname, parent)
13 if defnode is None:
14 continue
15 defnode_scope = defnode.scope()
16 if isinstance(defnode_scope, COMP_NODE_TYPES + (nodes.Lambda,)):
17 # Avoid the case where var_node_scope is a nested function
18 # FunctionDef is a Lambda until https://github.com/PyCQA/astroid/issues/291
19 if isinstance(defnode_scope, nodes.FunctionDef):
20 var_node_scope = var_node.scope()
21 if var_node_scope is not defnode_scope and isinstance(
22 var_node_scope, nodes.FunctionDef
23 ):
24 return False
25 return True
26 if defnode.lineno < var_node.lineno:
27 return True
28 # `defnode` and `var_node` on the same line
29 for defnode_anc in defnode.node_ancestors():
30 if defnode_anc.lineno != var_node.lineno:
31 continue
32 if isinstance(
33 defnode_anc,
34 (
35 nodes.For,
36 nodes.While,
37 nodes.With,
38 nodes.TryExcept,
39 nodes.TryFinally,
40 nodes.ExceptHandler,
41 ),
42 ):
43 return True
44 # possibly multiple statements on the same line using semicolon separator
45 stmt = var_node.statement(future=True)
46 _node = stmt.previous_sibling()
47 lineno = stmt.fromlineno
48 while _node and _node.fromlineno == lineno:
49 for assign_node in _node.nodes_of_class(nodes.AssignName):
50 if assign_node.name == varname:
51 return True
52 for imp_node in _node.nodes_of_class((nodes.ImportFrom, nodes.Import)):
53 if varname in [name[1] or name[0] for name in imp_node.names]:
54 return True
55 _node = _node.previous_sibling()
56 return False
Path 3: 83 calls (0.14)
Name (78) AssignName (3) DelName (2)
False (83)
1def is_defined_before(var_node: nodes.Name) -> bool:
2 """Check if the given variable node is defined before.
3
4 Verify that the variable node is defined by a parent node
5 (e.g. if or with) earlier than `var_node`, or is defined by a
6 (list, set, dict, or generator comprehension, lambda)
7 or in a previous sibling node on the same line
8 (statement_defining ; statement_using).
9 """
10 varname = var_node.name
11 for parent in var_node.node_ancestors():
12 defnode = defnode_in_scope(var_node, varname, parent)
13 if defnode is None:
14 continue
15 defnode_scope = defnode.scope()
16 if isinstance(defnode_scope, COMP_NODE_TYPES + (nodes.Lambda,)):
17 # Avoid the case where var_node_scope is a nested function
18 # FunctionDef is a Lambda until https://github.com/PyCQA/astroid/issues/291
19 if isinstance(defnode_scope, nodes.FunctionDef):
20 var_node_scope = var_node.scope()
21 if var_node_scope is not defnode_scope and isinstance(
22 var_node_scope, nodes.FunctionDef
23 ):
24 return False
25 return True
26 if defnode.lineno < var_node.lineno:
27 return True
28 # `defnode` and `var_node` on the same line
29 for defnode_anc in defnode.node_ancestors():
30 if defnode_anc.lineno != var_node.lineno:
31 continue
32 if isinstance(
33 defnode_anc,
34 (
35 nodes.For,
36 nodes.While,
37 nodes.With,
38 nodes.TryExcept,
39 nodes.TryFinally,
40 nodes.ExceptHandler,
41 ),
42 ):
43 return True
44 # possibly multiple statements on the same line using semicolon separator
45 stmt = var_node.statement(future=True)
46 _node = stmt.previous_sibling()
47 lineno = stmt.fromlineno
48 while _node and _node.fromlineno == lineno:
49 for assign_node in _node.nodes_of_class(nodes.AssignName):
50 if assign_node.name == varname:
51 return True
52 for imp_node in _node.nodes_of_class((nodes.ImportFrom, nodes.Import)):
53 if varname in [name[1] or name[0] for name in imp_node.names]:
54 return True
55 _node = _node.previous_sibling()
56 return False
Path 4: 3 calls (0.01)
Name (3)
True (3)
1def is_defined_before(var_node: nodes.Name) -> bool:
2 """Check if the given variable node is defined before.
3
4 Verify that the variable node is defined by a parent node
5 (e.g. if or with) earlier than `var_node`, or is defined by a
6 (list, set, dict, or generator comprehension, lambda)
7 or in a previous sibling node on the same line
8 (statement_defining ; statement_using).
9 """
10 varname = var_node.name
11 for parent in var_node.node_ancestors():
12 defnode = defnode_in_scope(var_node, varname, parent)
13 if defnode is None:
14 continue
15 defnode_scope = defnode.scope()
16 if isinstance(defnode_scope, COMP_NODE_TYPES + (nodes.Lambda,)):
17 # Avoid the case where var_node_scope is a nested function
18 # FunctionDef is a Lambda until https://github.com/PyCQA/astroid/issues/291
19 if isinstance(defnode_scope, nodes.FunctionDef):
20 var_node_scope = var_node.scope()
21 if var_node_scope is not defnode_scope and isinstance(
22 var_node_scope, nodes.FunctionDef
23 ):
24 return False
25 return True
26 if defnode.lineno < var_node.lineno:
27 return True
28 # `defnode` and `var_node` on the same line
29 for defnode_anc in defnode.node_ancestors():
30 if defnode_anc.lineno != var_node.lineno:
31 continue
32 if isinstance(
33 defnode_anc,
34 (
35 nodes.For,
36 nodes.While,
37 nodes.With,
38 nodes.TryExcept,
39 nodes.TryFinally,
40 nodes.ExceptHandler,
41 ),
42 ):
43 return True
44 # possibly multiple statements on the same line using semicolon separator
45 stmt = var_node.statement(future=True)
46 _node = stmt.previous_sibling()
47 lineno = stmt.fromlineno
48 while _node and _node.fromlineno == lineno:
49 for assign_node in _node.nodes_of_class(nodes.AssignName):
50 if assign_node.name == varname:
51 return True
52 for imp_node in _node.nodes_of_class((nodes.ImportFrom, nodes.Import)):
53 if varname in [name[1] or name[0] for name in imp_node.names]:
54 return True
55 _node = _node.previous_sibling()
56 return False
Path 5: 3 calls (0.01)
Name (3)
False (3)
1def is_defined_before(var_node: nodes.Name) -> bool:
2 """Check if the given variable node is defined before.
3
4 Verify that the variable node is defined by a parent node
5 (e.g. if or with) earlier than `var_node`, or is defined by a
6 (list, set, dict, or generator comprehension, lambda)
7 or in a previous sibling node on the same line
8 (statement_defining ; statement_using).
9 """
10 varname = var_node.name
11 for parent in var_node.node_ancestors():
12 defnode = defnode_in_scope(var_node, varname, parent)
13 if defnode is None:
14 continue
15 defnode_scope = defnode.scope()
16 if isinstance(defnode_scope, COMP_NODE_TYPES + (nodes.Lambda,)):
17 # Avoid the case where var_node_scope is a nested function
18 # FunctionDef is a Lambda until https://github.com/PyCQA/astroid/issues/291
19 if isinstance(defnode_scope, nodes.FunctionDef):
20 var_node_scope = var_node.scope()
21 if var_node_scope is not defnode_scope and isinstance(
22 var_node_scope, nodes.FunctionDef
23 ):
24 return False
25 return True
26 if defnode.lineno < var_node.lineno:
27 return True
28 # `defnode` and `var_node` on the same line
29 for defnode_anc in defnode.node_ancestors():
30 if defnode_anc.lineno != var_node.lineno:
31 continue
32 if isinstance(
33 defnode_anc,
34 (
35 nodes.For,
36 nodes.While,
37 nodes.With,
38 nodes.TryExcept,
39 nodes.TryFinally,
40 nodes.ExceptHandler,
41 ),
42 ):
43 return True
44 # possibly multiple statements on the same line using semicolon separator
45 stmt = var_node.statement(future=True)
46 _node = stmt.previous_sibling()
47 lineno = stmt.fromlineno
48 while _node and _node.fromlineno == lineno:
49 for assign_node in _node.nodes_of_class(nodes.AssignName):
50 if assign_node.name == varname:
51 return True
52 for imp_node in _node.nodes_of_class((nodes.ImportFrom, nodes.Import)):
53 if varname in [name[1] or name[0] for name in imp_node.names]:
54 return True
55 _node = _node.previous_sibling()
56 return False
Path 6: 2 calls (0.0)
AssignName (1) Name (1)
True (2)
1def is_defined_before(var_node: nodes.Name) -> bool:
2 """Check if the given variable node is defined before.
3
4 Verify that the variable node is defined by a parent node
5 (e.g. if or with) earlier than `var_node`, or is defined by a
6 (list, set, dict, or generator comprehension, lambda)
7 or in a previous sibling node on the same line
8 (statement_defining ; statement_using).
9 """
10 varname = var_node.name
11 for parent in var_node.node_ancestors():
12 defnode = defnode_in_scope(var_node, varname, parent)
13 if defnode is None:
14 continue
15 defnode_scope = defnode.scope()
16 if isinstance(defnode_scope, COMP_NODE_TYPES + (nodes.Lambda,)):
17 # Avoid the case where var_node_scope is a nested function
18 # FunctionDef is a Lambda until https://github.com/PyCQA/astroid/issues/291
19 if isinstance(defnode_scope, nodes.FunctionDef):
20 var_node_scope = var_node.scope()
21 if var_node_scope is not defnode_scope and isinstance(
22 var_node_scope, nodes.FunctionDef
23 ):
24 return False
25 return True
26 if defnode.lineno < var_node.lineno:
27 return True
28 # `defnode` and `var_node` on the same line
29 for defnode_anc in defnode.node_ancestors():
30 if defnode_anc.lineno != var_node.lineno:
31 continue
32 if isinstance(
33 defnode_anc,
34 (
35 nodes.For,
36 nodes.While,
37 nodes.With,
38 nodes.TryExcept,
39 nodes.TryFinally,
40 nodes.ExceptHandler,
41 ),
42 ):
43 return True
44 # possibly multiple statements on the same line using semicolon separator
45 stmt = var_node.statement(future=True)
46 _node = stmt.previous_sibling()
47 lineno = stmt.fromlineno
48 while _node and _node.fromlineno == lineno:
49 for assign_node in _node.nodes_of_class(nodes.AssignName):
50 if assign_node.name == varname:
51 return True
52 for imp_node in _node.nodes_of_class((nodes.ImportFrom, nodes.Import)):
53 if varname in [name[1] or name[0] for name in imp_node.names]:
54 return True
55 _node = _node.previous_sibling()
56 return False
Path 7: 1 calls (0.0)
Name (1)
True (1)
1def is_defined_before(var_node: nodes.Name) -> bool:
2 """Check if the given variable node is defined before.
3
4 Verify that the variable node is defined by a parent node
5 (e.g. if or with) earlier than `var_node`, or is defined by a
6 (list, set, dict, or generator comprehension, lambda)
7 or in a previous sibling node on the same line
8 (statement_defining ; statement_using).
9 """
10 varname = var_node.name
11 for parent in var_node.node_ancestors():
12 defnode = defnode_in_scope(var_node, varname, parent)
13 if defnode is None:
14 continue
15 defnode_scope = defnode.scope()
16 if isinstance(defnode_scope, COMP_NODE_TYPES + (nodes.Lambda,)):
17 # Avoid the case where var_node_scope is a nested function
18 # FunctionDef is a Lambda until https://github.com/PyCQA/astroid/issues/291
19 if isinstance(defnode_scope, nodes.FunctionDef):
20 var_node_scope = var_node.scope()
21 if var_node_scope is not defnode_scope and isinstance(
22 var_node_scope, nodes.FunctionDef
23 ):
24 return False
25 return True
26 if defnode.lineno < var_node.lineno:
27 return True
28 # `defnode` and `var_node` on the same line
29 for defnode_anc in defnode.node_ancestors():
30 if defnode_anc.lineno != var_node.lineno:
31 continue
32 if isinstance(
33 defnode_anc,
34 (
35 nodes.For,
36 nodes.While,
37 nodes.With,
38 nodes.TryExcept,
39 nodes.TryFinally,
40 nodes.ExceptHandler,
41 ),
42 ):
43 return True
44 # possibly multiple statements on the same line using semicolon separator
45 stmt = var_node.statement(future=True)
46 _node = stmt.previous_sibling()
47 lineno = stmt.fromlineno
48 while _node and _node.fromlineno == lineno:
49 for assign_node in _node.nodes_of_class(nodes.AssignName):
50 if assign_node.name == varname:
51 return True
52 for imp_node in _node.nodes_of_class((nodes.ImportFrom, nodes.Import)):
53 if varname in [name[1] or name[0] for name in imp_node.names]:
54 return True
55 _node = _node.previous_sibling()
56 return False
Path 8: 1 calls (0.0)
Name (1)
True (1)
1def is_defined_before(var_node: nodes.Name) -> bool:
2 """Check if the given variable node is defined before.
3
4 Verify that the variable node is defined by a parent node
5 (e.g. if or with) earlier than `var_node`, or is defined by a
6 (list, set, dict, or generator comprehension, lambda)
7 or in a previous sibling node on the same line
8 (statement_defining ; statement_using).
9 """
10 varname = var_node.name
11 for parent in var_node.node_ancestors():
12 defnode = defnode_in_scope(var_node, varname, parent)
13 if defnode is None:
14 continue
15 defnode_scope = defnode.scope()
16 if isinstance(defnode_scope, COMP_NODE_TYPES + (nodes.Lambda,)):
17 # Avoid the case where var_node_scope is a nested function
18 # FunctionDef is a Lambda until https://github.com/PyCQA/astroid/issues/291
19 if isinstance(defnode_scope, nodes.FunctionDef):
20 var_node_scope = var_node.scope()
21 if var_node_scope is not defnode_scope and isinstance(
22 var_node_scope, nodes.FunctionDef
23 ):
24 return False
25 return True
26 if defnode.lineno < var_node.lineno:
27 return True
28 # `defnode` and `var_node` on the same line
29 for defnode_anc in defnode.node_ancestors():
30 if defnode_anc.lineno != var_node.lineno:
31 continue
32 if isinstance(
33 defnode_anc,
34 (
35 nodes.For,
36 nodes.While,
37 nodes.With,
38 nodes.TryExcept,
39 nodes.TryFinally,
40 nodes.ExceptHandler,
41 ),
42 ):
43 return True
44 # possibly multiple statements on the same line using semicolon separator
45 stmt = var_node.statement(future=True)
46 _node = stmt.previous_sibling()
47 lineno = stmt.fromlineno
48 while _node and _node.fromlineno == lineno:
49 for assign_node in _node.nodes_of_class(nodes.AssignName):
50 if assign_node.name == varname:
51 return True
52 for imp_node in _node.nodes_of_class((nodes.ImportFrom, nodes.Import)):
53 if varname in [name[1] or name[0] for name in imp_node.names]:
54 return True
55 _node = _node.previous_sibling()
56 return False
Path 9: 1 calls (0.0)
Name (1)
True (1)
1def is_defined_before(var_node: nodes.Name) -> bool:
2 """Check if the given variable node is defined before.
3
4 Verify that the variable node is defined by a parent node
5 (e.g. if or with) earlier than `var_node`, or is defined by a
6 (list, set, dict, or generator comprehension, lambda)
7 or in a previous sibling node on the same line
8 (statement_defining ; statement_using).
9 """
10 varname = var_node.name
11 for parent in var_node.node_ancestors():
12 defnode = defnode_in_scope(var_node, varname, parent)
13 if defnode is None:
14 continue
15 defnode_scope = defnode.scope()
16 if isinstance(defnode_scope, COMP_NODE_TYPES + (nodes.Lambda,)):
17 # Avoid the case where var_node_scope is a nested function
18 # FunctionDef is a Lambda until https://github.com/PyCQA/astroid/issues/291
19 if isinstance(defnode_scope, nodes.FunctionDef):
20 var_node_scope = var_node.scope()
21 if var_node_scope is not defnode_scope and isinstance(
22 var_node_scope, nodes.FunctionDef
23 ):
24 return False
25 return True
26 if defnode.lineno < var_node.lineno:
27 return True
28 # `defnode` and `var_node` on the same line
29 for defnode_anc in defnode.node_ancestors():
30 if defnode_anc.lineno != var_node.lineno:
31 continue
32 if isinstance(
33 defnode_anc,
34 (
35 nodes.For,
36 nodes.While,
37 nodes.With,
38 nodes.TryExcept,
39 nodes.TryFinally,
40 nodes.ExceptHandler,
41 ),
42 ):
43 return True
44 # possibly multiple statements on the same line using semicolon separator
45 stmt = var_node.statement(future=True)
46 _node = stmt.previous_sibling()
47 lineno = stmt.fromlineno
48 while _node and _node.fromlineno == lineno:
49 for assign_node in _node.nodes_of_class(nodes.AssignName):
50 if assign_node.name == varname:
51 return True
52 for imp_node in _node.nodes_of_class((nodes.ImportFrom, nodes.Import)):
53 if varname in [name[1] or name[0] for name in imp_node.names]:
54 return True
55 _node = _node.previous_sibling()
56 return False
Path 10: 1 calls (0.0)
Name (1)
True (1)
1def is_defined_before(var_node: nodes.Name) -> bool:
2 """Check if the given variable node is defined before.
3
4 Verify that the variable node is defined by a parent node
5 (e.g. if or with) earlier than `var_node`, or is defined by a
6 (list, set, dict, or generator comprehension, lambda)
7 or in a previous sibling node on the same line
8 (statement_defining ; statement_using).
9 """
10 varname = var_node.name
11 for parent in var_node.node_ancestors():
12 defnode = defnode_in_scope(var_node, varname, parent)
13 if defnode is None:
14 continue
15 defnode_scope = defnode.scope()
16 if isinstance(defnode_scope, COMP_NODE_TYPES + (nodes.Lambda,)):
17 # Avoid the case where var_node_scope is a nested function
18 # FunctionDef is a Lambda until https://github.com/PyCQA/astroid/issues/291
19 if isinstance(defnode_scope, nodes.FunctionDef):
20 var_node_scope = var_node.scope()
21 if var_node_scope is not defnode_scope and isinstance(
22 var_node_scope, nodes.FunctionDef
23 ):
24 return False
25 return True
26 if defnode.lineno < var_node.lineno:
27 return True
28 # `defnode` and `var_node` on the same line
29 for defnode_anc in defnode.node_ancestors():
30 if defnode_anc.lineno != var_node.lineno:
31 continue
32 if isinstance(
33 defnode_anc,
34 (
35 nodes.For,
36 nodes.While,
37 nodes.With,
38 nodes.TryExcept,
39 nodes.TryFinally,
40 nodes.ExceptHandler,
41 ),
42 ):
43 return True
44 # possibly multiple statements on the same line using semicolon separator
45 stmt = var_node.statement(future=True)
46 _node = stmt.previous_sibling()
47 lineno = stmt.fromlineno
48 while _node and _node.fromlineno == lineno:
49 for assign_node in _node.nodes_of_class(nodes.AssignName):
50 if assign_node.name == varname:
51 return True
52 for imp_node in _node.nodes_of_class((nodes.ImportFrom, nodes.Import)):
53 if varname in [name[1] or name[0] for name in imp_node.names]:
54 return True
55 _node = _node.previous_sibling()
56 return False
Path 11: 1 calls (0.0)
Name (1)
False (1)
1def is_defined_before(var_node: nodes.Name) -> bool:
2 """Check if the given variable node is defined before.
3
4 Verify that the variable node is defined by a parent node
5 (e.g. if or with) earlier than `var_node`, or is defined by a
6 (list, set, dict, or generator comprehension, lambda)
7 or in a previous sibling node on the same line
8 (statement_defining ; statement_using).
9 """
10 varname = var_node.name
11 for parent in var_node.node_ancestors():
12 defnode = defnode_in_scope(var_node, varname, parent)
13 if defnode is None:
14 continue
15 defnode_scope = defnode.scope()
16 if isinstance(defnode_scope, COMP_NODE_TYPES + (nodes.Lambda,)):
17 # Avoid the case where var_node_scope is a nested function
18 # FunctionDef is a Lambda until https://github.com/PyCQA/astroid/issues/291
19 if isinstance(defnode_scope, nodes.FunctionDef):
20 var_node_scope = var_node.scope()
21 if var_node_scope is not defnode_scope and isinstance(
22 var_node_scope, nodes.FunctionDef
23 ):
24 return False
25 return True
26 if defnode.lineno < var_node.lineno:
27 return True
28 # `defnode` and `var_node` on the same line
29 for defnode_anc in defnode.node_ancestors():
30 if defnode_anc.lineno != var_node.lineno:
31 continue
32 if isinstance(
33 defnode_anc,
34 (
35 nodes.For,
36 nodes.While,
37 nodes.With,
38 nodes.TryExcept,
39 nodes.TryFinally,
40 nodes.ExceptHandler,
41 ),
42 ):
43 return True
44 # possibly multiple statements on the same line using semicolon separator
45 stmt = var_node.statement(future=True)
46 _node = stmt.previous_sibling()
47 lineno = stmt.fromlineno
48 while _node and _node.fromlineno == lineno:
49 for assign_node in _node.nodes_of_class(nodes.AssignName):
50 if assign_node.name == varname:
51 return True
52 for imp_node in _node.nodes_of_class((nodes.ImportFrom, nodes.Import)):
53 if varname in [name[1] or name[0] for name in imp_node.names]:
54 return True
55 _node = _node.previous_sibling()
56 return False