示例#1
0
    def _get_imported_module(self, importnode, modname):
        try:
            return importnode.do_import_module(modname)
        except astroid.TooManyLevelsError:
            if _ignore_import_failure(importnode, modname,
                                      self._ignored_modules):
                return None
            self.add_message("relative-beyond-top-level", node=importnode)
        except astroid.AstroidSyntaxError as exc:
            message = f"Cannot import {modname!r} due to syntax error {str(exc.error)!r}"  # pylint: disable=no-member; false positive
            self.add_message("syntax-error",
                             line=importnode.lineno,
                             args=message)

        except astroid.AstroidBuildingException:
            if not self.linter.is_message_enabled("import-error"):
                return None
            if _ignore_import_failure(importnode, modname,
                                      self._ignored_modules):
                return None
            if not self.config.analyse_fallback_blocks and is_from_fallback_block(
                    importnode):
                return None

            dotted_modname = get_import_name(importnode, modname)
            self.add_message("import-error",
                             args=repr(dotted_modname),
                             node=importnode)
        return None
示例#2
0
 def visit_importfrom(self, node: nodes.ImportFrom) -> None:
     """Triggered when a from statement is seen."""
     basename = node.modname
     basename = get_import_name(node, basename)
     self.check_deprecated_module(node, basename)
     class_names = (name for name, _ in node.names)
     self.check_deprecated_class(node, basename, class_names)
示例#3
0
    def visit_importfrom(self, node: nodes.ImportFrom) -> None:
        """Triggered when a from statement is seen."""
        basename = node.modname
        imported_module = self._get_imported_module(node, basename)
        absolute_name = get_import_name(node, basename)

        self._check_import_as_rename(node)
        self._check_misplaced_future(node)
        self.check_deprecated_module(node, absolute_name)
        self._check_preferred_module(node, basename)
        self._check_wildcard_imports(node, imported_module)
        self._check_same_line_imports(node)
        self._check_reimport(node, basename=basename, level=node.level)
        self._check_toplevel(node)

        if isinstance(node.parent, nodes.Module):
            # Allow imports nested
            self._check_position(node)
        if isinstance(node.scope(), nodes.Module):
            self._record_import(node, imported_module)
        if imported_module is None:
            return
        for name, _ in node.names:
            if name != "*":
                self._add_imported_module(node,
                                          f"{imported_module.name}.{name}")
            else:
                self._add_imported_module(node, imported_module.name)