示例#1
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)

        self._check_import_as_rename(node)
        self._check_misplaced_future(node)
        self.check_deprecated_module(node, basename)
        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)
示例#2
0
def _process_import_from(impfrom: ImportFrom, name: str, pkgfiles: Set[str]):
    realname = impfrom.real_name(name)

    # First try to load the import as module, if not present, let's try to
    # import a symbol
    try:
        mod = impfrom.do_import_module(impfrom.modname + '.' + realname)
        if not _is_module_packaged(mod, pkgfiles):
            return set()

        syms = set()
        for pub_name in mod.public_names():
            syms.update(_get_provides_from_name(mod, pub_name, pkgfiles))
        return {name + '.' + s for s in syms}
    except AstroidImportError:
        # If we reach here modname.realname cannot be imported as module. Hence
        # we must then try to load a symbol called realname from the package
        # named modname
        pass

    mod = impfrom.do_import_module(impfrom.modname)
    if not _is_module_packaged(mod, pkgfiles):
        return set()

    # Do lookup in the package and generate symbols
    syms = _get_provides_from_name(mod, realname, pkgfiles)
    if realname == name:
        return syms

    # replace "real name" prefix by "as name"
    as_syms = set()
    for sym in syms:
        # get the suffix part of prefix.suffix pattern in sym. If sym does not
        # contain '.', suffix will be ''
        suffix = sym.split(sep='.', maxsplit=1)[1:]
        as_syms.add(name + '.' + suffix if suffix else name)
    return as_syms
 def visit_importfrom(self, node: ImportFrom) -> None:
     # handle 'from . import foo, bar'
     imported = [node.modname
                 ] if node.modname else [n for n, _ in node.names]
     for modname in imported:
         self._check_import(
             node,
             ModuleName(modname)
             if node.level is None else _get_absolute_importee(
                 root_name=node.root().name,
                 modname=modname,
                 level=node.level,
                 is_package=_is_package(node),
             ),
         )
示例#4
0
    def visit_importfrom(self, node: nodes.ImportFrom) -> None:
        """Visit an astroid.ImportFrom node.

        resolve module dependencies
        """
        basename = node.modname
        context_file = node.root().file
        if context_file is not None:
            relative = astroid.modutils.is_relative(basename, context_file)
        else:
            relative = False
        for name in node.names:
            if name[0] == "*":
                continue
            # analyze dependencies
            fullname = f"{basename}.{name[0]}"
            if fullname.find(".") > -1:
                try:
                    fullname = astroid.modutils.get_module_part(fullname, context_file)
                except ImportError:
                    continue
            if fullname != basename:
                self._imported_module(node, fullname, relative)
示例#5
0
 def add_from_depend(self, node: nodes.ImportFrom, from_module: str) -> None:
     """Add dependencies created by from-imports."""
     mod_name = node.root().name
     obj = self.module(mod_name)
     if from_module not in obj.node.depends:
         obj.node.depends.append(from_module)