diff options
author | Claudiu Popa <devnull@localhost> | 2014-10-02 15:24:30 +0300 |
---|---|---|
committer | Claudiu Popa <devnull@localhost> | 2014-10-02 15:24:30 +0300 |
commit | 11f7f4f337a97f102173f43d2f891c2910492a48 (patch) | |
tree | 54734f06938f90d30cf58a6650869cbbf9e5907b /checkers/variables.py | |
parent | d0349fddd66dcd0f334cdfdbd50e6eecab7766ad (diff) | |
download | pylint-11f7f4f337a97f102173f43d2f891c2910492a48.tar.gz |
Simplify the import lookup for frames in which a global appears.
Diffstat (limited to 'checkers/variables.py')
-rw-r--r-- | checkers/variables.py | 37 |
1 files changed, 18 insertions, 19 deletions
diff --git a/checkers/variables.py b/checkers/variables.py index d1b04e1..b5ea34f 100644 --- a/checkers/variables.py +++ b/checkers/variables.py @@ -564,6 +564,23 @@ builtins. Remember that you should avoid to define new builtins when possible.' continue self.add_message('unused-variable', args=name, node=stmt) + def _find_frame_imports(self, name, frame): + """ + Detect imports in the frame, with the required + *name*. Such imports can be considered assignments. + Returns True if an import for the given name was found. + """ + imports = frame.nodes_of_class((astroid.Import, astroid.From)) + for import_node in imports: + for import_name, import_alias in import_node.names: + # If the import uses an alias, check only that. + # Otherwise, check only the import name. + if import_alias: + if import_alias == name: + return True + elif import_name and import_name == name: + return True + @check_messages('global-variable-undefined', 'global-variable-not-assigned', 'global-statement', 'global-at-module-level', 'redefined-builtin') def visit_global(self, node): @@ -589,25 +606,7 @@ builtins. Remember that you should avoid to define new builtins when possible.' # same scope level assignment break else: - # global but no assignment - # Detect imports in the current frame, with the required - # name. Such imports can be considered assignments. - imports = frame.nodes_of_class((astroid.Import, astroid.From)) - for import_node in imports: - found = False - for import_name, import_alias in import_node.names: - # If the import uses an alias, check only that. - # Otherwise, check only the import name. - if import_alias: - if import_alias == name: - found = True - break - elif import_name and import_name == name: - found = True - break - if found: - break - else: + if not self._find_frame_imports(name, frame): self.add_message('global-variable-not-assigned', args=name, node=node) default_message = False |