Skip to content

Commit ccf54cf

Browse files
committed
Update GlobalVariablesChecker.
Stops flagging variables in top-level comprehension expressions. Changes wording of message (fixes pyta-uoft#223). Supercedes pyta-uoft#307.
1 parent 703c22a commit ccf54cf

File tree

2 files changed

+19
-14
lines changed

2 files changed

+19
-14
lines changed

examples/global_variables_example.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@ def function() -> None:
1313
print(ex)
1414

1515

16+
# The local variable in a comprehension is okay
17+
print([x for x in [1, 2, 3]])
18+
print({x + 1 for x in [1, 2, 3]})
19+
print({x: x * 3 for x in [1, 2, 3]})
20+
print(list(x + 1 for x in [1, 2, 3]))
21+
22+
1623
def function1() -> None:
1724
"""A test for the global variables checker."""
1825

python_ta/checkers/global_variables_checker.py

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class GlobalVariablesChecker(BaseChecker):
1212
__implements__ = IAstroidChecker
1313

1414
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: '
1616
'%s', 'forbidden-global-variables', '')}
1717

1818
# this is important so that your checker is executed before others
@@ -26,18 +26,14 @@ def visit_global(self, node):
2626
args = "the keyword 'global' is used on line {}".format(node.lineno)
2727
self.add_message('forbidden-global-variables', node=node, args=args)
2828

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-
3329
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
3531
all other globals.
3632
"""
3733
self._inspect_vars(node)
3834

3935
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
4137
all other globals.
4238
"""
4339
self._inspect_vars(node)
@@ -58,27 +54,29 @@ def _store_name_or_alias(self, node):
5854
self.import_names.append(name_tuple[0]) # no alias
5955

6056
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
6258
messages for all other global variables.
6359
"""
6460
if hasattr(node, 'name') and node.name in self.import_names:
6561
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):
6763
node_list = _get_child_disallowed_global_var_nodes(node)
6864
for node in node_list:
69-
args = "a global variable '{}' is declared on line {}"\
65+
args = "a global variable '{}' is assigned to on line {}"\
7066
.format(node.name, node.lineno)
7167
self.add_message('forbidden-global-variables', node=node, args=args)
7268

7369

7470
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.
7773
"""
7874
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()):
8178
return [node]
79+
8280
for child_node in node.get_children():
8381
node_list += _get_child_disallowed_global_var_nodes(child_node)
8482
return node_list

0 commit comments

Comments
 (0)