當前位置: 首頁>>代碼示例>>Python>>正文


Python astroid.Module方法代碼示例

本文整理匯總了Python中astroid.Module方法的典型用法代碼示例。如果您正苦於以下問題:Python astroid.Module方法的具體用法?Python astroid.Module怎麽用?Python astroid.Module使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在astroid的用法示例。


在下文中一共展示了astroid.Module方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: visit_importfrom

# 需要導入模塊: import astroid [as 別名]
# 或者: from astroid import Module [as 別名]
def visit_importfrom(self, node):
        if node.modname == '__future__':
            for name, _ in node.names:
                if name == 'division':
                    self._future_division = True
                elif name == 'absolute_import':
                    self._future_absolute_import = True
        else:
            if not self._future_absolute_import:
                if self.linter.is_message_enabled('no-absolute-import'):
                    self.add_message('no-absolute-import', node=node)
                    self._future_absolute_import = True
            if not _is_conditional_import(node) and not node.level:
                self._warn_if_deprecated(node, node.modname, {x[0] for x in node.names})

        if node.names[0][0] == '*':
            if self.linter.is_message_enabled('import-star-module-level'):
                if not isinstance(node.scope(), astroid.Module):
                    self.add_message('import-star-module-level', node=node) 
開發者ID:AtomLinter,項目名稱:linter-pylama,代碼行數:21,代碼來源:python3.py

示例2: visit_attribute

# 需要導入模塊: import astroid [as 別名]
# 或者: from astroid import Module [as 別名]
def visit_attribute(self, node):
        """Look for removed attributes"""
        if node.attrname == 'xreadlines':
            self.add_message('xreadlines-attribute', node=node)
            return

        exception_message = 'message'
        try:
            for inferred in node.expr.infer():
                if (isinstance(inferred, astroid.Instance) and
                        utils.inherit_from_std_ex(inferred)):
                    if node.attrname == exception_message:

                        # Exceptions with .message clearly defined are an exception
                        if exception_message in inferred.instance_attrs:
                            continue
                        self.add_message('exception-message-attribute', node=node)
                if isinstance(inferred, astroid.Module):
                    self._warn_if_deprecated(node, inferred.name, {node.attrname},
                                             report_on_modules=False)
        except astroid.InferenceError:
            return 
開發者ID:AtomLinter,項目名稱:linter-pylama,代碼行數:24,代碼來源:python3.py

示例3: visit_importfrom

# 需要導入模塊: import astroid [as 別名]
# 或者: from astroid import Module [as 別名]
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, imported_module)
        self._check_same_line_imports(node)
        self._check_reimport(node, basename=basename, level=node.level)

        if isinstance(node.parent, astroid.Module):
            # Allow imports nested
            self._check_position(node)
        if isinstance(node.scope(), astroid.Module):
            self._record_import(node, imported_module)
        if imported_module is None:
            return
        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' % (imported_module.name, name)) 
開發者ID:AtomLinter,項目名稱:linter-pylama,代碼行數:26,代碼來源:imports.py

示例4: visit_functiondef

# 需要導入模塊: import astroid [as 別名]
# 或者: from astroid import Module [as 別名]
def visit_functiondef(self, node):
        if not self.linter.is_message_enabled('wrong-import-position', node.fromlineno):
            return
        # If it is the first non import instruction of the module, record it.
        if self._first_non_import_node:
            return

        # Check if the node belongs to an `If` or a `Try` block. If they
        # contain imports, skip recording this node.
        if not isinstance(node.parent.scope(), astroid.Module):
            return

        root = node
        while not isinstance(root.parent, astroid.Module):
            root = root.parent

        if isinstance(root, (astroid.If, astroid.TryFinally, astroid.TryExcept)):
            if any(root.nodes_of_class((astroid.Import, astroid.ImportFrom))):
                return

        self._first_non_import_node = node 
開發者ID:AtomLinter,項目名稱:linter-pylama,代碼行數:23,代碼來源:imports.py

示例5: functiondef_node

# 需要導入模塊: import astroid [as 別名]
# 或者: from astroid import Module [as 別名]
def functiondef_node(draw, name=None, annotated=False, returns=False):
    name = name or draw(valid_identifier())
    args = draw(arguments_node(annotated))
    body = []
    returns_node = astroid.Return()
    arg_node, arg_type_node = draw(hs.sampled_from(list(zip(args.args, args.annotations))))
    if returns:
        returns_node.postinit(arg_node)
    else:
        returns_node.postinit(const_node(None))
    body.append(returns_node)
    node = astroid.FunctionDef(name=name)
    node.parent = astroid.Module('Default', None)
    node.postinit(
        args,
        body,
        None,
        arg_type_node
    )
    return node 
開發者ID:pyta-uoft,項目名稱:pyta,代碼行數:22,代碼來源:custom_hypothesis_support.py

示例6: _transfer

# 需要導入模塊: import astroid [as 別名]
# 或者: from astroid import Module [as 別名]
def _transfer(self, block: CFGBlock, in_facts: Set[str], local_vars: Set[str]) -> Set[str]:
        gen = in_facts.copy()
        kill = set()
        for statement in block.statements:
            if isinstance(statement, astroid.FunctionDef):
                continue
            for node in statement.nodes_of_class((astroid.AssignName, astroid.DelName, astroid.Name),
                                              astroid.FunctionDef):
                if isinstance(node, astroid.AssignName):
                    gen.add(node.name)
                elif isinstance(node, astroid.DelName):
                    kill.add(node.name)
                else:
                    name = node.name
                    # comment out 'self.config....' check when running tests
                    if not (name in astroid.Module.scope_attrs or utils.is_builtin(name)) \
                            and name in local_vars \
                            and name not in gen.difference(kill):
                        self._possibly_undefined.add(node)
                    elif node in self._possibly_undefined:
                        self._possibly_undefined.remove(node)
        return gen.difference(kill) 
開發者ID:pyta-uoft,項目名稱:pyta,代碼行數:24,代碼來源:possibly_undefined_checker.py

示例7: _get_assigns

# 需要導入模塊: import astroid [as 別名]
# 或者: from astroid import Module [as 別名]
def _get_assigns(self, node: Union[astroid.FunctionDef, astroid.Module]) -> Set[str]:
        """Returns a set of all local and parameter variables that could be
        defined in the program (either a function or module).

        IF a variable 'v' is defined in a function and there is no global/nonlocal
        statement applied to 'v' THEN 'v' is a local variable.

        Note that `local variable` in the context of a module level analysis,
        refers to global variables.
        """
        assigns = set()
        kills = set()
        for name, nodes in node.scope().locals.items():
            if any(isinstance(elem, astroid.AssignName) for elem in nodes):
                assigns.add(name)
        for statement in node.nodes_of_class((astroid.Nonlocal, astroid.Global,
                                              astroid.ImportFrom, astroid.Import), astroid.FunctionDef):
            for name in statement.names:
                if type(name) is tuple:
                    # name[1] is the alias of the imported object/var name[0]
                    # name[1] == str or None
                    name = name[1] or name[0]
                kills.add(name)

        return assigns.difference(kills) 
開發者ID:pyta-uoft,項目名稱:pyta,代碼行數:27,代碼來源:possibly_undefined_checker.py

示例8: _get_assigns

# 需要導入模塊: import astroid [as 別名]
# 或者: from astroid import Module [as 別名]
def _get_assigns(self, node: Union[astroid.FunctionDef, astroid.Module]) -> Set[str]:
        """Returns a set of all local and parameter variables that could be
        defined in the program (either a function or module).

        IF a variable 'v' is defined in a function and there is no global/nonlocal
        statement applied to 'v' THEN 'v' is a local variable.

        Note that `local variable` in the context of a module level analysis,
        refers to global variables.
        """
        assigns = set()
        kills = set()
        for name, nodes in node.locals.items():
            if any(isinstance(elem, astroid.AssignName) for elem in nodes):
                assigns.add(name)
        return assigns.difference(kills) 
開發者ID:pyta-uoft,項目名稱:pyta,代碼行數:18,代碼來源:redundant_assignment_checker.py

示例9: display

# 需要導入模塊: import astroid [as 別名]
# 或者: from astroid import Module [as 別名]
def display(cfgs: Dict[NodeNG, ControlFlowGraph],
            filename: str, view: bool = True) -> None:
    graph = graphviz.Digraph(name=filename, **GRAPH_OPTIONS)
    for node, cfg in cfgs.items():
        if isinstance(node, astroid.Module):
            subgraph_label = '__main__'
        elif isinstance(node, astroid.FunctionDef):
            subgraph_label = node.name
        else:
            continue
        with graph.subgraph(name=f'cluster_{id(node)}') as c:
            visited = set()
            _visit(cfg.start, c, visited)
            for block in cfg.unreachable_blocks:
                _visit(block, c, visited)
            c.attr(label=subgraph_label)

    graph.render(filename, view=view) 
開發者ID:pyta-uoft,項目名稱:pyta,代碼行數:20,代碼來源:draw_cfg.py

示例10: visit_importfrom

# 需要導入模塊: import astroid [as 別名]
# 或者: from astroid import Module [as 別名]
def visit_importfrom(self, node):
        if node.modname == "__future__":
            for name, _ in node.names:
                if name == "division":
                    self._future_division = True
                elif name == "absolute_import":
                    self._future_absolute_import = True
        else:
            if not self._future_absolute_import:
                if self.linter.is_message_enabled("no-absolute-import"):
                    self.add_message("no-absolute-import", node=node)
                    self._future_absolute_import = True
            if not _is_conditional_import(node) and not node.level:
                self._warn_if_deprecated(node, node.modname, {x[0] for x in node.names})

        if node.names[0][0] == "*":
            if self.linter.is_message_enabled("import-star-module-level"):
                if not isinstance(node.scope(), astroid.Module):
                    self.add_message("import-star-module-level", node=node) 
開發者ID:sofia-netsurv,項目名稱:python-netsurv,代碼行數:21,代碼來源:python3.py

示例11: visit_attribute

# 需要導入模塊: import astroid [as 別名]
# 或者: from astroid import Module [as 別名]
def visit_attribute(self, node):
        """Look for removed attributes"""
        if node.attrname == "xreadlines":
            self.add_message("xreadlines-attribute", node=node)
            return

        exception_message = "message"
        try:
            for inferred in node.expr.infer():
                if isinstance(inferred, astroid.Instance) and utils.inherit_from_std_ex(
                    inferred
                ):
                    if node.attrname == exception_message:

                        # Exceptions with .message clearly defined are an exception
                        if exception_message in inferred.instance_attrs:
                            continue
                        self.add_message("exception-message-attribute", node=node)
                if isinstance(inferred, astroid.Module):
                    self._warn_if_deprecated(
                        node, inferred.name, {node.attrname}, report_on_modules=False
                    )
        except astroid.InferenceError:
            return 
開發者ID:sofia-netsurv,項目名稱:python-netsurv,代碼行數:26,代碼來源:python3.py

示例12: _get_import_name

# 需要導入模塊: import astroid [as 別名]
# 或者: from astroid import Module [as 別名]
def _get_import_name(importnode, modname):
    """Get a prepared module name from the given import node

    In the case of relative imports, this will return the
    absolute qualified module name, which might be useful
    for debugging. Otherwise, the initial module name
    is returned unchanged.
    """
    if isinstance(importnode, astroid.ImportFrom):
        if importnode.level:
            root = importnode.root()
            if isinstance(root, astroid.Module):
                modname = root.relative_to_absolute_name(
                    modname, level=importnode.level
                )
    return modname 
開發者ID:sofia-netsurv,項目名稱:python-netsurv,代碼行數:18,代碼來源:imports.py

示例13: visit_import

# 需要導入模塊: import astroid [as 別名]
# 或者: from astroid import Module [as 別名]
def visit_import(self, node):
        """triggered when an import statement is seen"""
        self._check_reimport(node)
        self._check_import_as_rename(node)

        modnode = node.root()
        names = [name for name, _ in node.names]
        if len(names) >= 2:
            self.add_message("multiple-imports", args=", ".join(names), node=node)

        for name in names:
            self._check_deprecated_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, imported_module)

            if imported_module is None:
                continue

            self._check_relative_import(modnode, node, imported_module, name)
            self._add_imported_module(node, imported_module.name) 
開發者ID:sofia-netsurv,項目名稱:python-netsurv,代碼行數:26,代碼來源:imports.py

示例14: visit_functiondef

# 需要導入模塊: import astroid [as 別名]
# 或者: from astroid import Module [as 別名]
def visit_functiondef(self, node):
        if not self.linter.is_message_enabled("wrong-import-position", node.fromlineno):
            return
        # If it is the first non import instruction of the module, record it.
        if self._first_non_import_node:
            return

        # Check if the node belongs to an `If` or a `Try` block. If they
        # contain imports, skip recording this node.
        if not isinstance(node.parent.scope(), astroid.Module):
            return

        root = node
        while not isinstance(root.parent, astroid.Module):
            root = root.parent

        if isinstance(root, (astroid.If, astroid.TryFinally, astroid.TryExcept)):
            if any(root.nodes_of_class((astroid.Import, astroid.ImportFrom))):
                return

        self._first_non_import_node = node 
開發者ID:sofia-netsurv,項目名稱:python-netsurv,代碼行數:23,代碼來源:imports.py

示例15: _get_nomember_msgid_hint

# 需要導入模塊: import astroid [as 別名]
# 或者: from astroid import Module [as 別名]
def _get_nomember_msgid_hint(self, node, owner):
        suggestions_are_possible = self._suggestion_mode and isinstance(
            owner, astroid.Module
        )
        if suggestions_are_possible and _is_c_extension(owner):
            msg = "c-extension-no-member"
            hint = ""
        else:
            msg = "no-member"
            if self.config.missing_member_hint:
                hint = _missing_member_hint(
                    owner,
                    node.attrname,
                    self.config.missing_member_hint_distance,
                    self.config.missing_member_max_choices,
                )
            else:
                hint = ""
        return msg, hint 
開發者ID:sofia-netsurv,項目名稱:python-netsurv,代碼行數:21,代碼來源:typecheck.py


注:本文中的astroid.Module方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。