diff options
author | Ćukasz Rogalski <rogalski.91@gmail.com> | 2017-02-09 21:57:29 +0100 |
---|---|---|
committer | Claudiu Popa <pcmanticore@gmail.com> | 2017-02-14 00:09:32 +0200 |
commit | 61364842f6f65ed723a7e3bf21fc5d6253002fa6 (patch) | |
tree | 23da8ed83041a6350cbee5950dfd0cb7ca677a08 /pylint | |
parent | 3c8b44fc27bb6e7827f37cfa995c39e28f620c42 (diff) | |
download | pylint-git-61364842f6f65ed723a7e3bf21fc5d6253002fa6.tar.gz |
refactor checker to perform import once
Diffstat (limited to 'pylint')
-rw-r--r-- | pylint/checkers/imports.py | 37 |
1 files changed, 18 insertions, 19 deletions
diff --git a/pylint/checkers/imports.py b/pylint/checkers/imports.py index 3ee80f310..5aaea7ce3 100644 --- a/pylint/checkers/imports.py +++ b/pylint/checkers/imports.py @@ -376,43 +376,44 @@ given file (report RP0402 must not be disabled)'} for name in names: self._check_deprecated_module(node, name) - importedmodnode = self._get_imported_module(node, name) + imported_module = self._get_imported_module(node, name) if isinstance(node.parent, astroid.Module): # Allow imports nested self._check_position(node) if isinstance(node.scope(), astroid.Module): - self._record_import(node, importedmodnode) + self._record_import(node, imported_module) - if importedmodnode is None: + if imported_module is None: continue - self._check_relative_import(modnode, node, importedmodnode, name) - self._add_imported_module(node, importedmodnode.name) + self._check_relative_import(modnode, node, imported_module, name) + self._add_imported_module(node, imported_module.name) @check_messages(*(MSGS.keys())) def visit_importfrom(self, node): """triggered when a from statement is seen""" basename = node.modname + imported_module = self._get_imported_module(node, basename) + self._check_misplaced_future(node) self._check_deprecated_module(node, basename) - self._check_wildcard_imports(node) + self._check_wildcard_imports(node, imported_module) self._check_same_line_imports(node) self._check_reimport(node, basename=basename, level=node.level) - modnode = node.root() - importedmodnode = self._get_imported_module(node, basename) if isinstance(node.parent, astroid.Module): # Allow imports nested self._check_position(node) if isinstance(node.scope(), astroid.Module): - self._record_import(node, importedmodnode) - if importedmodnode is None: + self._record_import(node, imported_module) + if imported_module is None: return - self._check_relative_import(modnode, node, importedmodnode, basename) + modnode = node.root() + self._check_relative_import(modnode, node, imported_module, basename) for name, _ in node.names: if name != '*': - self._add_imported_module(node, '%s.%s' % (importedmodnode.name, name)) + self._add_imported_module(node, '%s.%s' % (imported_module.name, name)) @check_messages('wrong-import-order', 'ungrouped-imports', 'wrong-import-position') @@ -738,19 +739,17 @@ given file (report RP0402 must not be disabled)'} result[importee] = importers return self.__int_dep_info - def _check_wildcard_imports(self, node): + def _check_wildcard_imports(self, node, imported_module): for name, _ in node.names: - if name == '*' and not self._wildcard_import_is_allowed(node): + if name == '*' and not self._wildcard_import_is_allowed(imported_module): self.add_message('wildcard-import', args=node.modname, node=node) - def _wildcard_import_is_allowed(self, node): + def _wildcard_import_is_allowed(self, imported_module): if not self.config.allow_wildcard_with_all: return False - try: - module = node.do_import_module() - return '__all__' in module.locals - except astroid.AstroidImportError: + if imported_module is None: return False + return '__all__' in imported_module.locals def register(linter): |