diff options
| author | Claudiu Popa <pcmanticore@gmail.com> | 2014-10-02 15:24:30 +0300 |
|---|---|---|
| committer | Claudiu Popa <pcmanticore@gmail.com> | 2014-10-02 15:24:30 +0300 |
| commit | 851f6c007abbd8ff880ebb856fc28a40640b2a87 (patch) | |
| tree | 8455d0445f42008e21f5332843bc3bda32abfc44 /checkers/variables.py | |
| parent | 2a585d5de2dd01dcd7c3c311443ccccac78a60d2 (diff) | |
| download | pylint-git-851f6c007abbd8ff880ebb856fc28a40640b2a87.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 d1b04e116..b5ea34f48 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 |
