@@ -12,7 +12,7 @@ class GlobalVariablesChecker(BaseChecker):
12
12
__implements__ = IAstroidChecker
13
13
14
14
name = 'global_variables'
15
- msgs = {'E9997' : ('Global variables should not be used in CSC108/CSC148 - '
15
+ msgs = {'E9997' : ('Global variables must be constants in CSC108/CSC148: '
16
16
'%s' , 'forbidden-global-variables' , '' )}
17
17
18
18
# this is important so that your checker is executed before others
@@ -26,18 +26,14 @@ def visit_global(self, node):
26
26
args = "the keyword 'global' is used on line {}" .format (node .lineno )
27
27
self .add_message ('forbidden-global-variables' , node = node , args = args )
28
28
29
- def visit_nonlocal (self , node ):
30
- args = "the keyword 'nonlocal' is used on line {}" .format (node .lineno )
31
- self .add_message ('forbidden-global-variables' , node = node , args = args )
32
-
33
29
def visit_assign (self , node ):
34
- """Allow global constant variables (uppercase), but issue messages for
30
+ """Allow global constant variables (uppercase), but issue messages for
35
31
all other globals.
36
32
"""
37
33
self ._inspect_vars (node )
38
34
39
35
def visit_name (self , node ):
40
- """Allow global constant variables (uppercase), but issue messages for
36
+ """Allow global constant variables (uppercase), but issue messages for
41
37
all other globals.
42
38
"""
43
39
self ._inspect_vars (node )
@@ -58,27 +54,29 @@ def _store_name_or_alias(self, node):
58
54
self .import_names .append (name_tuple [0 ]) # no alias
59
55
60
56
def _inspect_vars (self , node ):
61
- """Allows constant, global variables (i.e. uppercase), but issue
57
+ """Allows constant, global variables (i.e. uppercase), but issue
62
58
messages for all other global variables.
63
59
"""
64
60
if hasattr (node , 'name' ) and node .name in self .import_names :
65
61
return
66
- if ( isinstance (node .frame (), astroid .scoped_nodes .Module ) and not is_in_main (node ) ):
62
+ if isinstance (node .frame (), astroid .scoped_nodes .Module ) and not is_in_main (node ):
67
63
node_list = _get_child_disallowed_global_var_nodes (node )
68
64
for node in node_list :
69
- args = "a global variable '{}' is declared on line {}" \
65
+ args = "a global variable '{}' is assigned to on line {}" \
70
66
.format (node .name , node .lineno )
71
67
self .add_message ('forbidden-global-variables' , node = node , args = args )
72
68
73
69
74
70
def _get_child_disallowed_global_var_nodes (node ):
75
- """Return a list of all top-level Name or AssignName nodes for a given
76
- global, non-constant variable.
71
+ """Return a list of all top-level Name or AssignName nodes for a given
72
+ global, non-constant variable.
77
73
"""
78
74
node_list = []
79
- if ((isinstance (node , astroid .AssignName ) or (isinstance (node , astroid .Name ) and not isinstance (node .parent , astroid .Call )))
80
- and not re .match (CONST_NAME_RGX , node .name )):
75
+ if ((isinstance (node , (astroid .AssignName , astroid .Name )) and not isinstance (node .parent , astroid .Call )) and
76
+ not re .match (CONST_NAME_RGX , node .name ) and
77
+ node .scope () is node .root ()):
81
78
return [node ]
79
+
82
80
for child_node in node .get_children ():
83
81
node_list += _get_child_disallowed_global_var_nodes (child_node )
84
82
return node_list
0 commit comments