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
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
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
def goto_definitions(self, name): def_ = name.get_definition() is_simple_name = name.parent.type not in ("power", "trailer") if is_simple_name: if name.parent.type in ("file_input", "classdef", "funcdef"): return [self.wrap(name.parent)] if def_.type == "expr_stmt" and name in def_.get_defined_names(): return self.eval_statement(def_, name) elif def_.type == "for_stmt": container_types = self.eval_element(def_.children[3]) for_types = iterable.py__iter__types(self, container_types, def_.children[3]) return finder.check_tuple_assignments(self, for_types, name) elif def_.type in ("import_from", "import_name"): return imports.ImportWrapper(self, name).follow() call = helpers.call_of_leaf(name) return self.eval_element(call)
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
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
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
def goto_definitions(self, name): def_ = name.get_definition() is_simple_name = name.parent.type not in ('power', 'trailer') if is_simple_name: if name.parent.type in ('file_input', 'classdef', 'funcdef'): return [self.wrap(name.parent)] if def_.type == 'expr_stmt' and name in def_.get_defined_names(): return self.eval_statement(def_, name) elif def_.type == 'for_stmt': container_types = self.eval_element(def_.children[3]) for_types = iterable.py__iter__types(self, container_types, def_.children[3]) return finder.check_tuple_assignments(self, for_types, name) elif def_.type in ('import_from', 'import_name'): return imports.ImportWrapper(self, name).follow() call = helpers.call_of_leaf(name) return self.eval_element(call)
def goto_definitions(self, name): def_ = name.get_definition() is_simple_name = name.parent.type not in ('power', 'trailer') if is_simple_name: if name.parent.type == 'classdef' and name.parent.name == name: return [self.wrap(name.parent)] if name.parent.type in ('file_input', 'funcdef'): return [self.wrap(name.parent)] if def_.type == 'expr_stmt' and name in def_.get_defined_names(): return self.eval_statement(def_, name) elif def_.type == 'for_stmt': container_types = self.eval_element(def_.children[3]) for_types = iterable.py__iter__types(self, container_types, def_.children[3]) return finder.check_tuple_assignments(self, for_types, name) elif def_.type in ('import_from', 'import_name'): return imports.ImportWrapper(self, name).follow() call = helpers.call_of_leaf(name) return self.eval_element(call)
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
def goto_definitions(self, context, name): def_ = name.get_definition() is_simple_name = name.parent.type not in ('power', 'trailer') if is_simple_name: if name.parent.type == 'classdef' and name.parent.name == name: return [er.ClassContext(self, name.parent, context)] elif name.parent.type == 'funcdef': return [er.FunctionContext(self, context, name.parent)] elif name.parent.type == 'file_input': raise NotImplementedError if def_.type == 'expr_stmt' and name in def_.get_defined_names(): return self.eval_statement(context, def_, name) elif def_.type == 'for_stmt': container_types = self.eval_element(context, def_.children[3]) for_types = iterable.py__iter__types(self, container_types, def_.children[3]) return finder.check_tuple_assignments(self, for_types, name) elif def_.type in ('import_from', 'import_name'): return imports.infer_import(context, name) return helpers.evaluate_call_of_leaf(context, name)
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
def goto_definitions(self, context, name): def_ = name.get_definition() is_simple_name = name.parent.type not in ('power', 'trailer') if is_simple_name: if name.parent.type == 'classdef' and name.parent.name == name: return [er.ClassContext(self, name.parent, context)] elif name.parent.type == 'funcdef': return [er.FunctionContext(self, context, name.parent)] elif name.parent.type == 'file_input': raise NotImplementedError if def_.type == 'expr_stmt' and name in def_.get_defined_names(): return self.eval_statement(context, def_, name) elif def_.type == 'for_stmt': container_types = self.eval_element(context, def_.children[3]) cn = ContextualizedNode(context, def_.children[3]) for_types = iterable.py__iter__types(self, container_types, cn) c_node = ContextualizedName(context, name) return finder.check_tuple_assignments(self, c_node, for_types) elif def_.type in ('import_from', 'import_name'): return imports.infer_import(context, name) return helpers.evaluate_call_of_leaf(context, name)
def goto_definitions(self, context, name): def_ = name.get_definition(import_name_always=True) if def_ is not None: type_ = def_.type if type_ == 'classdef': return [er.ClassContext(self, name.parent, context)] elif type_ == 'funcdef': return [er.FunctionContext(self, context, name.parent)] if type_ == 'expr_stmt': is_simple_name = name.parent.type not in ('power', 'trailer') if is_simple_name: return self.eval_statement(context, def_, name) if type_ == 'for_stmt': container_types = self.eval_element(context, def_.children[3]) cn = ContextualizedNode(context, def_.children[3]) for_types = iterable.py__iter__types(self, container_types, cn) c_node = ContextualizedName(context, name) return finder.check_tuple_assignments(self, c_node, for_types) if type_ in ('import_from', 'import_name'): return imports.infer_import(context, name) return helpers.evaluate_call_of_leaf(context, name)