Beispiel #1
0
def _name_to_types(evaluator, context, tree_name):
    types = []
    node = tree_name.get_definition(import_name_always=True)
    if node is None:
        node = tree_name.parent
        if node.type == 'global_stmt':
            context = evaluator.create_context(context, tree_name)
            finder = NameFinder(evaluator, context, context, tree_name.value)
            filters = finder.get_filters(search_global=True)
            # For global_stmt lookups, we only need the first possible scope,
            # which means the function itself.
            filters = [next(filters)]
            return finder.find(filters, attribute_lookup=False)
        elif node.type not in ('import_from', 'import_name'):
            raise ValueError("Should not happen.")

    typ = node.type
    if typ == 'for_stmt':
        types = pep0484.find_type_from_comment_hint_for(
            context, node, tree_name)
        if types:
            return types
    if typ == 'with_stmt':
        types = pep0484.find_type_from_comment_hint_with(
            context, node, tree_name)
        if types:
            return types
    if typ in ('for_stmt', 'comp_for'):
        try:
            types = context.predefined_names[node][tree_name.value]
        except KeyError:
            cn = ContextualizedNode(context, node.children[3])
            for_types = iterable.py__iter__types(evaluator, cn.infer(), cn)
            c_node = ContextualizedName(context, tree_name)
            types = check_tuple_assignments(evaluator, c_node, for_types)
    elif typ == 'expr_stmt':
        types = _remove_statements(evaluator, context, node, tree_name)
    elif typ == 'with_stmt':
        context_managers = context.eval_node(
            node.get_test_node_from_name(tree_name))
        enter_methods = unite(
            context_manager.py__getattribute__('__enter__')
            for context_manager in context_managers)
        types = unite(method.execute_evaluated() for method in enter_methods)
    elif typ in ('import_from', 'import_name'):
        types = imports.infer_import(context, tree_name)
    elif typ in ('funcdef', 'classdef'):
        types = _apply_decorators(evaluator, context, node)
    elif typ == 'try_stmt':
        # TODO an exception can also be a tuple. Check for those.
        # TODO check for types that are not classes and add it to
        # the static analysis report.
        exceptions = context.eval_node(
            tree_name.get_previous_sibling().get_previous_sibling())
        types = unite(
            evaluator.execute(t, param.ValuesArguments([]))
            for t in exceptions)
    else:
        raise ValueError("Should not happen.")
    return types
Beispiel #2
0
def _name_to_types(evaluator, context, tree_name):
    types = []
    node = tree_name.get_definition(import_name_always=True)
    if node is None:
        node = tree_name.parent
        if node.type == 'global_stmt':
            context = evaluator.create_context(context, tree_name)
            finder = NameFinder(evaluator, context, context, tree_name.value)
            filters = finder.get_filters(search_global=True)
            # For global_stmt lookups, we only need the first possible scope,
            # which means the function itself.
            filters = [next(filters)]
            return finder.find(filters, attribute_lookup=False)
        elif node.type not in ('import_from', 'import_name'):
            raise ValueError("Should not happen.")

    typ = node.type
    if typ == 'for_stmt':
        types = pep0484.find_type_from_comment_hint_for(context, node, tree_name)
        if types:
            return types
    if typ == 'with_stmt':
        types = pep0484.find_type_from_comment_hint_with(context, node, tree_name)
        if types:
            return types
    if typ in ('for_stmt', 'comp_for'):
        try:
            types = context.predefined_names[node][tree_name.value]
        except KeyError:
            cn = ContextualizedNode(context, node.children[3])
            for_types = iterable.py__iter__types(evaluator, cn.infer(), cn)
            c_node = ContextualizedName(context, tree_name)
            types = check_tuple_assignments(evaluator, c_node, for_types)
    elif typ == 'expr_stmt':
        types = _remove_statements(evaluator, context, node, tree_name)
    elif typ == 'with_stmt':
        context_managers = context.eval_node(node.get_test_node_from_name(tree_name))
        enter_methods = unite(
            context_manager.py__getattribute__('__enter__')
            for context_manager in context_managers
        )
        types = unite(method.execute_evaluated() for method in enter_methods)
    elif typ in ('import_from', 'import_name'):
        types = imports.infer_import(context, tree_name)
    elif typ in ('funcdef', 'classdef'):
        types = _apply_decorators(evaluator, context, node)
    elif typ == 'try_stmt':
        # TODO an exception can also be a tuple. Check for those.
        # TODO check for types that are not classes and add it to
        # the static analysis report.
        exceptions = context.eval_node(tree_name.get_previous_sibling().get_previous_sibling())
        types = unite(
            evaluator.execute(t, param.ValuesArguments([]))
            for t in exceptions
        )
    else:
        raise ValueError("Should not happen.")
    return types
Beispiel #3
0
def _name_to_types(evaluator, context, tree_name):
    types = []
    node = tree_name.get_definition()
    if node.type == 'for_stmt':
        types = pep0484.find_type_from_comment_hint_for(
            context, node, tree_name)
        if types:
            return types
    if node.type == 'with_stmt':
        types = pep0484.find_type_from_comment_hint_with(
            context, node, tree_name)
        if types:
            return types
    if node.type in ('for_stmt', 'comp_for'):
        try:
            types = context.predefined_names[node][tree_name.value]
        except KeyError:
            container_types = context.eval_node(node.children[3])
            for_types = iterable.py__iter__types(evaluator, container_types,
                                                 node.children[3])
            types = check_tuple_assignments(evaluator, for_types, tree_name)
    elif node.type == 'expr_stmt':
        types = _remove_statements(evaluator, context, node, tree_name)
    elif node.type == 'with_stmt':
        types = context.eval_node(node.node_from_name(tree_name))
    elif isinstance(node, tree.Import):
        types = imports.infer_import(context, tree_name)
    elif node.type in ('funcdef', 'classdef'):
        types = _apply_decorators(evaluator, context, node)
    elif node.type == 'global_stmt':
        context = evaluator.create_context(context, tree_name)
        finder = NameFinder(evaluator, context, context, str(tree_name))
        filters = finder.get_filters(search_global=True)
        # For global_stmt lookups, we only need the first possible scope,
        # which means the function itself.
        filters = [next(filters)]
        types += finder.find(filters, attribute_lookup=False)
    elif isinstance(node, tree.TryStmt):
        # TODO an exception can also be a tuple. Check for those.
        # TODO check for types that are not classes and add it to
        # the static analysis report.
        exceptions = context.eval_node(
            tree_name.get_previous_sibling().get_previous_sibling())
        types = unite(
            evaluator.execute(t, param.ValuesArguments([]))
            for t in exceptions)
    else:
        raise ValueError("Should not happen.")
    return types
Beispiel #4
0
def _name_to_types(evaluator, context, tree_name):
    types = []
    node = tree_name.get_definition()
    if node.isinstance(tree.ForStmt):
        types = pep0484.find_type_from_comment_hint_for(context, node, tree_name)
        if types:
            return types
    if node.isinstance(tree.WithStmt):
        types = pep0484.find_type_from_comment_hint_with(context, node, tree_name)
        if types:
            return types
    if node.type in ('for_stmt', 'comp_for'):
        try:
            types = context.predefined_names[node][tree_name.value]
        except KeyError:
            container_types = context.eval_node(node.children[3])
            for_types = iterable.py__iter__types(evaluator, container_types, node.children[3])
            types = check_tuple_assignments(evaluator, for_types, tree_name)
    elif node.isinstance(tree.ExprStmt):
        types = _remove_statements(evaluator, context, node, tree_name)
    elif node.isinstance(tree.WithStmt):
        types = context.eval_node(node.node_from_name(tree_name))
    elif isinstance(node, tree.Import):
        types = imports.infer_import(context, tree_name)
    elif node.type in ('funcdef', 'classdef'):
        types = _apply_decorators(evaluator, context, node)
    elif node.type == 'global_stmt':
        context = evaluator.create_context(context, tree_name)
        finder = NameFinder(evaluator, context, context, str(tree_name))
        filters = finder.get_filters(search_global=True)
        # For global_stmt lookups, we only need the first possible scope,
        # which means the function itself.
        filters = [next(filters)]
        types += finder.find(filters, attribute_lookup=False)
    elif isinstance(node, tree.TryStmt):
        # TODO an exception can also be a tuple. Check for those.
        # TODO check for types that are not classes and add it to
        # the static analysis report.
        exceptions = context.eval_node(tree_name.get_previous_sibling().get_previous_sibling())
        types = unite(
            evaluator.execute(t, param.ValuesArguments([]))
            for t in exceptions
        )
    else:
        raise ValueError("Should not happen.")
    return types
Beispiel #5
0
def _name_to_types(evaluator, name, scope):
    typ = name.get_definition()
    if typ.isinstance(tree.ForStmt):
        types = pep0484.find_type_from_comment_hint_for(evaluator, typ, name)
        if types:
            return types
    if typ.isinstance(tree.WithStmt):
        types = pep0484.find_type_from_comment_hint_with(evaluator, typ, name)
        if types:
            return types
    if typ.isinstance(tree.ForStmt, tree.CompFor):
        container_types = evaluator.eval_element(typ.children[3])
        for_types = iterable.py__iter__types(evaluator, container_types,
                                             typ.children[3])
        types = check_tuple_assignments(evaluator, for_types, name)
    elif isinstance(typ, tree.Param):
        types = _eval_param(evaluator, typ, scope)
    elif typ.isinstance(tree.ExprStmt):
        types = _remove_statements(evaluator, typ, name)
    elif typ.isinstance(tree.WithStmt):
        types = evaluator.eval_element(typ.node_from_name(name))
    elif isinstance(typ, tree.Import):
        types = imports.ImportWrapper(evaluator, name).follow()
    elif isinstance(typ, tree.GlobalStmt):
        # TODO theoretically we shouldn't be using search_global here, it
        # doesn't make sense, because it's a local search (for that name)!
        # However, globals are not that important and resolving them doesn't
        # guarantee correctness in any way, because we don't check for when
        # something is executed.
        types = evaluator.find_types(typ.get_parent_scope(),
                                     str(name),
                                     search_global=True)
    elif isinstance(typ, tree.TryStmt):
        # TODO an exception can also be a tuple. Check for those.
        # TODO check for types that are not classes and add it to
        # the static analysis report.
        exceptions = evaluator.eval_element(
            name.get_previous_sibling().get_previous_sibling())
        types = set(
            chain.from_iterable(evaluator.execute(t) for t in exceptions))
    else:
        if typ.isinstance(er.Function):
            typ = typ.get_decorated_func()
        types = set([typ])
    return types
Beispiel #6
0
def _name_to_types(evaluator, name, scope):
    types = []
    typ = name.get_definition()
    if typ.isinstance(tree.ForStmt):
        types = pep0484.find_type_from_comment_hint_for(evaluator, typ, name)
        if types:
            return types
    if typ.isinstance(tree.WithStmt):
        types = pep0484.find_type_from_comment_hint_with(evaluator, typ, name)
        if types:
            return types
    if typ.isinstance(tree.ForStmt, tree.CompFor):
        container_types = evaluator.eval_element(typ.children[3])
        for_types = iterable.py__iter__types(evaluator, container_types,
                                             typ.children[3])
        types = check_tuple_assignments(evaluator, for_types, name)
    elif isinstance(typ, tree.Param):
        types = _eval_param(evaluator, typ, scope)
    elif typ.isinstance(tree.ExprStmt):
        types = _remove_statements(evaluator, typ, name)
    elif typ.isinstance(tree.WithStmt):
        types = evaluator.eval_element(typ.node_from_name(name))
    elif isinstance(typ, tree.Import):
        types = imports.ImportWrapper(evaluator, name).follow()
    elif typ.type == 'global_stmt':
        for s in _get_global_stmt_scopes(evaluator, typ, name):
            finder = NameFinder(evaluator, s, str(name))
            names_dicts = finder.scopes(search_global=True)
            # For global_stmt lookups, we only need the first possible scope,
            # which means the function itself.
            names_dicts = [next(names_dicts)]
            types += finder.find(names_dicts, attribute_lookup=False)
    elif isinstance(typ, tree.TryStmt):
        # TODO an exception can also be a tuple. Check for those.
        # TODO check for types that are not classes and add it to
        # the static analysis report.
        exceptions = evaluator.eval_element(
            name.get_previous_sibling().get_previous_sibling())
        types = set(
            chain.from_iterable(evaluator.execute(t) for t in exceptions))
    else:
        if typ.isinstance(er.Function):
            typ = typ.get_decorated_func()
        types = set([typ])
    return types
Beispiel #7
0
def _name_to_types(evaluator, name, scope):
    types = []
    typ = name.get_definition()
    if typ.isinstance(tree.ForStmt):
        types = pep0484.find_type_from_comment_hint_for(evaluator, typ, name)
        if types:
            return types
    if typ.isinstance(tree.WithStmt):
        types = pep0484.find_type_from_comment_hint_with(evaluator, typ, name)
        if types:
            return types
    if typ.isinstance(tree.ForStmt, tree.CompFor):
        container_types = evaluator.eval_element(typ.children[3])
        for_types = iterable.py__iter__types(evaluator, container_types, typ.children[3])
        types = check_tuple_assignments(evaluator, for_types, name)
    elif isinstance(typ, tree.Param):
        types = _eval_param(evaluator, typ, scope)
    elif typ.isinstance(tree.ExprStmt):
        types = _remove_statements(evaluator, typ, name)
    elif typ.isinstance(tree.WithStmt):
        types = evaluator.eval_element(typ.node_from_name(name))
    elif isinstance(typ, tree.Import):
        types = imports.ImportWrapper(evaluator, name).follow()
    elif typ.type == "global_stmt":
        for s in _get_global_stmt_scopes(evaluator, typ, name):
            finder = NameFinder(evaluator, s, str(name))
            names_dicts = finder.scopes(search_global=True)
            # For global_stmt lookups, we only need the first possible scope,
            # which means the function itself.
            names_dicts = [next(names_dicts)]
            types += finder.find(names_dicts, attribute_lookup=False)
    elif isinstance(typ, tree.TryStmt):
        # TODO an exception can also be a tuple. Check for those.
        # TODO check for types that are not classes and add it to
        # the static analysis report.
        exceptions = evaluator.eval_element(name.get_previous_sibling().get_previous_sibling())
        types = set(chain.from_iterable(evaluator.execute(t) for t in exceptions))
    else:
        if typ.isinstance(er.Function):
            typ = typ.get_decorated_func()
        types = set([typ])
    return types
Beispiel #8
0
def _name_to_types(evaluator, name, scope):
    typ = name.get_definition()
    if typ.isinstance(tree.ForStmt):
        types = pep0484.find_type_from_comment_hint_for(evaluator, typ, name)
        if types:
            return types
    if typ.isinstance(tree.WithStmt):
        types = pep0484.find_type_from_comment_hint_with(evaluator, typ, name)
        if types:
            return types
    if typ.isinstance(tree.ForStmt, tree.CompFor):
        container_types = evaluator.eval_element(typ.children[3])
        for_types = iterable.py__iter__types(evaluator, container_types, typ.children[3])
        types = check_tuple_assignments(evaluator, for_types, name)
    elif isinstance(typ, tree.Param):
        types = _eval_param(evaluator, typ, scope)
    elif typ.isinstance(tree.ExprStmt):
        types = _remove_statements(evaluator, typ, name)
    elif typ.isinstance(tree.WithStmt):
        types = evaluator.eval_element(typ.node_from_name(name))
    elif isinstance(typ, tree.Import):
        types = imports.ImportWrapper(evaluator, name).follow()
    elif isinstance(typ, tree.GlobalStmt):
        # TODO theoretically we shouldn't be using search_global here, it
        # doesn't make sense, because it's a local search (for that name)!
        # However, globals are not that important and resolving them doesn't
        # guarantee correctness in any way, because we don't check for when
        # something is executed.
        types = evaluator.find_types(typ.get_parent_scope(), str(name), search_global=True)
    elif isinstance(typ, tree.TryStmt):
        # TODO an exception can also be a tuple. Check for those.
        # TODO check for types that are not classes and add it to
        # the static analysis report.
        exceptions = evaluator.eval_element(name.get_previous_sibling().get_previous_sibling())
        types = set(chain.from_iterable(evaluator.execute(t) for t in exceptions))
    else:
        if typ.isinstance(er.Function):
            typ = typ.get_decorated_func()
        types = set([typ])
    return types
Beispiel #9
0
def tree_name_to_contexts(evaluator, context, tree_name):

    context_set = ContextSet()
    module_node = context.get_root_context().tree_node
    if module_node is not None:
        names = module_node.get_used_names().get(tree_name.value, [])
        for name in names:
            expr_stmt = name.parent

            correct_scope = parser_utils.get_parent_scope(name) == context.tree_node

            if expr_stmt.type == "expr_stmt" and expr_stmt.children[1].type == "annassign" and correct_scope:
                context_set |= _evaluate_for_annotation(context, expr_stmt.children[1].children[1])

    if context_set:
        return context_set

    types = []
    node = tree_name.get_definition(import_name_always=True)
    if node is None:
        node = tree_name.parent
        if node.type == 'global_stmt':
            context = evaluator.create_context(context, tree_name)
            finder = NameFinder(evaluator, context, context, tree_name.value)
            filters = finder.get_filters(search_global=True)
            # For global_stmt lookups, we only need the first possible scope,
            # which means the function itself.
            filters = [next(filters)]
            return finder.find(filters, attribute_lookup=False)
        elif node.type not in ('import_from', 'import_name'):
            raise ValueError("Should not happen. type: %s", node.type)

    typ = node.type
    if typ == 'for_stmt':
        types = pep0484.find_type_from_comment_hint_for(context, node, tree_name)
        if types:
            return types
    if typ == 'with_stmt':
        types = pep0484.find_type_from_comment_hint_with(context, node, tree_name)
        if types:
            return types

    if typ in ('for_stmt', 'comp_for'):
        try:
            types = context.predefined_names[node][tree_name.value]
        except KeyError:
            cn = ContextualizedNode(context, node.children[3])
            for_types = iterate_contexts(
                cn.infer(),
                contextualized_node=cn,
                is_async=node.parent.type == 'async_stmt',
            )
            c_node = ContextualizedName(context, tree_name)
            types = check_tuple_assignments(evaluator, c_node, for_types)
    elif typ == 'expr_stmt':
        types = _remove_statements(evaluator, context, node, tree_name)
    elif typ == 'with_stmt':
        context_managers = context.eval_node(node.get_test_node_from_name(tree_name))
        enter_methods = context_managers.py__getattribute__(u'__enter__')
        return enter_methods.execute_evaluated()
    elif typ in ('import_from', 'import_name'):
        types = imports.infer_import(context, tree_name)
    elif typ in ('funcdef', 'classdef'):
        types = _apply_decorators(context, node)
    elif typ == 'try_stmt':
        # TODO an exception can also be a tuple. Check for those.
        # TODO check for types that are not classes and add it to
        # the static analysis report.
        exceptions = context.eval_node(tree_name.get_previous_sibling().get_previous_sibling())
        types = exceptions.execute_evaluated()
    else:
        raise ValueError("Should not happen. type: %s" % typ)
    return types
Beispiel #10
0
def tree_name_to_contexts(evaluator, context, tree_name):

    context_set = ContextSet()
    module_node = context.get_root_context().tree_node
    if module_node is not None:
        names = module_node.get_used_names().get(tree_name.value, [])
        for name in names:
            expr_stmt = name.parent

            correct_scope = parser_utils.get_parent_scope(
                name) == context.tree_node

            if expr_stmt.type == "expr_stmt" and expr_stmt.children[
                    1].type == "annassign" and correct_scope:
                context_set |= _evaluate_for_annotation(
                    context, expr_stmt.children[1].children[1])

    if context_set:
        return context_set

    types = []
    node = tree_name.get_definition(import_name_always=True)
    if node is None:
        node = tree_name.parent
        if node.type == 'global_stmt':
            context = evaluator.create_context(context, tree_name)
            finder = NameFinder(evaluator, context, context, tree_name.value)
            filters = finder.get_filters(search_global=True)
            # For global_stmt lookups, we only need the first possible scope,
            # which means the function itself.
            filters = [next(filters)]
            return finder.find(filters, attribute_lookup=False)
        elif node.type not in ('import_from', 'import_name'):
            raise ValueError("Should not happen. type: %s", node.type)

    typ = node.type
    if typ == 'for_stmt':
        types = pep0484.find_type_from_comment_hint_for(
            context, node, tree_name)
        if types:
            return types
    if typ == 'with_stmt':
        types = pep0484.find_type_from_comment_hint_with(
            context, node, tree_name)
        if types:
            return types

    if typ in ('for_stmt', 'comp_for'):
        try:
            types = context.predefined_names[node][tree_name.value]
        except KeyError:
            cn = ContextualizedNode(context, node.children[3])
            for_types = iterate_contexts(
                cn.infer(),
                contextualized_node=cn,
                is_async=node.parent.type == 'async_stmt',
            )
            c_node = ContextualizedName(context, tree_name)
            types = check_tuple_assignments(evaluator, c_node, for_types)
    elif typ == 'expr_stmt':
        types = _remove_statements(evaluator, context, node, tree_name)
    elif typ == 'with_stmt':
        context_managers = context.eval_node(
            node.get_test_node_from_name(tree_name))
        enter_methods = context_managers.py__getattribute__(u'__enter__')
        return enter_methods.execute_evaluated()
    elif typ in ('import_from', 'import_name'):
        types = imports.infer_import(context, tree_name)
    elif typ in ('funcdef', 'classdef'):
        types = _apply_decorators(context, node)
    elif typ == 'try_stmt':
        # TODO an exception can also be a tuple. Check for those.
        # TODO check for types that are not classes and add it to
        # the static analysis report.
        exceptions = context.eval_node(
            tree_name.get_previous_sibling().get_previous_sibling())
        types = exceptions.execute_evaluated()
    else:
        raise ValueError("Should not happen. type: %s" % typ)
    return types