Method: pylint.checkers.variables._detect_global_scope
Calls: 1675, Exceptions: 158, Paths: 23Back
Path 1: 310 calls (0.19)
Name (310)
FunctionDef (305) AsyncFunctionDef (5)
Module (310)
False (310)
1def _detect_global_scope(
2 node: nodes.Name, frame: nodes.LocalsDictNodeNG, defframe: nodes.LocalsDictNodeNG
3) -> bool:
4 """Detect that the given frames share a global scope.
5
6 Two frames share a global scope when neither
7 of them are hidden under a function scope, as well
8 as any parent scope of them, until the root scope.
9 In this case, depending from something defined later on
10 will only work if guarded by a nested function definition.
11
12 Example:
13 class A:
14 # B has the same global scope as `C`, leading to a NameError.
15 # Return True to indicate a shared scope.
16 class B(C): ...
17 class C: ...
18
19 Whereas this does not lead to a NameError:
20 class A:
21 def guard():
22 # Return False to indicate no scope sharing.
23 class B(C): ...
24 class C: ...
25 """
26 def_scope = scope = None
27 if frame and frame.parent:
28 scope = frame.parent.scope()
29 if defframe and defframe.parent:
30 def_scope = defframe.parent.scope()
31 if (
32 isinstance(frame, nodes.ClassDef)
33 and scope is not def_scope
34 and scope is utils.get_node_first_ancestor_of_type(node, nodes.FunctionDef)
35 ):
36 # If the current node's scope is a class nested under a function,
37 # and the def_scope is something else, then they aren't shared.
38 return False
39 if isinstance(frame, nodes.FunctionDef):
40 # If the parent of the current node is a
41 # function, then it can be under its scope (defined in); or
42 # the `->` part of annotations. The same goes
43 # for annotations of function arguments, they'll have
44 # their parent the Arguments node.
45 if frame.parent_of(defframe):
46 return node.lineno < defframe.lineno # type: ignore[no-any-return]
47 if not isinstance(node.parent, (nodes.FunctionDef, nodes.Arguments)):
48 return False
49 elif any(
50 not isinstance(f, (nodes.ClassDef, nodes.Module)) for f in (frame, defframe)
51 ):
52 # Not interested in other frames, since they are already
53 # not in a global scope.
54 return False
55
56 break_scopes = []
57 for current_scope in (scope, def_scope):
58 # Look for parent scopes. If there is anything different
59 # than a module or a class scope, then they frames don't
60 # share a global scope.
61 parent_scope = current_scope
62 while parent_scope:
63 if not isinstance(parent_scope, (nodes.ClassDef, nodes.Module)):
64 break_scopes.append(parent_scope)
65 break
66 if parent_scope.parent:
67 parent_scope = parent_scope.parent.scope()
68 else:
69 break
70 if break_scopes and len(set(break_scopes)) != 1:
71 # Store different scopes than expected.
72 # If the stored scopes are, in fact, the very same, then it means
73 # that the two frames (frame and defframe) share the same scope,
74 # and we could apply our lineno analysis over them.
75 # For instance, this works when they are inside a function, the node
76 # that uses a definition and the definition itself.
77 return False
78 # At this point, we are certain that frame and defframe share a scope
79 # and the definition of the first depends on the second.
80 return frame.lineno < defframe.lineno # type: ignore[no-any-return]
Path 2: 278 calls (0.17)
Name (278)
FunctionDef (258) AsyncFunctionDef (20)
FunctionDef (137) ClassDef (132) AsyncFunctionDef (9)
False (278)
1def _detect_global_scope(
2 node: nodes.Name, frame: nodes.LocalsDictNodeNG, defframe: nodes.LocalsDictNodeNG
3) -> bool:
4 """Detect that the given frames share a global scope.
5
6 Two frames share a global scope when neither
7 of them are hidden under a function scope, as well
8 as any parent scope of them, until the root scope.
9 In this case, depending from something defined later on
10 will only work if guarded by a nested function definition.
11
12 Example:
13 class A:
14 # B has the same global scope as `C`, leading to a NameError.
15 # Return True to indicate a shared scope.
16 class B(C): ...
17 class C: ...
18
19 Whereas this does not lead to a NameError:
20 class A:
21 def guard():
22 # Return False to indicate no scope sharing.
23 class B(C): ...
24 class C: ...
25 """
26 def_scope = scope = None
27 if frame and frame.parent:
28 scope = frame.parent.scope()
29 if defframe and defframe.parent:
30 def_scope = defframe.parent.scope()
31 if (
32 isinstance(frame, nodes.ClassDef)
33 and scope is not def_scope
34 and scope is utils.get_node_first_ancestor_of_type(node, nodes.FunctionDef)
35 ):
36 # If the current node's scope is a class nested under a function,
37 # and the def_scope is something else, then they aren't shared.
38 return False
39 if isinstance(frame, nodes.FunctionDef):
40 # If the parent of the current node is a
41 # function, then it can be under its scope (defined in); or
42 # the `->` part of annotations. The same goes
43 # for annotations of function arguments, they'll have
44 # their parent the Arguments node.
45 if frame.parent_of(defframe):
46 return node.lineno < defframe.lineno # type: ignore[no-any-return]
47 if not isinstance(node.parent, (nodes.FunctionDef, nodes.Arguments)):
48 return False
49 elif any(
50 not isinstance(f, (nodes.ClassDef, nodes.Module)) for f in (frame, defframe)
51 ):
52 # Not interested in other frames, since they are already
53 # not in a global scope.
54 return False
55
56 break_scopes = []
57 for current_scope in (scope, def_scope):
58 # Look for parent scopes. If there is anything different
59 # than a module or a class scope, then they frames don't
60 # share a global scope.
61 parent_scope = current_scope
62 while parent_scope:
63 if not isinstance(parent_scope, (nodes.ClassDef, nodes.Module)):
64 break_scopes.append(parent_scope)
65 break
66 if parent_scope.parent:
67 parent_scope = parent_scope.parent.scope()
68 else:
69 break
70 if break_scopes and len(set(break_scopes)) != 1:
71 # Store different scopes than expected.
72 # If the stored scopes are, in fact, the very same, then it means
73 # that the two frames (frame and defframe) share the same scope,
74 # and we could apply our lineno analysis over them.
75 # For instance, this works when they are inside a function, the node
76 # that uses a definition and the definition itself.
77 return False
78 # At this point, we are certain that frame and defframe share a scope
79 # and the definition of the first depends on the second.
80 return frame.lineno < defframe.lineno # type: ignore[no-any-return]
Path 3: 246 calls (0.15)
Name (246)
ClassDef (246)
ClassDef (246)
False (243) True (3)
1def _detect_global_scope(
2 node: nodes.Name, frame: nodes.LocalsDictNodeNG, defframe: nodes.LocalsDictNodeNG
3) -> bool:
4 """Detect that the given frames share a global scope.
5
6 Two frames share a global scope when neither
7 of them are hidden under a function scope, as well
8 as any parent scope of them, until the root scope.
9 In this case, depending from something defined later on
10 will only work if guarded by a nested function definition.
11
12 Example:
13 class A:
14 # B has the same global scope as `C`, leading to a NameError.
15 # Return True to indicate a shared scope.
16 class B(C): ...
17 class C: ...
18
19 Whereas this does not lead to a NameError:
20 class A:
21 def guard():
22 # Return False to indicate no scope sharing.
23 class B(C): ...
24 class C: ...
25 """
26 def_scope = scope = None
27 if frame and frame.parent:
28 scope = frame.parent.scope()
29 if defframe and defframe.parent:
30 def_scope = defframe.parent.scope()
31 if (
32 isinstance(frame, nodes.ClassDef)
33 and scope is not def_scope
34 and scope is utils.get_node_first_ancestor_of_type(node, nodes.FunctionDef)
35 ):
36 # If the current node's scope is a class nested under a function,
37 # and the def_scope is something else, then they aren't shared.
38 return False
39 if isinstance(frame, nodes.FunctionDef):
40 # If the parent of the current node is a
41 # function, then it can be under its scope (defined in); or
42 # the `->` part of annotations. The same goes
43 # for annotations of function arguments, they'll have
44 # their parent the Arguments node.
45 if frame.parent_of(defframe):
46 return node.lineno < defframe.lineno # type: ignore[no-any-return]
47 if not isinstance(node.parent, (nodes.FunctionDef, nodes.Arguments)):
48 return False
49 elif any(
50 not isinstance(f, (nodes.ClassDef, nodes.Module)) for f in (frame, defframe)
51 ):
52 # Not interested in other frames, since they are already
53 # not in a global scope.
54 return False
55
56 break_scopes = []
57 for current_scope in (scope, def_scope):
58 # Look for parent scopes. If there is anything different
59 # than a module or a class scope, then they frames don't
60 # share a global scope.
61 parent_scope = current_scope
62 while parent_scope:
63 if not isinstance(parent_scope, (nodes.ClassDef, nodes.Module)):
64 break_scopes.append(parent_scope)
65 break
66 if parent_scope.parent:
67 parent_scope = parent_scope.parent.scope()
68 else:
69 break
70 if break_scopes and len(set(break_scopes)) != 1:
71 # Store different scopes than expected.
72 # If the stored scopes are, in fact, the very same, then it means
73 # that the two frames (frame and defframe) share the same scope,
74 # and we could apply our lineno analysis over them.
75 # For instance, this works when they are inside a function, the node
76 # that uses a definition and the definition itself.
77 return False
78 # At this point, we are certain that frame and defframe share a scope
79 # and the definition of the first depends on the second.
80 return frame.lineno < defframe.lineno # type: ignore[no-any-return]
Path 4: 226 calls (0.13)
Name (226)
ClassDef (226)
Module (226)
False (226)
1def _detect_global_scope(
2 node: nodes.Name, frame: nodes.LocalsDictNodeNG, defframe: nodes.LocalsDictNodeNG
3) -> bool:
4 """Detect that the given frames share a global scope.
5
6 Two frames share a global scope when neither
7 of them are hidden under a function scope, as well
8 as any parent scope of them, until the root scope.
9 In this case, depending from something defined later on
10 will only work if guarded by a nested function definition.
11
12 Example:
13 class A:
14 # B has the same global scope as `C`, leading to a NameError.
15 # Return True to indicate a shared scope.
16 class B(C): ...
17 class C: ...
18
19 Whereas this does not lead to a NameError:
20 class A:
21 def guard():
22 # Return False to indicate no scope sharing.
23 class B(C): ...
24 class C: ...
25 """
26 def_scope = scope = None
27 if frame and frame.parent:
28 scope = frame.parent.scope()
29 if defframe and defframe.parent:
30 def_scope = defframe.parent.scope()
31 if (
32 isinstance(frame, nodes.ClassDef)
33 and scope is not def_scope
34 and scope is utils.get_node_first_ancestor_of_type(node, nodes.FunctionDef)
35 ):
36 # If the current node's scope is a class nested under a function,
37 # and the def_scope is something else, then they aren't shared.
38 return False
39 if isinstance(frame, nodes.FunctionDef):
40 # If the parent of the current node is a
41 # function, then it can be under its scope (defined in); or
42 # the `->` part of annotations. The same goes
43 # for annotations of function arguments, they'll have
44 # their parent the Arguments node.
45 if frame.parent_of(defframe):
46 return node.lineno < defframe.lineno # type: ignore[no-any-return]
47 if not isinstance(node.parent, (nodes.FunctionDef, nodes.Arguments)):
48 return False
49 elif any(
50 not isinstance(f, (nodes.ClassDef, nodes.Module)) for f in (frame, defframe)
51 ):
52 # Not interested in other frames, since they are already
53 # not in a global scope.
54 return False
55
56 break_scopes = []
57 for current_scope in (scope, def_scope):
58 # Look for parent scopes. If there is anything different
59 # than a module or a class scope, then they frames don't
60 # share a global scope.
61 parent_scope = current_scope
62 while parent_scope:
63 if not isinstance(parent_scope, (nodes.ClassDef, nodes.Module)):
64 break_scopes.append(parent_scope)
65 break
66 if parent_scope.parent:
67 parent_scope = parent_scope.parent.scope()
68 else:
69 break
70 if break_scopes and len(set(break_scopes)) != 1:
71 # Store different scopes than expected.
72 # If the stored scopes are, in fact, the very same, then it means
73 # that the two frames (frame and defframe) share the same scope,
74 # and we could apply our lineno analysis over them.
75 # For instance, this works when they are inside a function, the node
76 # that uses a definition and the definition itself.
77 return False
78 # At this point, we are certain that frame and defframe share a scope
79 # and the definition of the first depends on the second.
80 return frame.lineno < defframe.lineno # type: ignore[no-any-return]
Path 5: 200 calls (0.12)
Name (200)
Module (200)
ClassDef (200)
True (200)
1def _detect_global_scope(
2 node: nodes.Name, frame: nodes.LocalsDictNodeNG, defframe: nodes.LocalsDictNodeNG
3) -> bool:
4 """Detect that the given frames share a global scope.
5
6 Two frames share a global scope when neither
7 of them are hidden under a function scope, as well
8 as any parent scope of them, until the root scope.
9 In this case, depending from something defined later on
10 will only work if guarded by a nested function definition.
11
12 Example:
13 class A:
14 # B has the same global scope as `C`, leading to a NameError.
15 # Return True to indicate a shared scope.
16 class B(C): ...
17 class C: ...
18
19 Whereas this does not lead to a NameError:
20 class A:
21 def guard():
22 # Return False to indicate no scope sharing.
23 class B(C): ...
24 class C: ...
25 """
26 def_scope = scope = None
27 if frame and frame.parent:
28 scope = frame.parent.scope()
29 if defframe and defframe.parent:
30 def_scope = defframe.parent.scope()
31 if (
32 isinstance(frame, nodes.ClassDef)
33 and scope is not def_scope
34 and scope is utils.get_node_first_ancestor_of_type(node, nodes.FunctionDef)
35 ):
36 # If the current node's scope is a class nested under a function,
37 # and the def_scope is something else, then they aren't shared.
38 return False
39 if isinstance(frame, nodes.FunctionDef):
40 # If the parent of the current node is a
41 # function, then it can be under its scope (defined in); or
42 # the `->` part of annotations. The same goes
43 # for annotations of function arguments, they'll have
44 # their parent the Arguments node.
45 if frame.parent_of(defframe):
46 return node.lineno < defframe.lineno # type: ignore[no-any-return]
47 if not isinstance(node.parent, (nodes.FunctionDef, nodes.Arguments)):
48 return False
49 elif any(
50 not isinstance(f, (nodes.ClassDef, nodes.Module)) for f in (frame, defframe)
51 ):
52 # Not interested in other frames, since they are already
53 # not in a global scope.
54 return False
55
56 break_scopes = []
57 for current_scope in (scope, def_scope):
58 # Look for parent scopes. If there is anything different
59 # than a module or a class scope, then they frames don't
60 # share a global scope.
61 parent_scope = current_scope
62 while parent_scope:
63 if not isinstance(parent_scope, (nodes.ClassDef, nodes.Module)):
64 break_scopes.append(parent_scope)
65 break
66 if parent_scope.parent:
67 parent_scope = parent_scope.parent.scope()
68 else:
69 break
70 if break_scopes and len(set(break_scopes)) != 1:
71 # Store different scopes than expected.
72 # If the stored scopes are, in fact, the very same, then it means
73 # that the two frames (frame and defframe) share the same scope,
74 # and we could apply our lineno analysis over them.
75 # For instance, this works when they are inside a function, the node
76 # that uses a definition and the definition itself.
77 return False
78 # At this point, we are certain that frame and defframe share a scope
79 # and the definition of the first depends on the second.
80 return frame.lineno < defframe.lineno # type: ignore[no-any-return]
Path 6: 151 calls (0.09)
Name (151)
FunctionDef (151)
FunctionDef (106) ClassDef (44) AsyncFunctionDef (1)
False (150) True (1)
1def _detect_global_scope(
2 node: nodes.Name, frame: nodes.LocalsDictNodeNG, defframe: nodes.LocalsDictNodeNG
3) -> bool:
4 """Detect that the given frames share a global scope.
5
6 Two frames share a global scope when neither
7 of them are hidden under a function scope, as well
8 as any parent scope of them, until the root scope.
9 In this case, depending from something defined later on
10 will only work if guarded by a nested function definition.
11
12 Example:
13 class A:
14 # B has the same global scope as `C`, leading to a NameError.
15 # Return True to indicate a shared scope.
16 class B(C): ...
17 class C: ...
18
19 Whereas this does not lead to a NameError:
20 class A:
21 def guard():
22 # Return False to indicate no scope sharing.
23 class B(C): ...
24 class C: ...
25 """
26 def_scope = scope = None
27 if frame and frame.parent:
28 scope = frame.parent.scope()
29 if defframe and defframe.parent:
30 def_scope = defframe.parent.scope()
31 if (
32 isinstance(frame, nodes.ClassDef)
33 and scope is not def_scope
34 and scope is utils.get_node_first_ancestor_of_type(node, nodes.FunctionDef)
35 ):
36 # If the current node's scope is a class nested under a function,
37 # and the def_scope is something else, then they aren't shared.
38 return False
39 if isinstance(frame, nodes.FunctionDef):
40 # If the parent of the current node is a
41 # function, then it can be under its scope (defined in); or
42 # the `->` part of annotations. The same goes
43 # for annotations of function arguments, they'll have
44 # their parent the Arguments node.
45 if frame.parent_of(defframe):
46 return node.lineno < defframe.lineno # type: ignore[no-any-return]
47 if not isinstance(node.parent, (nodes.FunctionDef, nodes.Arguments)):
48 return False
49 elif any(
50 not isinstance(f, (nodes.ClassDef, nodes.Module)) for f in (frame, defframe)
51 ):
52 # Not interested in other frames, since they are already
53 # not in a global scope.
54 return False
55
56 break_scopes = []
57 for current_scope in (scope, def_scope):
58 # Look for parent scopes. If there is anything different
59 # than a module or a class scope, then they frames don't
60 # share a global scope.
61 parent_scope = current_scope
62 while parent_scope:
63 if not isinstance(parent_scope, (nodes.ClassDef, nodes.Module)):
64 break_scopes.append(parent_scope)
65 break
66 if parent_scope.parent:
67 parent_scope = parent_scope.parent.scope()
68 else:
69 break
70 if break_scopes and len(set(break_scopes)) != 1:
71 # Store different scopes than expected.
72 # If the stored scopes are, in fact, the very same, then it means
73 # that the two frames (frame and defframe) share the same scope,
74 # and we could apply our lineno analysis over them.
75 # For instance, this works when they are inside a function, the node
76 # that uses a definition and the definition itself.
77 return False
78 # At this point, we are certain that frame and defframe share a scope
79 # and the definition of the first depends on the second.
80 return frame.lineno < defframe.lineno # type: ignore[no-any-return]
Path 7: 118 calls (0.07)
Name (118)
Module (118)
FunctionDef (116) AsyncFunctionDef (2)
False (118)
GeneratorExit (118)
1def _detect_global_scope(
2 node: nodes.Name, frame: nodes.LocalsDictNodeNG, defframe: nodes.LocalsDictNodeNG
3) -> bool:
4 """Detect that the given frames share a global scope.
5
6 Two frames share a global scope when neither
7 of them are hidden under a function scope, as well
8 as any parent scope of them, until the root scope.
9 In this case, depending from something defined later on
10 will only work if guarded by a nested function definition.
11
12 Example:
13 class A:
14 # B has the same global scope as `C`, leading to a NameError.
15 # Return True to indicate a shared scope.
16 class B(C): ...
17 class C: ...
18
19 Whereas this does not lead to a NameError:
20 class A:
21 def guard():
22 # Return False to indicate no scope sharing.
23 class B(C): ...
24 class C: ...
25 """
26 def_scope = scope = None
27 if frame and frame.parent:
28 scope = frame.parent.scope()
29 if defframe and defframe.parent:
30 def_scope = defframe.parent.scope()
31 if (
32 isinstance(frame, nodes.ClassDef)
33 and scope is not def_scope
34 and scope is utils.get_node_first_ancestor_of_type(node, nodes.FunctionDef)
35 ):
36 # If the current node's scope is a class nested under a function,
37 # and the def_scope is something else, then they aren't shared.
38 return False
39 if isinstance(frame, nodes.FunctionDef):
40 # If the parent of the current node is a
41 # function, then it can be under its scope (defined in); or
42 # the `->` part of annotations. The same goes
43 # for annotations of function arguments, they'll have
44 # their parent the Arguments node.
45 if frame.parent_of(defframe):
46 return node.lineno < defframe.lineno # type: ignore[no-any-return]
47 if not isinstance(node.parent, (nodes.FunctionDef, nodes.Arguments)):
48 return False
49 elif any(
50 not isinstance(f, (nodes.ClassDef, nodes.Module)) for f in (frame, defframe)
51 ):
52 # Not interested in other frames, since they are already
53 # not in a global scope.
54 return False
55
56 break_scopes = []
57 for current_scope in (scope, def_scope):
58 # Look for parent scopes. If there is anything different
59 # than a module or a class scope, then they frames don't
60 # share a global scope.
61 parent_scope = current_scope
62 while parent_scope:
63 if not isinstance(parent_scope, (nodes.ClassDef, nodes.Module)):
64 break_scopes.append(parent_scope)
65 break
66 if parent_scope.parent:
67 parent_scope = parent_scope.parent.scope()
68 else:
69 break
70 if break_scopes and len(set(break_scopes)) != 1:
71 # Store different scopes than expected.
72 # If the stored scopes are, in fact, the very same, then it means
73 # that the two frames (frame and defframe) share the same scope,
74 # and we could apply our lineno analysis over them.
75 # For instance, this works when they are inside a function, the node
76 # that uses a definition and the definition itself.
77 return False
78 # At this point, we are certain that frame and defframe share a scope
79 # and the definition of the first depends on the second.
80 return frame.lineno < defframe.lineno # type: ignore[no-any-return]
Path 8: 30 calls (0.02)
Name (30)
ClassDef (30)
FunctionDef (30)
False (30)
GeneratorExit (30)
1def _detect_global_scope(
2 node: nodes.Name, frame: nodes.LocalsDictNodeNG, defframe: nodes.LocalsDictNodeNG
3) -> bool:
4 """Detect that the given frames share a global scope.
5
6 Two frames share a global scope when neither
7 of them are hidden under a function scope, as well
8 as any parent scope of them, until the root scope.
9 In this case, depending from something defined later on
10 will only work if guarded by a nested function definition.
11
12 Example:
13 class A:
14 # B has the same global scope as `C`, leading to a NameError.
15 # Return True to indicate a shared scope.
16 class B(C): ...
17 class C: ...
18
19 Whereas this does not lead to a NameError:
20 class A:
21 def guard():
22 # Return False to indicate no scope sharing.
23 class B(C): ...
24 class C: ...
25 """
26 def_scope = scope = None
27 if frame and frame.parent:
28 scope = frame.parent.scope()
29 if defframe and defframe.parent:
30 def_scope = defframe.parent.scope()
31 if (
32 isinstance(frame, nodes.ClassDef)
33 and scope is not def_scope
34 and scope is utils.get_node_first_ancestor_of_type(node, nodes.FunctionDef)
35 ):
36 # If the current node's scope is a class nested under a function,
37 # and the def_scope is something else, then they aren't shared.
38 return False
39 if isinstance(frame, nodes.FunctionDef):
40 # If the parent of the current node is a
41 # function, then it can be under its scope (defined in); or
42 # the `->` part of annotations. The same goes
43 # for annotations of function arguments, they'll have
44 # their parent the Arguments node.
45 if frame.parent_of(defframe):
46 return node.lineno < defframe.lineno # type: ignore[no-any-return]
47 if not isinstance(node.parent, (nodes.FunctionDef, nodes.Arguments)):
48 return False
49 elif any(
50 not isinstance(f, (nodes.ClassDef, nodes.Module)) for f in (frame, defframe)
51 ):
52 # Not interested in other frames, since they are already
53 # not in a global scope.
54 return False
55
56 break_scopes = []
57 for current_scope in (scope, def_scope):
58 # Look for parent scopes. If there is anything different
59 # than a module or a class scope, then they frames don't
60 # share a global scope.
61 parent_scope = current_scope
62 while parent_scope:
63 if not isinstance(parent_scope, (nodes.ClassDef, nodes.Module)):
64 break_scopes.append(parent_scope)
65 break
66 if parent_scope.parent:
67 parent_scope = parent_scope.parent.scope()
68 else:
69 break
70 if break_scopes and len(set(break_scopes)) != 1:
71 # Store different scopes than expected.
72 # If the stored scopes are, in fact, the very same, then it means
73 # that the two frames (frame and defframe) share the same scope,
74 # and we could apply our lineno analysis over them.
75 # For instance, this works when they are inside a function, the node
76 # that uses a definition and the definition itself.
77 return False
78 # At this point, we are certain that frame and defframe share a scope
79 # and the definition of the first depends on the second.
80 return frame.lineno < defframe.lineno # type: ignore[no-any-return]
Path 9: 24 calls (0.01)
Name (24)
FunctionDef (23) AsyncFunctionDef (1)
ClassDef (24)
False (21) True (3)
1def _detect_global_scope(
2 node: nodes.Name, frame: nodes.LocalsDictNodeNG, defframe: nodes.LocalsDictNodeNG
3) -> bool:
4 """Detect that the given frames share a global scope.
5
6 Two frames share a global scope when neither
7 of them are hidden under a function scope, as well
8 as any parent scope of them, until the root scope.
9 In this case, depending from something defined later on
10 will only work if guarded by a nested function definition.
11
12 Example:
13 class A:
14 # B has the same global scope as `C`, leading to a NameError.
15 # Return True to indicate a shared scope.
16 class B(C): ...
17 class C: ...
18
19 Whereas this does not lead to a NameError:
20 class A:
21 def guard():
22 # Return False to indicate no scope sharing.
23 class B(C): ...
24 class C: ...
25 """
26 def_scope = scope = None
27 if frame and frame.parent:
28 scope = frame.parent.scope()
29 if defframe and defframe.parent:
30 def_scope = defframe.parent.scope()
31 if (
32 isinstance(frame, nodes.ClassDef)
33 and scope is not def_scope
34 and scope is utils.get_node_first_ancestor_of_type(node, nodes.FunctionDef)
35 ):
36 # If the current node's scope is a class nested under a function,
37 # and the def_scope is something else, then they aren't shared.
38 return False
39 if isinstance(frame, nodes.FunctionDef):
40 # If the parent of the current node is a
41 # function, then it can be under its scope (defined in); or
42 # the `->` part of annotations. The same goes
43 # for annotations of function arguments, they'll have
44 # their parent the Arguments node.
45 if frame.parent_of(defframe):
46 return node.lineno < defframe.lineno # type: ignore[no-any-return]
47 if not isinstance(node.parent, (nodes.FunctionDef, nodes.Arguments)):
48 return False
49 elif any(
50 not isinstance(f, (nodes.ClassDef, nodes.Module)) for f in (frame, defframe)
51 ):
52 # Not interested in other frames, since they are already
53 # not in a global scope.
54 return False
55
56 break_scopes = []
57 for current_scope in (scope, def_scope):
58 # Look for parent scopes. If there is anything different
59 # than a module or a class scope, then they frames don't
60 # share a global scope.
61 parent_scope = current_scope
62 while parent_scope:
63 if not isinstance(parent_scope, (nodes.ClassDef, nodes.Module)):
64 break_scopes.append(parent_scope)
65 break
66 if parent_scope.parent:
67 parent_scope = parent_scope.parent.scope()
68 else:
69 break
70 if break_scopes and len(set(break_scopes)) != 1:
71 # Store different scopes than expected.
72 # If the stored scopes are, in fact, the very same, then it means
73 # that the two frames (frame and defframe) share the same scope,
74 # and we could apply our lineno analysis over them.
75 # For instance, this works when they are inside a function, the node
76 # that uses a definition and the definition itself.
77 return False
78 # At this point, we are certain that frame and defframe share a scope
79 # and the definition of the first depends on the second.
80 return frame.lineno < defframe.lineno # type: ignore[no-any-return]
Path 10: 24 calls (0.01)
Name (24)
FunctionDef (24)
Module (24)
False (24)
1def _detect_global_scope(
2 node: nodes.Name, frame: nodes.LocalsDictNodeNG, defframe: nodes.LocalsDictNodeNG
3) -> bool:
4 """Detect that the given frames share a global scope.
5
6 Two frames share a global scope when neither
7 of them are hidden under a function scope, as well
8 as any parent scope of them, until the root scope.
9 In this case, depending from something defined later on
10 will only work if guarded by a nested function definition.
11
12 Example:
13 class A:
14 # B has the same global scope as `C`, leading to a NameError.
15 # Return True to indicate a shared scope.
16 class B(C): ...
17 class C: ...
18
19 Whereas this does not lead to a NameError:
20 class A:
21 def guard():
22 # Return False to indicate no scope sharing.
23 class B(C): ...
24 class C: ...
25 """
26 def_scope = scope = None
27 if frame and frame.parent:
28 scope = frame.parent.scope()
29 if defframe and defframe.parent:
30 def_scope = defframe.parent.scope()
31 if (
32 isinstance(frame, nodes.ClassDef)
33 and scope is not def_scope
34 and scope is utils.get_node_first_ancestor_of_type(node, nodes.FunctionDef)
35 ):
36 # If the current node's scope is a class nested under a function,
37 # and the def_scope is something else, then they aren't shared.
38 return False
39 if isinstance(frame, nodes.FunctionDef):
40 # If the parent of the current node is a
41 # function, then it can be under its scope (defined in); or
42 # the `->` part of annotations. The same goes
43 # for annotations of function arguments, they'll have
44 # their parent the Arguments node.
45 if frame.parent_of(defframe):
46 return node.lineno < defframe.lineno # type: ignore[no-any-return]
47 if not isinstance(node.parent, (nodes.FunctionDef, nodes.Arguments)):
48 return False
49 elif any(
50 not isinstance(f, (nodes.ClassDef, nodes.Module)) for f in (frame, defframe)
51 ):
52 # Not interested in other frames, since they are already
53 # not in a global scope.
54 return False
55
56 break_scopes = []
57 for current_scope in (scope, def_scope):
58 # Look for parent scopes. If there is anything different
59 # than a module or a class scope, then they frames don't
60 # share a global scope.
61 parent_scope = current_scope
62 while parent_scope:
63 if not isinstance(parent_scope, (nodes.ClassDef, nodes.Module)):
64 break_scopes.append(parent_scope)
65 break
66 if parent_scope.parent:
67 parent_scope = parent_scope.parent.scope()
68 else:
69 break
70 if break_scopes and len(set(break_scopes)) != 1:
71 # Store different scopes than expected.
72 # If the stored scopes are, in fact, the very same, then it means
73 # that the two frames (frame and defframe) share the same scope,
74 # and we could apply our lineno analysis over them.
75 # For instance, this works when they are inside a function, the node
76 # that uses a definition and the definition itself.
77 return False
78 # At this point, we are certain that frame and defframe share a scope
79 # and the definition of the first depends on the second.
80 return frame.lineno < defframe.lineno # type: ignore[no-any-return]
Path 11: 10 calls (0.01)
Name (10)
ClassDef (10)
FunctionDef (10)
False (10)
GeneratorExit (10)
1def _detect_global_scope(
2 node: nodes.Name, frame: nodes.LocalsDictNodeNG, defframe: nodes.LocalsDictNodeNG
3) -> bool:
4 """Detect that the given frames share a global scope.
5
6 Two frames share a global scope when neither
7 of them are hidden under a function scope, as well
8 as any parent scope of them, until the root scope.
9 In this case, depending from something defined later on
10 will only work if guarded by a nested function definition.
11
12 Example:
13 class A:
14 # B has the same global scope as `C`, leading to a NameError.
15 # Return True to indicate a shared scope.
16 class B(C): ...
17 class C: ...
18
19 Whereas this does not lead to a NameError:
20 class A:
21 def guard():
22 # Return False to indicate no scope sharing.
23 class B(C): ...
24 class C: ...
25 """
26 def_scope = scope = None
27 if frame and frame.parent:
28 scope = frame.parent.scope()
29 if defframe and defframe.parent:
30 def_scope = defframe.parent.scope()
31 if (
32 isinstance(frame, nodes.ClassDef)
33 and scope is not def_scope
34 and scope is utils.get_node_first_ancestor_of_type(node, nodes.FunctionDef)
35 ):
36 # If the current node's scope is a class nested under a function,
37 # and the def_scope is something else, then they aren't shared.
38 return False
39 if isinstance(frame, nodes.FunctionDef):
40 # If the parent of the current node is a
41 # function, then it can be under its scope (defined in); or
42 # the `->` part of annotations. The same goes
43 # for annotations of function arguments, they'll have
44 # their parent the Arguments node.
45 if frame.parent_of(defframe):
46 return node.lineno < defframe.lineno # type: ignore[no-any-return]
47 if not isinstance(node.parent, (nodes.FunctionDef, nodes.Arguments)):
48 return False
49 elif any(
50 not isinstance(f, (nodes.ClassDef, nodes.Module)) for f in (frame, defframe)
51 ):
52 # Not interested in other frames, since they are already
53 # not in a global scope.
54 return False
55
56 break_scopes = []
57 for current_scope in (scope, def_scope):
58 # Look for parent scopes. If there is anything different
59 # than a module or a class scope, then they frames don't
60 # share a global scope.
61 parent_scope = current_scope
62 while parent_scope:
63 if not isinstance(parent_scope, (nodes.ClassDef, nodes.Module)):
64 break_scopes.append(parent_scope)
65 break
66 if parent_scope.parent:
67 parent_scope = parent_scope.parent.scope()
68 else:
69 break
70 if break_scopes and len(set(break_scopes)) != 1:
71 # Store different scopes than expected.
72 # If the stored scopes are, in fact, the very same, then it means
73 # that the two frames (frame and defframe) share the same scope,
74 # and we could apply our lineno analysis over them.
75 # For instance, this works when they are inside a function, the node
76 # that uses a definition and the definition itself.
77 return False
78 # At this point, we are certain that frame and defframe share a scope
79 # and the definition of the first depends on the second.
80 return frame.lineno < defframe.lineno # type: ignore[no-any-return]
Path 12: 10 calls (0.01)
Name (10)
FunctionDef (10)
Module (10)
False (10)
1def _detect_global_scope(
2 node: nodes.Name, frame: nodes.LocalsDictNodeNG, defframe: nodes.LocalsDictNodeNG
3) -> bool:
4 """Detect that the given frames share a global scope.
5
6 Two frames share a global scope when neither
7 of them are hidden under a function scope, as well
8 as any parent scope of them, until the root scope.
9 In this case, depending from something defined later on
10 will only work if guarded by a nested function definition.
11
12 Example:
13 class A:
14 # B has the same global scope as `C`, leading to a NameError.
15 # Return True to indicate a shared scope.
16 class B(C): ...
17 class C: ...
18
19 Whereas this does not lead to a NameError:
20 class A:
21 def guard():
22 # Return False to indicate no scope sharing.
23 class B(C): ...
24 class C: ...
25 """
26 def_scope = scope = None
27 if frame and frame.parent:
28 scope = frame.parent.scope()
29 if defframe and defframe.parent:
30 def_scope = defframe.parent.scope()
31 if (
32 isinstance(frame, nodes.ClassDef)
33 and scope is not def_scope
34 and scope is utils.get_node_first_ancestor_of_type(node, nodes.FunctionDef)
35 ):
36 # If the current node's scope is a class nested under a function,
37 # and the def_scope is something else, then they aren't shared.
38 return False
39 if isinstance(frame, nodes.FunctionDef):
40 # If the parent of the current node is a
41 # function, then it can be under its scope (defined in); or
42 # the `->` part of annotations. The same goes
43 # for annotations of function arguments, they'll have
44 # their parent the Arguments node.
45 if frame.parent_of(defframe):
46 return node.lineno < defframe.lineno # type: ignore[no-any-return]
47 if not isinstance(node.parent, (nodes.FunctionDef, nodes.Arguments)):
48 return False
49 elif any(
50 not isinstance(f, (nodes.ClassDef, nodes.Module)) for f in (frame, defframe)
51 ):
52 # Not interested in other frames, since they are already
53 # not in a global scope.
54 return False
55
56 break_scopes = []
57 for current_scope in (scope, def_scope):
58 # Look for parent scopes. If there is anything different
59 # than a module or a class scope, then they frames don't
60 # share a global scope.
61 parent_scope = current_scope
62 while parent_scope:
63 if not isinstance(parent_scope, (nodes.ClassDef, nodes.Module)):
64 break_scopes.append(parent_scope)
65 break
66 if parent_scope.parent:
67 parent_scope = parent_scope.parent.scope()
68 else:
69 break
70 if break_scopes and len(set(break_scopes)) != 1:
71 # Store different scopes than expected.
72 # If the stored scopes are, in fact, the very same, then it means
73 # that the two frames (frame and defframe) share the same scope,
74 # and we could apply our lineno analysis over them.
75 # For instance, this works when they are inside a function, the node
76 # that uses a definition and the definition itself.
77 return False
78 # At this point, we are certain that frame and defframe share a scope
79 # and the definition of the first depends on the second.
80 return frame.lineno < defframe.lineno # type: ignore[no-any-return]
Path 13: 9 calls (0.01)
Name (9)
FunctionDef (9)
ClassDef (5) FunctionDef (4)
True (5) False (4)
1def _detect_global_scope(
2 node: nodes.Name, frame: nodes.LocalsDictNodeNG, defframe: nodes.LocalsDictNodeNG
3) -> bool:
4 """Detect that the given frames share a global scope.
5
6 Two frames share a global scope when neither
7 of them are hidden under a function scope, as well
8 as any parent scope of them, until the root scope.
9 In this case, depending from something defined later on
10 will only work if guarded by a nested function definition.
11
12 Example:
13 class A:
14 # B has the same global scope as `C`, leading to a NameError.
15 # Return True to indicate a shared scope.
16 class B(C): ...
17 class C: ...
18
19 Whereas this does not lead to a NameError:
20 class A:
21 def guard():
22 # Return False to indicate no scope sharing.
23 class B(C): ...
24 class C: ...
25 """
26 def_scope = scope = None
27 if frame and frame.parent:
28 scope = frame.parent.scope()
29 if defframe and defframe.parent:
30 def_scope = defframe.parent.scope()
31 if (
32 isinstance(frame, nodes.ClassDef)
33 and scope is not def_scope
34 and scope is utils.get_node_first_ancestor_of_type(node, nodes.FunctionDef)
35 ):
36 # If the current node's scope is a class nested under a function,
37 # and the def_scope is something else, then they aren't shared.
38 return False
39 if isinstance(frame, nodes.FunctionDef):
40 # If the parent of the current node is a
41 # function, then it can be under its scope (defined in); or
42 # the `->` part of annotations. The same goes
43 # for annotations of function arguments, they'll have
44 # their parent the Arguments node.
45 if frame.parent_of(defframe):
46 return node.lineno < defframe.lineno # type: ignore[no-any-return]
47 if not isinstance(node.parent, (nodes.FunctionDef, nodes.Arguments)):
48 return False
49 elif any(
50 not isinstance(f, (nodes.ClassDef, nodes.Module)) for f in (frame, defframe)
51 ):
52 # Not interested in other frames, since they are already
53 # not in a global scope.
54 return False
55
56 break_scopes = []
57 for current_scope in (scope, def_scope):
58 # Look for parent scopes. If there is anything different
59 # than a module or a class scope, then they frames don't
60 # share a global scope.
61 parent_scope = current_scope
62 while parent_scope:
63 if not isinstance(parent_scope, (nodes.ClassDef, nodes.Module)):
64 break_scopes.append(parent_scope)
65 break
66 if parent_scope.parent:
67 parent_scope = parent_scope.parent.scope()
68 else:
69 break
70 if break_scopes and len(set(break_scopes)) != 1:
71 # Store different scopes than expected.
72 # If the stored scopes are, in fact, the very same, then it means
73 # that the two frames (frame and defframe) share the same scope,
74 # and we could apply our lineno analysis over them.
75 # For instance, this works when they are inside a function, the node
76 # that uses a definition and the definition itself.
77 return False
78 # At this point, we are certain that frame and defframe share a scope
79 # and the definition of the first depends on the second.
80 return frame.lineno < defframe.lineno # type: ignore[no-any-return]
Path 14: 8 calls (0.0)
Name (8)
FunctionDef (8)
FunctionDef (4) ClassDef (4)
False (8)
1def _detect_global_scope(
2 node: nodes.Name, frame: nodes.LocalsDictNodeNG, defframe: nodes.LocalsDictNodeNG
3) -> bool:
4 """Detect that the given frames share a global scope.
5
6 Two frames share a global scope when neither
7 of them are hidden under a function scope, as well
8 as any parent scope of them, until the root scope.
9 In this case, depending from something defined later on
10 will only work if guarded by a nested function definition.
11
12 Example:
13 class A:
14 # B has the same global scope as `C`, leading to a NameError.
15 # Return True to indicate a shared scope.
16 class B(C): ...
17 class C: ...
18
19 Whereas this does not lead to a NameError:
20 class A:
21 def guard():
22 # Return False to indicate no scope sharing.
23 class B(C): ...
24 class C: ...
25 """
26 def_scope = scope = None
27 if frame and frame.parent:
28 scope = frame.parent.scope()
29 if defframe and defframe.parent:
30 def_scope = defframe.parent.scope()
31 if (
32 isinstance(frame, nodes.ClassDef)
33 and scope is not def_scope
34 and scope is utils.get_node_first_ancestor_of_type(node, nodes.FunctionDef)
35 ):
36 # If the current node's scope is a class nested under a function,
37 # and the def_scope is something else, then they aren't shared.
38 return False
39 if isinstance(frame, nodes.FunctionDef):
40 # If the parent of the current node is a
41 # function, then it can be under its scope (defined in); or
42 # the `->` part of annotations. The same goes
43 # for annotations of function arguments, they'll have
44 # their parent the Arguments node.
45 if frame.parent_of(defframe):
46 return node.lineno < defframe.lineno # type: ignore[no-any-return]
47 if not isinstance(node.parent, (nodes.FunctionDef, nodes.Arguments)):
48 return False
49 elif any(
50 not isinstance(f, (nodes.ClassDef, nodes.Module)) for f in (frame, defframe)
51 ):
52 # Not interested in other frames, since they are already
53 # not in a global scope.
54 return False
55
56 break_scopes = []
57 for current_scope in (scope, def_scope):
58 # Look for parent scopes. If there is anything different
59 # than a module or a class scope, then they frames don't
60 # share a global scope.
61 parent_scope = current_scope
62 while parent_scope:
63 if not isinstance(parent_scope, (nodes.ClassDef, nodes.Module)):
64 break_scopes.append(parent_scope)
65 break
66 if parent_scope.parent:
67 parent_scope = parent_scope.parent.scope()
68 else:
69 break
70 if break_scopes and len(set(break_scopes)) != 1:
71 # Store different scopes than expected.
72 # If the stored scopes are, in fact, the very same, then it means
73 # that the two frames (frame and defframe) share the same scope,
74 # and we could apply our lineno analysis over them.
75 # For instance, this works when they are inside a function, the node
76 # that uses a definition and the definition itself.
77 return False
78 # At this point, we are certain that frame and defframe share a scope
79 # and the definition of the first depends on the second.
80 return frame.lineno < defframe.lineno # type: ignore[no-any-return]
Path 15: 7 calls (0.0)
Name (7)
ClassDef (7)
ClassDef (7)
False (6) True (1)
1def _detect_global_scope(
2 node: nodes.Name, frame: nodes.LocalsDictNodeNG, defframe: nodes.LocalsDictNodeNG
3) -> bool:
4 """Detect that the given frames share a global scope.
5
6 Two frames share a global scope when neither
7 of them are hidden under a function scope, as well
8 as any parent scope of them, until the root scope.
9 In this case, depending from something defined later on
10 will only work if guarded by a nested function definition.
11
12 Example:
13 class A:
14 # B has the same global scope as `C`, leading to a NameError.
15 # Return True to indicate a shared scope.
16 class B(C): ...
17 class C: ...
18
19 Whereas this does not lead to a NameError:
20 class A:
21 def guard():
22 # Return False to indicate no scope sharing.
23 class B(C): ...
24 class C: ...
25 """
26 def_scope = scope = None
27 if frame and frame.parent:
28 scope = frame.parent.scope()
29 if defframe and defframe.parent:
30 def_scope = defframe.parent.scope()
31 if (
32 isinstance(frame, nodes.ClassDef)
33 and scope is not def_scope
34 and scope is utils.get_node_first_ancestor_of_type(node, nodes.FunctionDef)
35 ):
36 # If the current node's scope is a class nested under a function,
37 # and the def_scope is something else, then they aren't shared.
38 return False
39 if isinstance(frame, nodes.FunctionDef):
40 # If the parent of the current node is a
41 # function, then it can be under its scope (defined in); or
42 # the `->` part of annotations. The same goes
43 # for annotations of function arguments, they'll have
44 # their parent the Arguments node.
45 if frame.parent_of(defframe):
46 return node.lineno < defframe.lineno # type: ignore[no-any-return]
47 if not isinstance(node.parent, (nodes.FunctionDef, nodes.Arguments)):
48 return False
49 elif any(
50 not isinstance(f, (nodes.ClassDef, nodes.Module)) for f in (frame, defframe)
51 ):
52 # Not interested in other frames, since they are already
53 # not in a global scope.
54 return False
55
56 break_scopes = []
57 for current_scope in (scope, def_scope):
58 # Look for parent scopes. If there is anything different
59 # than a module or a class scope, then they frames don't
60 # share a global scope.
61 parent_scope = current_scope
62 while parent_scope:
63 if not isinstance(parent_scope, (nodes.ClassDef, nodes.Module)):
64 break_scopes.append(parent_scope)
65 break
66 if parent_scope.parent:
67 parent_scope = parent_scope.parent.scope()
68 else:
69 break
70 if break_scopes and len(set(break_scopes)) != 1:
71 # Store different scopes than expected.
72 # If the stored scopes are, in fact, the very same, then it means
73 # that the two frames (frame and defframe) share the same scope,
74 # and we could apply our lineno analysis over them.
75 # For instance, this works when they are inside a function, the node
76 # that uses a definition and the definition itself.
77 return False
78 # At this point, we are certain that frame and defframe share a scope
79 # and the definition of the first depends on the second.
80 return frame.lineno < defframe.lineno # type: ignore[no-any-return]
Path 16: 7 calls (0.0)
Name (7)
FunctionDef (7)
Module (7)
False (7)
1def _detect_global_scope(
2 node: nodes.Name, frame: nodes.LocalsDictNodeNG, defframe: nodes.LocalsDictNodeNG
3) -> bool:
4 """Detect that the given frames share a global scope.
5
6 Two frames share a global scope when neither
7 of them are hidden under a function scope, as well
8 as any parent scope of them, until the root scope.
9 In this case, depending from something defined later on
10 will only work if guarded by a nested function definition.
11
12 Example:
13 class A:
14 # B has the same global scope as `C`, leading to a NameError.
15 # Return True to indicate a shared scope.
16 class B(C): ...
17 class C: ...
18
19 Whereas this does not lead to a NameError:
20 class A:
21 def guard():
22 # Return False to indicate no scope sharing.
23 class B(C): ...
24 class C: ...
25 """
26 def_scope = scope = None
27 if frame and frame.parent:
28 scope = frame.parent.scope()
29 if defframe and defframe.parent:
30 def_scope = defframe.parent.scope()
31 if (
32 isinstance(frame, nodes.ClassDef)
33 and scope is not def_scope
34 and scope is utils.get_node_first_ancestor_of_type(node, nodes.FunctionDef)
35 ):
36 # If the current node's scope is a class nested under a function,
37 # and the def_scope is something else, then they aren't shared.
38 return False
39 if isinstance(frame, nodes.FunctionDef):
40 # If the parent of the current node is a
41 # function, then it can be under its scope (defined in); or
42 # the `->` part of annotations. The same goes
43 # for annotations of function arguments, they'll have
44 # their parent the Arguments node.
45 if frame.parent_of(defframe):
46 return node.lineno < defframe.lineno # type: ignore[no-any-return]
47 if not isinstance(node.parent, (nodes.FunctionDef, nodes.Arguments)):
48 return False
49 elif any(
50 not isinstance(f, (nodes.ClassDef, nodes.Module)) for f in (frame, defframe)
51 ):
52 # Not interested in other frames, since they are already
53 # not in a global scope.
54 return False
55
56 break_scopes = []
57 for current_scope in (scope, def_scope):
58 # Look for parent scopes. If there is anything different
59 # than a module or a class scope, then they frames don't
60 # share a global scope.
61 parent_scope = current_scope
62 while parent_scope:
63 if not isinstance(parent_scope, (nodes.ClassDef, nodes.Module)):
64 break_scopes.append(parent_scope)
65 break
66 if parent_scope.parent:
67 parent_scope = parent_scope.parent.scope()
68 else:
69 break
70 if break_scopes and len(set(break_scopes)) != 1:
71 # Store different scopes than expected.
72 # If the stored scopes are, in fact, the very same, then it means
73 # that the two frames (frame and defframe) share the same scope,
74 # and we could apply our lineno analysis over them.
75 # For instance, this works when they are inside a function, the node
76 # that uses a definition and the definition itself.
77 return False
78 # At this point, we are certain that frame and defframe share a scope
79 # and the definition of the first depends on the second.
80 return frame.lineno < defframe.lineno # type: ignore[no-any-return]
Path 17: 4 calls (0.0)
Name (4)
ClassDef (4)
Module (4)
False (4)
1def _detect_global_scope(
2 node: nodes.Name, frame: nodes.LocalsDictNodeNG, defframe: nodes.LocalsDictNodeNG
3) -> bool:
4 """Detect that the given frames share a global scope.
5
6 Two frames share a global scope when neither
7 of them are hidden under a function scope, as well
8 as any parent scope of them, until the root scope.
9 In this case, depending from something defined later on
10 will only work if guarded by a nested function definition.
11
12 Example:
13 class A:
14 # B has the same global scope as `C`, leading to a NameError.
15 # Return True to indicate a shared scope.
16 class B(C): ...
17 class C: ...
18
19 Whereas this does not lead to a NameError:
20 class A:
21 def guard():
22 # Return False to indicate no scope sharing.
23 class B(C): ...
24 class C: ...
25 """
26 def_scope = scope = None
27 if frame and frame.parent:
28 scope = frame.parent.scope()
29 if defframe and defframe.parent:
30 def_scope = defframe.parent.scope()
31 if (
32 isinstance(frame, nodes.ClassDef)
33 and scope is not def_scope
34 and scope is utils.get_node_first_ancestor_of_type(node, nodes.FunctionDef)
35 ):
36 # If the current node's scope is a class nested under a function,
37 # and the def_scope is something else, then they aren't shared.
38 return False
39 if isinstance(frame, nodes.FunctionDef):
40 # If the parent of the current node is a
41 # function, then it can be under its scope (defined in); or
42 # the `->` part of annotations. The same goes
43 # for annotations of function arguments, they'll have
44 # their parent the Arguments node.
45 if frame.parent_of(defframe):
46 return node.lineno < defframe.lineno # type: ignore[no-any-return]
47 if not isinstance(node.parent, (nodes.FunctionDef, nodes.Arguments)):
48 return False
49 elif any(
50 not isinstance(f, (nodes.ClassDef, nodes.Module)) for f in (frame, defframe)
51 ):
52 # Not interested in other frames, since they are already
53 # not in a global scope.
54 return False
55
56 break_scopes = []
57 for current_scope in (scope, def_scope):
58 # Look for parent scopes. If there is anything different
59 # than a module or a class scope, then they frames don't
60 # share a global scope.
61 parent_scope = current_scope
62 while parent_scope:
63 if not isinstance(parent_scope, (nodes.ClassDef, nodes.Module)):
64 break_scopes.append(parent_scope)
65 break
66 if parent_scope.parent:
67 parent_scope = parent_scope.parent.scope()
68 else:
69 break
70 if break_scopes and len(set(break_scopes)) != 1:
71 # Store different scopes than expected.
72 # If the stored scopes are, in fact, the very same, then it means
73 # that the two frames (frame and defframe) share the same scope,
74 # and we could apply our lineno analysis over them.
75 # For instance, this works when they are inside a function, the node
76 # that uses a definition and the definition itself.
77 return False
78 # At this point, we are certain that frame and defframe share a scope
79 # and the definition of the first depends on the second.
80 return frame.lineno < defframe.lineno # type: ignore[no-any-return]
Path 18: 4 calls (0.0)
Name (4)
ClassDef (4)
FunctionDef (2) ClassDef (2)
False (4)
1def _detect_global_scope(
2 node: nodes.Name, frame: nodes.LocalsDictNodeNG, defframe: nodes.LocalsDictNodeNG
3) -> bool:
4 """Detect that the given frames share a global scope.
5
6 Two frames share a global scope when neither
7 of them are hidden under a function scope, as well
8 as any parent scope of them, until the root scope.
9 In this case, depending from something defined later on
10 will only work if guarded by a nested function definition.
11
12 Example:
13 class A:
14 # B has the same global scope as `C`, leading to a NameError.
15 # Return True to indicate a shared scope.
16 class B(C): ...
17 class C: ...
18
19 Whereas this does not lead to a NameError:
20 class A:
21 def guard():
22 # Return False to indicate no scope sharing.
23 class B(C): ...
24 class C: ...
25 """
26 def_scope = scope = None
27 if frame and frame.parent:
28 scope = frame.parent.scope()
29 if defframe and defframe.parent:
30 def_scope = defframe.parent.scope()
31 if (
32 isinstance(frame, nodes.ClassDef)
33 and scope is not def_scope
34 and scope is utils.get_node_first_ancestor_of_type(node, nodes.FunctionDef)
35 ):
36 # If the current node's scope is a class nested under a function,
37 # and the def_scope is something else, then they aren't shared.
38 return False
39 if isinstance(frame, nodes.FunctionDef):
40 # If the parent of the current node is a
41 # function, then it can be under its scope (defined in); or
42 # the `->` part of annotations. The same goes
43 # for annotations of function arguments, they'll have
44 # their parent the Arguments node.
45 if frame.parent_of(defframe):
46 return node.lineno < defframe.lineno # type: ignore[no-any-return]
47 if not isinstance(node.parent, (nodes.FunctionDef, nodes.Arguments)):
48 return False
49 elif any(
50 not isinstance(f, (nodes.ClassDef, nodes.Module)) for f in (frame, defframe)
51 ):
52 # Not interested in other frames, since they are already
53 # not in a global scope.
54 return False
55
56 break_scopes = []
57 for current_scope in (scope, def_scope):
58 # Look for parent scopes. If there is anything different
59 # than a module or a class scope, then they frames don't
60 # share a global scope.
61 parent_scope = current_scope
62 while parent_scope:
63 if not isinstance(parent_scope, (nodes.ClassDef, nodes.Module)):
64 break_scopes.append(parent_scope)
65 break
66 if parent_scope.parent:
67 parent_scope = parent_scope.parent.scope()
68 else:
69 break
70 if break_scopes and len(set(break_scopes)) != 1:
71 # Store different scopes than expected.
72 # If the stored scopes are, in fact, the very same, then it means
73 # that the two frames (frame and defframe) share the same scope,
74 # and we could apply our lineno analysis over them.
75 # For instance, this works when they are inside a function, the node
76 # that uses a definition and the definition itself.
77 return False
78 # At this point, we are certain that frame and defframe share a scope
79 # and the definition of the first depends on the second.
80 return frame.lineno < defframe.lineno # type: ignore[no-any-return]
Path 19: 3 calls (0.0)
Name (3)
ClassDef (3)
ClassDef (3)
True (2) False (1)
1def _detect_global_scope(
2 node: nodes.Name, frame: nodes.LocalsDictNodeNG, defframe: nodes.LocalsDictNodeNG
3) -> bool:
4 """Detect that the given frames share a global scope.
5
6 Two frames share a global scope when neither
7 of them are hidden under a function scope, as well
8 as any parent scope of them, until the root scope.
9 In this case, depending from something defined later on
10 will only work if guarded by a nested function definition.
11
12 Example:
13 class A:
14 # B has the same global scope as `C`, leading to a NameError.
15 # Return True to indicate a shared scope.
16 class B(C): ...
17 class C: ...
18
19 Whereas this does not lead to a NameError:
20 class A:
21 def guard():
22 # Return False to indicate no scope sharing.
23 class B(C): ...
24 class C: ...
25 """
26 def_scope = scope = None
27 if frame and frame.parent:
28 scope = frame.parent.scope()
29 if defframe and defframe.parent:
30 def_scope = defframe.parent.scope()
31 if (
32 isinstance(frame, nodes.ClassDef)
33 and scope is not def_scope
34 and scope is utils.get_node_first_ancestor_of_type(node, nodes.FunctionDef)
35 ):
36 # If the current node's scope is a class nested under a function,
37 # and the def_scope is something else, then they aren't shared.
38 return False
39 if isinstance(frame, nodes.FunctionDef):
40 # If the parent of the current node is a
41 # function, then it can be under its scope (defined in); or
42 # the `->` part of annotations. The same goes
43 # for annotations of function arguments, they'll have
44 # their parent the Arguments node.
45 if frame.parent_of(defframe):
46 return node.lineno < defframe.lineno # type: ignore[no-any-return]
47 if not isinstance(node.parent, (nodes.FunctionDef, nodes.Arguments)):
48 return False
49 elif any(
50 not isinstance(f, (nodes.ClassDef, nodes.Module)) for f in (frame, defframe)
51 ):
52 # Not interested in other frames, since they are already
53 # not in a global scope.
54 return False
55
56 break_scopes = []
57 for current_scope in (scope, def_scope):
58 # Look for parent scopes. If there is anything different
59 # than a module or a class scope, then they frames don't
60 # share a global scope.
61 parent_scope = current_scope
62 while parent_scope:
63 if not isinstance(parent_scope, (nodes.ClassDef, nodes.Module)):
64 break_scopes.append(parent_scope)
65 break
66 if parent_scope.parent:
67 parent_scope = parent_scope.parent.scope()
68 else:
69 break
70 if break_scopes and len(set(break_scopes)) != 1:
71 # Store different scopes than expected.
72 # If the stored scopes are, in fact, the very same, then it means
73 # that the two frames (frame and defframe) share the same scope,
74 # and we could apply our lineno analysis over them.
75 # For instance, this works when they are inside a function, the node
76 # that uses a definition and the definition itself.
77 return False
78 # At this point, we are certain that frame and defframe share a scope
79 # and the definition of the first depends on the second.
80 return frame.lineno < defframe.lineno # type: ignore[no-any-return]
Path 20: 2 calls (0.0)
Name (2)
ClassDef (2)
ClassDef (2)
False (2)
1def _detect_global_scope(
2 node: nodes.Name, frame: nodes.LocalsDictNodeNG, defframe: nodes.LocalsDictNodeNG
3) -> bool:
4 """Detect that the given frames share a global scope.
5
6 Two frames share a global scope when neither
7 of them are hidden under a function scope, as well
8 as any parent scope of them, until the root scope.
9 In this case, depending from something defined later on
10 will only work if guarded by a nested function definition.
11
12 Example:
13 class A:
14 # B has the same global scope as `C`, leading to a NameError.
15 # Return True to indicate a shared scope.
16 class B(C): ...
17 class C: ...
18
19 Whereas this does not lead to a NameError:
20 class A:
21 def guard():
22 # Return False to indicate no scope sharing.
23 class B(C): ...
24 class C: ...
25 """
26 def_scope = scope = None
27 if frame and frame.parent:
28 scope = frame.parent.scope()
29 if defframe and defframe.parent:
30 def_scope = defframe.parent.scope()
31 if (
32 isinstance(frame, nodes.ClassDef)
33 and scope is not def_scope
34 and scope is utils.get_node_first_ancestor_of_type(node, nodes.FunctionDef)
35 ):
36 # If the current node's scope is a class nested under a function,
37 # and the def_scope is something else, then they aren't shared.
38 return False
39 if isinstance(frame, nodes.FunctionDef):
40 # If the parent of the current node is a
41 # function, then it can be under its scope (defined in); or
42 # the `->` part of annotations. The same goes
43 # for annotations of function arguments, they'll have
44 # their parent the Arguments node.
45 if frame.parent_of(defframe):
46 return node.lineno < defframe.lineno # type: ignore[no-any-return]
47 if not isinstance(node.parent, (nodes.FunctionDef, nodes.Arguments)):
48 return False
49 elif any(
50 not isinstance(f, (nodes.ClassDef, nodes.Module)) for f in (frame, defframe)
51 ):
52 # Not interested in other frames, since they are already
53 # not in a global scope.
54 return False
55
56 break_scopes = []
57 for current_scope in (scope, def_scope):
58 # Look for parent scopes. If there is anything different
59 # than a module or a class scope, then they frames don't
60 # share a global scope.
61 parent_scope = current_scope
62 while parent_scope:
63 if not isinstance(parent_scope, (nodes.ClassDef, nodes.Module)):
64 break_scopes.append(parent_scope)
65 break
66 if parent_scope.parent:
67 parent_scope = parent_scope.parent.scope()
68 else:
69 break
70 if break_scopes and len(set(break_scopes)) != 1:
71 # Store different scopes than expected.
72 # If the stored scopes are, in fact, the very same, then it means
73 # that the two frames (frame and defframe) share the same scope,
74 # and we could apply our lineno analysis over them.
75 # For instance, this works when they are inside a function, the node
76 # that uses a definition and the definition itself.
77 return False
78 # At this point, we are certain that frame and defframe share a scope
79 # and the definition of the first depends on the second.
80 return frame.lineno < defframe.lineno # type: ignore[no-any-return]
Path 21: 2 calls (0.0)
Name (2)
FunctionDef (2)
FunctionDef (2)
False (2)
1def _detect_global_scope(
2 node: nodes.Name, frame: nodes.LocalsDictNodeNG, defframe: nodes.LocalsDictNodeNG
3) -> bool:
4 """Detect that the given frames share a global scope.
5
6 Two frames share a global scope when neither
7 of them are hidden under a function scope, as well
8 as any parent scope of them, until the root scope.
9 In this case, depending from something defined later on
10 will only work if guarded by a nested function definition.
11
12 Example:
13 class A:
14 # B has the same global scope as `C`, leading to a NameError.
15 # Return True to indicate a shared scope.
16 class B(C): ...
17 class C: ...
18
19 Whereas this does not lead to a NameError:
20 class A:
21 def guard():
22 # Return False to indicate no scope sharing.
23 class B(C): ...
24 class C: ...
25 """
26 def_scope = scope = None
27 if frame and frame.parent:
28 scope = frame.parent.scope()
29 if defframe and defframe.parent:
30 def_scope = defframe.parent.scope()
31 if (
32 isinstance(frame, nodes.ClassDef)
33 and scope is not def_scope
34 and scope is utils.get_node_first_ancestor_of_type(node, nodes.FunctionDef)
35 ):
36 # If the current node's scope is a class nested under a function,
37 # and the def_scope is something else, then they aren't shared.
38 return False
39 if isinstance(frame, nodes.FunctionDef):
40 # If the parent of the current node is a
41 # function, then it can be under its scope (defined in); or
42 # the `->` part of annotations. The same goes
43 # for annotations of function arguments, they'll have
44 # their parent the Arguments node.
45 if frame.parent_of(defframe):
46 return node.lineno < defframe.lineno # type: ignore[no-any-return]
47 if not isinstance(node.parent, (nodes.FunctionDef, nodes.Arguments)):
48 return False
49 elif any(
50 not isinstance(f, (nodes.ClassDef, nodes.Module)) for f in (frame, defframe)
51 ):
52 # Not interested in other frames, since they are already
53 # not in a global scope.
54 return False
55
56 break_scopes = []
57 for current_scope in (scope, def_scope):
58 # Look for parent scopes. If there is anything different
59 # than a module or a class scope, then they frames don't
60 # share a global scope.
61 parent_scope = current_scope
62 while parent_scope:
63 if not isinstance(parent_scope, (nodes.ClassDef, nodes.Module)):
64 break_scopes.append(parent_scope)
65 break
66 if parent_scope.parent:
67 parent_scope = parent_scope.parent.scope()
68 else:
69 break
70 if break_scopes and len(set(break_scopes)) != 1:
71 # Store different scopes than expected.
72 # If the stored scopes are, in fact, the very same, then it means
73 # that the two frames (frame and defframe) share the same scope,
74 # and we could apply our lineno analysis over them.
75 # For instance, this works when they are inside a function, the node
76 # that uses a definition and the definition itself.
77 return False
78 # At this point, we are certain that frame and defframe share a scope
79 # and the definition of the first depends on the second.
80 return frame.lineno < defframe.lineno # type: ignore[no-any-return]
Path 22: 1 calls (0.0)
Name (1)
FunctionDef (1)
FunctionDef (1)
False (1)
1def _detect_global_scope(
2 node: nodes.Name, frame: nodes.LocalsDictNodeNG, defframe: nodes.LocalsDictNodeNG
3) -> bool:
4 """Detect that the given frames share a global scope.
5
6 Two frames share a global scope when neither
7 of them are hidden under a function scope, as well
8 as any parent scope of them, until the root scope.
9 In this case, depending from something defined later on
10 will only work if guarded by a nested function definition.
11
12 Example:
13 class A:
14 # B has the same global scope as `C`, leading to a NameError.
15 # Return True to indicate a shared scope.
16 class B(C): ...
17 class C: ...
18
19 Whereas this does not lead to a NameError:
20 class A:
21 def guard():
22 # Return False to indicate no scope sharing.
23 class B(C): ...
24 class C: ...
25 """
26 def_scope = scope = None
27 if frame and frame.parent:
28 scope = frame.parent.scope()
29 if defframe and defframe.parent:
30 def_scope = defframe.parent.scope()
31 if (
32 isinstance(frame, nodes.ClassDef)
33 and scope is not def_scope
34 and scope is utils.get_node_first_ancestor_of_type(node, nodes.FunctionDef)
35 ):
36 # If the current node's scope is a class nested under a function,
37 # and the def_scope is something else, then they aren't shared.
38 return False
39 if isinstance(frame, nodes.FunctionDef):
40 # If the parent of the current node is a
41 # function, then it can be under its scope (defined in); or
42 # the `->` part of annotations. The same goes
43 # for annotations of function arguments, they'll have
44 # their parent the Arguments node.
45 if frame.parent_of(defframe):
46 return node.lineno < defframe.lineno # type: ignore[no-any-return]
47 if not isinstance(node.parent, (nodes.FunctionDef, nodes.Arguments)):
48 return False
49 elif any(
50 not isinstance(f, (nodes.ClassDef, nodes.Module)) for f in (frame, defframe)
51 ):
52 # Not interested in other frames, since they are already
53 # not in a global scope.
54 return False
55
56 break_scopes = []
57 for current_scope in (scope, def_scope):
58 # Look for parent scopes. If there is anything different
59 # than a module or a class scope, then they frames don't
60 # share a global scope.
61 parent_scope = current_scope
62 while parent_scope:
63 if not isinstance(parent_scope, (nodes.ClassDef, nodes.Module)):
64 break_scopes.append(parent_scope)
65 break
66 if parent_scope.parent:
67 parent_scope = parent_scope.parent.scope()
68 else:
69 break
70 if break_scopes and len(set(break_scopes)) != 1:
71 # Store different scopes than expected.
72 # If the stored scopes are, in fact, the very same, then it means
73 # that the two frames (frame and defframe) share the same scope,
74 # and we could apply our lineno analysis over them.
75 # For instance, this works when they are inside a function, the node
76 # that uses a definition and the definition itself.
77 return False
78 # At this point, we are certain that frame and defframe share a scope
79 # and the definition of the first depends on the second.
80 return frame.lineno < defframe.lineno # type: ignore[no-any-return]
Path 23: 1 calls (0.0)
Name (1)
FunctionDef (1)
ClassDef (1)
False (1)
1def _detect_global_scope(
2 node: nodes.Name, frame: nodes.LocalsDictNodeNG, defframe: nodes.LocalsDictNodeNG
3) -> bool:
4 """Detect that the given frames share a global scope.
5
6 Two frames share a global scope when neither
7 of them are hidden under a function scope, as well
8 as any parent scope of them, until the root scope.
9 In this case, depending from something defined later on
10 will only work if guarded by a nested function definition.
11
12 Example:
13 class A:
14 # B has the same global scope as `C`, leading to a NameError.
15 # Return True to indicate a shared scope.
16 class B(C): ...
17 class C: ...
18
19 Whereas this does not lead to a NameError:
20 class A:
21 def guard():
22 # Return False to indicate no scope sharing.
23 class B(C): ...
24 class C: ...
25 """
26 def_scope = scope = None
27 if frame and frame.parent:
28 scope = frame.parent.scope()
29 if defframe and defframe.parent:
30 def_scope = defframe.parent.scope()
31 if (
32 isinstance(frame, nodes.ClassDef)
33 and scope is not def_scope
34 and scope is utils.get_node_first_ancestor_of_type(node, nodes.FunctionDef)
35 ):
36 # If the current node's scope is a class nested under a function,
37 # and the def_scope is something else, then they aren't shared.
38 return False
39 if isinstance(frame, nodes.FunctionDef):
40 # If the parent of the current node is a
41 # function, then it can be under its scope (defined in); or
42 # the `->` part of annotations. The same goes
43 # for annotations of function arguments, they'll have
44 # their parent the Arguments node.
45 if frame.parent_of(defframe):
46 return node.lineno < defframe.lineno # type: ignore[no-any-return]
47 if not isinstance(node.parent, (nodes.FunctionDef, nodes.Arguments)):
48 return False
49 elif any(
50 not isinstance(f, (nodes.ClassDef, nodes.Module)) for f in (frame, defframe)
51 ):
52 # Not interested in other frames, since they are already
53 # not in a global scope.
54 return False
55
56 break_scopes = []
57 for current_scope in (scope, def_scope):
58 # Look for parent scopes. If there is anything different
59 # than a module or a class scope, then they frames don't
60 # share a global scope.
61 parent_scope = current_scope
62 while parent_scope:
63 if not isinstance(parent_scope, (nodes.ClassDef, nodes.Module)):
64 break_scopes.append(parent_scope)
65 break
66 if parent_scope.parent:
67 parent_scope = parent_scope.parent.scope()
68 else:
69 break
70 if break_scopes and len(set(break_scopes)) != 1:
71 # Store different scopes than expected.
72 # If the stored scopes are, in fact, the very same, then it means
73 # that the two frames (frame and defframe) share the same scope,
74 # and we could apply our lineno analysis over them.
75 # For instance, this works when they are inside a function, the node
76 # that uses a definition and the definition itself.
77 return False
78 # At this point, we are certain that frame and defframe share a scope
79 # and the definition of the first depends on the second.
80 return frame.lineno < defframe.lineno # type: ignore[no-any-return]