Пример #1
0
def overrides_a_method(class_node: astroid.node_classes.NodeNG,
                       name: str) -> bool:
    """return True if <name> is a method overridden from an ancestor"""
    for ancestor in class_node.ancestors():
        if name in ancestor and isinstance(ancestor[name], astroid.FunctionDef):
            return True
    return False
Пример #2
0
def inherit_from_std_ex(node: astroid.node_classes.NodeNG) -> bool:
    """
    Return true if the given class node is subclass of
    exceptions.Exception.
    """
    ancestors = node.ancestors() if hasattr(node, "ancestors") else []
    for ancestor in itertools.chain([node], ancestors):
        if (ancestor.name in ("Exception", "BaseException")
                and ancestor.root().name == EXCEPTIONS_MODULE):
            return True
    return False
Пример #3
0
def inherit_from_std_ex(node: astroid.node_classes.NodeNG) -> bool:
    """
    Return true if the given class node is subclass of
    exceptions.Exception.
    """
    if (node.name in ("Exception", "BaseException")
            and node.root().name == EXCEPTIONS_MODULE):
        return True
    if not hasattr(node, "ancestors"):
        return False
    return any(
        inherit_from_std_ex(parent) for parent in node.ancestors(recurs=True))
Пример #4
0
def is_protocol_class(cls: astroid.node_classes.NodeNG) -> bool:
    """Check if the given node represents a protocol class

    :param cls: The node to check
    :returns: True if the node is a typing protocol class, false otherwise.
    """
    if not isinstance(cls, astroid.ClassDef):
        return False

    # Use .ancestors() since not all protocol classes can have
    # their mro deduced.
    return any(parent.qname() in TYPING_PROTOCOLS for parent in cls.ancestors())
Пример #5
0
def inherit_from_std_ex(node: astroid.node_classes.NodeNG) -> bool:
    """
    Return true if the given class node is subclass of
    exceptions.Exception.
    """
    if node.name in ('Exception', 'BaseException') \
            and node.root().name == EXCEPTIONS_MODULE:
        return True
    if not hasattr(node, 'ancestors'):
        return False
    return any(inherit_from_std_ex(parent)
               for parent in node.ancestors(recurs=True))