Ejemplo n.º 1
0
def is_requests_func(node: NodeNG) -> bool:
    """
    Checks if the node represents a requests HTTP call function.

    Examples are: requests.get/post/..., requests.Session.get/post/...
    """

    # simple case
    if isinstance(node, nodes.Attribute) and node.attrname in SESSION_METHODS:
        if (
            is_requests_api_module(node.expr)
            and node.attrname not in SESSION_ONLY_METHODS
        ) or is_requests_session(node.expr):
            return True

    if isinstance(node, nodes.Name):
        _, assigns = node.lookup(node.name)
        for assign in assigns:
            if (
                isinstance(assign, nodes.ImportFrom)
                and assign.modname in REQUESTS_API_MODULES
                and assign.real_name(node.name) in HTTP_METHOD_FUNCTIONS
            ):
                return True

            if hasattr(assign, 'assign_type'):
                assign = assign.assign_type()

            if isinstance(assign, nodes.Assign) and is_requests_func(assign.value):
                return True

    return False
Ejemplo n.º 2
0
def is_requests_api_module(node: NodeNG) -> bool:
    """True if node represents the requests or requests.api module."""

    if isinstance(node, (nodes.Name, nodes.Expr)):
        _, assigns = node.lookup(node.name)
        for assign in assigns:
            if (
                isinstance(assign, (nodes.Import, nodes.ImportFrom))
                and assign.real_name(node.name) in REQUESTS_API_MODULES
            ):
                return True

    return False
Ejemplo n.º 3
0
def _lookup(node: NodeNG) -> List[NodeNG]:
    """Given a name or attribute node, returns the assignments to that name/attr."""

    if isinstance(node, nodes.Name):
        return node.lookup(node.name)[1]

    if isinstance(node, nodes.Attribute):
        try:
            obj = next(node.expr.infer())
        except InferenceError:
            return []

        if isinstance(obj, bases.Instance):
            try:
                return obj.getattr(node.attrname)
            except AttributeInferenceError:
                return []

    return []