当前位置: 首页>>代码示例>>Python>>正文


Python astroid.Instance方法代码示例

本文整理汇总了Python中astroid.Instance方法的典型用法代码示例。如果您正苦于以下问题:Python astroid.Instance方法的具体用法?Python astroid.Instance怎么用?Python astroid.Instance使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在astroid的用法示例。


在下文中一共展示了astroid.Instance方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: lookup

# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Instance [as 别名]
def lookup(node, name):
    """Lookup the given special method name in the given *node*

    If the special method was found, then a list of attributes
    will be returned. Otherwise, `astroid.AttributeInferenceError`
    is going to be raised.
    """
    if isinstance(node, (astroid.List,
                         astroid.Tuple,
                         astroid.Const,
                         astroid.Dict,
                         astroid.Set)):
        return _builtin_lookup(node, name)
    elif isinstance(node, astroid.Instance):
        return _lookup_in_mro(node, name)
    elif isinstance(node, astroid.ClassDef):
        return _class_lookup(node, name)

    raise exceptions.AttributeInferenceError(
        attribute=name,
        target=node
    ) 
开发者ID:AtomLinter,项目名称:linter-pylama,代码行数:24,代码来源:dunder_lookup.py

示例2: visit_tuple

# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Instance [as 别名]
def visit_tuple(self, tuple_node):
        if PY3K or not tuple_node.elts:
            self._checker.add_message('raising-bad-type',
                                      node=self._node,
                                      args='tuple')
            return

        # On Python 2, using the following is not an error:
        #    raise (ZeroDivisionError, None)
        #    raise (ZeroDivisionError, )
        # What's left to do is to check that the first
        # argument is indeed an exception. Verifying the other arguments
        # is not the scope of this check.
        first = tuple_node.elts[0]
        inferred = utils.safe_infer(first)
        if not inferred or inferred is astroid.Uninferable:
            return

        if (isinstance(inferred, astroid.Instance)
                and inferred.__class__.__name__ != 'Instance'):
            # TODO: explain why
            self.visit_default(tuple_node)
        else:
            self.visit(inferred) 
开发者ID:AtomLinter,项目名称:linter-pylama,代码行数:26,代码来源:exceptions.py

示例3: visit_attribute

# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Instance [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

示例4: is_method_call

# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Instance [as 别名]
def is_method_call(func, types=(), methods=()):
    """Determines if a BoundMethod node represents a method call.

    Args:
      func (astroid.BoundMethod): The BoundMethod AST node to check.
      types (Optional[String]): Optional sequence of caller type names to restrict check.
      methods (Optional[String]): Optional sequence of method names to restrict check.

    Returns:
      bool: true if the node represents a method call for the given type and
      method names, False otherwise.
    """
    return (isinstance(func, astroid.BoundMethod)
            and isinstance(func.bound, astroid.Instance)
            and (func.bound.name in types if types else True)
            and (func.name in methods if methods else True)) 
开发者ID:AtomLinter,项目名称:linter-pylama,代码行数:18,代码来源:logging.py

示例5: _has_data_descriptor

# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Instance [as 别名]
def _has_data_descriptor(cls, attr):
    attributes = cls.getattr(attr)
    for attribute in attributes:
        try:
            for inferred in attribute.infer():
                if isinstance(inferred, astroid.Instance):
                    try:
                        inferred.getattr('__get__')
                        inferred.getattr('__set__')
                    except astroid.NotFoundError:
                        continue
                    else:
                        return True
        except astroid.InferenceError:
            # Can't infer, avoid emitting a false positive in this case.
            return True
    return False 
开发者ID:AtomLinter,项目名称:linter-pylama,代码行数:19,代码来源:classes.py

示例6: _check_proper_bases

# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Instance [as 别名]
def _check_proper_bases(self, node):
        """
        Detect that a class inherits something which is not
        a class or a type.
        """
        for base in node.bases:
            ancestor = safe_infer(base)
            if ancestor in (astroid.YES, None):
                continue
            if (isinstance(ancestor, astroid.Instance) and
                    ancestor.is_subtype_of('%s.type' % (BUILTINS,))):
                continue

            if (not isinstance(ancestor, astroid.ClassDef) or
                    _is_invalid_base_class(ancestor)):
                self.add_message('inherit-non-class',
                                 args=base.as_string(), node=node) 
开发者ID:AtomLinter,项目名称:linter-pylama,代码行数:19,代码来源:classes.py

示例7: _is_iterator

# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Instance [as 别名]
def _is_iterator(node):
        if node is astroid.YES:
            # Just ignore YES objects.
            return True
        if isinstance(node, Generator):
            # Generators can be itered.
            return True

        if isinstance(node, astroid.Instance):
            try:
                node.local_attr(NEXT_METHOD)
                return True
            except astroid.NotFoundError:
                pass
        elif isinstance(node, astroid.ClassDef):
            metaclass = node.metaclass()
            if metaclass and isinstance(metaclass, astroid.ClassDef):
                try:
                    metaclass.local_attr(NEXT_METHOD)
                    return True
                except astroid.NotFoundError:
                    pass
        return False 
开发者ID:AtomLinter,项目名称:linter-pylama,代码行数:25,代码来源:classes.py

示例8: lookup

# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Instance [as 别名]
def lookup(node, name):
    """Lookup the given special method name in the given *node*

    If the special method was found, then a list of attributes
    will be returned. Otherwise, `astroid.AttributeInferenceError`
    is going to be raised.
    """
    if isinstance(
        node, (astroid.List, astroid.Tuple, astroid.Const, astroid.Dict, astroid.Set)
    ):
        return _builtin_lookup(node, name)
    if isinstance(node, astroid.Instance):
        return _lookup_in_mro(node, name)
    if isinstance(node, astroid.ClassDef):
        return _class_lookup(node, name)

    raise exceptions.AttributeInferenceError(attribute=name, target=node) 
开发者ID:sofia-netsurv,项目名称:python-netsurv,代码行数:19,代码来源:dunder_lookup.py

示例9: test_pylint_config_attr

# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Instance [as 别名]
def test_pylint_config_attr():
    mod = astroid.MANAGER.ast_from_module_name("pylint.lint")
    pylinter = mod["PyLinter"]
    expect = [
        "OptionsManagerMixIn",
        "object",
        "MessagesHandlerMixIn",
        "ReportsHandlerMixIn",
        "BaseTokenChecker",
        "BaseChecker",
        "OptionsProviderMixIn",
    ]
    assert [c.name for c in pylinter.ancestors()] == expect
    assert list(astroid.Instance(pylinter).getattr("config"))
    inferred = list(astroid.Instance(pylinter).igetattr("config"))
    assert len(inferred) == 1
    assert inferred[0].root().name == "optparse"
    assert inferred[0].name == "Values" 
开发者ID:sofia-netsurv,项目名称:python-netsurv,代码行数:20,代码来源:test_regr.py

示例10: visit_tuple

# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Instance [as 别名]
def visit_tuple(self, tuple_node):
        if PY3K or not tuple_node.elts:
            self._checker.add_message("raising-bad-type", node=self._node, args="tuple")
            return

        # On Python 2, using the following is not an error:
        #    raise (ZeroDivisionError, None)
        #    raise (ZeroDivisionError, )
        # What's left to do is to check that the first
        # argument is indeed an exception. Verifying the other arguments
        # is not the scope of this check.
        first = tuple_node.elts[0]
        inferred = utils.safe_infer(first)
        if not inferred or inferred is astroid.Uninferable:
            return

        if (
            isinstance(inferred, astroid.Instance)
            and inferred.__class__.__name__ != "Instance"
        ):
            # TODO: explain why
            self.visit_default(tuple_node)
        else:
            self.visit(inferred) 
开发者ID:sofia-netsurv,项目名称:python-netsurv,代码行数:26,代码来源:exceptions.py

示例11: visit_attribute

# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Instance [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: is_method_call

# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Instance [as 别名]
def is_method_call(func, types=(), methods=()):
    """Determines if a BoundMethod node represents a method call.

    Args:
      func (astroid.BoundMethod): The BoundMethod AST node to check.
      types (Optional[String]): Optional sequence of caller type names to restrict check.
      methods (Optional[String]): Optional sequence of method names to restrict check.

    Returns:
      bool: true if the node represents a method call for the given type and
      method names, False otherwise.
    """
    return (
        isinstance(func, astroid.BoundMethod)
        and isinstance(func.bound, astroid.Instance)
        and (func.bound.name in types if types else True)
        and (func.name in methods if methods else True)
    ) 
开发者ID:sofia-netsurv,项目名称:python-netsurv,代码行数:20,代码来源:logging.py

示例13: _has_data_descriptor

# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Instance [as 别名]
def _has_data_descriptor(cls, attr):
    attributes = cls.getattr(attr)
    for attribute in attributes:
        try:
            for inferred in attribute.infer():
                if isinstance(inferred, astroid.Instance):
                    try:
                        inferred.getattr("__get__")
                        inferred.getattr("__set__")
                    except astroid.NotFoundError:
                        continue
                    else:
                        return True
        except astroid.InferenceError:
            # Can't infer, avoid emitting a false positive in this case.
            return True
    return False 
开发者ID:sofia-netsurv,项目名称:python-netsurv,代码行数:19,代码来源:classes.py

示例14: _check_proper_bases

# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Instance [as 别名]
def _check_proper_bases(self, node):
        """
        Detect that a class inherits something which is not
        a class or a type.
        """
        for base in node.bases:
            ancestor = safe_infer(base)
            if ancestor in (astroid.Uninferable, None):
                continue
            if isinstance(ancestor, astroid.Instance) and ancestor.is_subtype_of(
                "%s.type" % (BUILTINS,)
            ):
                continue

            if not isinstance(ancestor, astroid.ClassDef) or _is_invalid_base_class(
                ancestor
            ):
                self.add_message("inherit-non-class", args=base.as_string(), node=node)

            if ancestor.name == object.__name__:
                self.add_message(
                    "useless-object-inheritance", args=node.name, node=node
                ) 
开发者ID:sofia-netsurv,项目名称:python-netsurv,代码行数:25,代码来源:classes.py

示例15: _is_iterator

# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Instance [as 别名]
def _is_iterator(node):
        if node is astroid.Uninferable:
            # Just ignore Uninferable objects.
            return True
        if isinstance(node, Generator):
            # Generators can be itered.
            return True

        if isinstance(node, astroid.Instance):
            try:
                node.local_attr(NEXT_METHOD)
                return True
            except astroid.NotFoundError:
                pass
        elif isinstance(node, astroid.ClassDef):
            metaclass = node.metaclass()
            if metaclass and isinstance(metaclass, astroid.ClassDef):
                try:
                    metaclass.local_attr(NEXT_METHOD)
                    return True
                except astroid.NotFoundError:
                    pass
        return False 
开发者ID:sofia-netsurv,项目名称:python-netsurv,代码行数:25,代码来源:classes.py


注:本文中的astroid.Instance方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。