def _check_isinstance_type(evaluator, element, search_name): try: assert element.type == 'power' # this might be removed if we analyze and, etc assert len(element.children) == 2 first, trailer = element.children assert isinstance(first, pr.Name) and first.value == 'isinstance' assert trailer.type == 'trailer' and trailer.children[0] == '(' assert len(trailer.children) == 3 # arglist stuff arglist = trailer.children[1] args = param.Arguments(evaluator, arglist, trailer) lst = list(args.unpack()) # Disallow keyword arguments assert len(lst) == 2 and lst[0][0] is None and lst[1][0] is None name = lst[0][1][0] # first argument, values, first value # Do a simple get_code comparison. They should just have the same code, # and everything will be all right. classes = lst[1][1][0] call = helpers.call_of_name(search_name) assert name.get_code() == call.get_code() except AssertionError: return [] result = [] for typ in evaluator.eval_element(classes): for typ in (typ.values() if isinstance(typ, iterable.Array) else [typ]): result += evaluator.execute(typ) return result
def goto(self, name): def resolve_implicit_imports(names): for name in names: if isinstance(name.parent, helpers.FakeImport): # Those are implicit imports. s = imports.ImportWrapper(self, name) for n in s.follow(is_goto=True): yield n yield name stmt = name.get_definition() par = name.parent if par.type == 'argument' and par.children[1] == '=' and par.children[ 0] == name: # Named param goto. trailer = par.parent if trailer.type == 'arglist': trailer = trailer.parent if trailer.type != 'classdef': for i, t in enumerate(trailer.parent.children): if t == trailer: to_evaluate = trailer.parent.children[:i] types = self.eval_element(to_evaluate[0]) for trailer in to_evaluate[1:]: types = self.eval_trailer(types, trailer) param_names = [] for typ in types: try: params = typ.params except AttributeError: pass else: param_names += [ param.name for param in params if param.name.value == name.value ] return param_names elif isinstance(par, pr.ExprStmt) and name in par.get_defined_names(): # Only take the parent, because if it's more complicated than just # a name it's something you can "goto" again. return [name] elif isinstance( par, (pr.Param, pr.Function, pr.Class)) and par.name is name: return [name] elif isinstance(stmt, pr.Import): return imports.ImportWrapper(self, name).follow(is_goto=True) scope = name.get_parent_scope() if pr.is_node(name.parent, 'trailer'): call = call_of_name(name, cut_own_trailer=True) types = self.eval_element(call) return resolve_implicit_imports( iterable.unite( self.find_types(typ, name, is_goto=True) for typ in types)) else: return self.find_types(scope, name, stmt.start_pos, search_global=True, is_goto=True)
def goto_definition(self, name): def_ = name.get_definition() if def_.type == 'expr_stmt' and name in def_.get_defined_names(): return self.eval_statement(def_, name) call = helpers.call_of_name(name) return self.eval_element(call)
def goto(self, name): def resolve_implicit_imports(names): for name in names: if isinstance(name.parent, helpers.FakeImport): # Those are implicit imports. s = imports.ImportWrapper(self, name) for n in s.follow(is_goto=True): yield n else: yield name stmt = name.get_definition() par = name.parent if par.type == 'argument' and par.children[1] == '=' and par.children[ 0] == name: # Named param goto. trailer = par.parent if trailer.type == 'arglist': trailer = trailer.parent if trailer.type != 'classdef': if trailer.type == 'decorator': types = self.eval_element(trailer.children[1]) else: i = trailer.parent.children.index(trailer) to_evaluate = trailer.parent.children[:i] types = self.eval_element(to_evaluate[0]) for trailer in to_evaluate[1:]: types = self.eval_trailer(types, trailer) param_names = [] for typ in types: try: params = typ.params except AttributeError: pass else: param_names += [ param.name for param in params if param.name.value == name.value ] return param_names elif isinstance(par, tree.ExprStmt) and name in par.get_defined_names(): # Only take the parent, because if it's more complicated than just # a name it's something you can "goto" again. return [name] elif isinstance( par, (tree.Param, tree.Function, tree.Class)) and par.name is name: return [name] elif isinstance(stmt, tree.Import): modules = imports.ImportWrapper(self, name).follow(is_goto=True) return list(resolve_implicit_imports(modules)) elif par.type == 'dotted_name': # Is a decorator. index = par.children.index(name) if index > 0: new_dotted = helpers.deep_ast_copy(par) new_dotted.children[index - 1:] = [] types = self.eval_element(new_dotted) return resolve_implicit_imports( iterable.unite( self.find_types(typ, name, is_goto=True) for typ in types)) scope = name.get_parent_scope() if tree.is_node(name.parent, 'trailer'): call = helpers.call_of_name(name, cut_own_trailer=True) types = self.eval_element(call) return resolve_implicit_imports( iterable.unite( self.find_types(typ, name, is_goto=True) for typ in types)) else: if stmt.type != 'expr_stmt': # We only need to adjust the start_pos for statements, because # there the name cannot be used. stmt = name return self.find_types(scope, name, stmt.start_pos, search_global=True, is_goto=True)
def _check_array_additions(evaluator, compare_array, module, is_list): """ Checks if a `Array` has "add" (append, insert, extend) statements: >>> a = [""] >>> a.append(1) """ if not settings.dynamic_array_additions or isinstance(module, compiled.CompiledObject): return [] def check_additions(arglist, add_name): params = list(param.Arguments(evaluator, arglist).unpack()) result = [] if add_name in ['insert']: params = params[1:] if add_name in ['append', 'add', 'insert']: for key, nodes in params: result += unite(evaluator.eval_element(node) for node in nodes) elif add_name in ['extend', 'update']: for key, nodes in params: iterators = unite(evaluator.eval_element(node) for node in nodes) result += get_iterator_types(iterators) return result from jedi.evaluate import representation as er, param def get_execution_parent(element): """ Used to get an Instance/FunctionExecution parent """ if isinstance(element, Array): node = element.atom else: # Is an Instance with an # Arguments([AlreadyEvaluated([ArrayInstance])]) inside # Yeah... I know... It's complicated ;-) node = list(element.var_args.argument_node[0])[0].var_args.trailer if isinstance(node, er.InstanceElement): return node return node.get_parent_until(er.FunctionExecution) temp_param_add, settings.dynamic_params_for_other_modules = \ settings.dynamic_params_for_other_modules, False search_names = ['append', 'extend', 'insert'] if is_list else ['add', 'update'] comp_arr_parent = get_execution_parent(compare_array) added_types = [] for add_name in search_names: try: possible_names = module.used_names[add_name] except KeyError: continue else: for name in possible_names: # Check if the original scope is an execution. If it is, one # can search for the same statement, that is in the module # dict. Executions are somewhat special in jedi, since they # literally copy the contents of a function. if isinstance(comp_arr_parent, er.FunctionExecution): if comp_arr_parent.start_pos < name.start_pos < comp_arr_parent.end_pos: name = comp_arr_parent.name_for_position(name.start_pos) else: # Don't check definitions that are not defined in the # same function. This is not "proper" anyway. It also # improves Jedi's speed for array lookups, since we # don't have to check the whole source tree anymore. continue trailer = name.parent power = trailer.parent trailer_pos = power.children.index(trailer) try: execution_trailer = power.children[trailer_pos + 1] except IndexError: continue else: if execution_trailer.type != 'trailer' \ or execution_trailer.children[0] != '(' \ or execution_trailer.children[1] == ')': continue power = helpers.call_of_name(name, cut_own_trailer=True) # InstanceElements are special, because they don't get copied, # but have this wrapper around them. if isinstance(comp_arr_parent, er.InstanceElement): power = er.get_instance_el(evaluator, comp_arr_parent.instance, power) if evaluator.recursion_detector.push_stmt(power): # Check for recursion. Possible by using 'extend' in # combination with function calls. continue if compare_array in evaluator.eval_element(power): # The arrays match. Now add the results added_types += check_additions(execution_trailer.children[1], add_name) evaluator.recursion_detector.pop_stmt() # reset settings settings.dynamic_params_for_other_modules = temp_param_add return added_types
def _check_array_additions(evaluator, compare_array, module, is_list): """ Checks if a `Array` has "add" (append, insert, extend) statements: >>> a = [""] >>> a.append(1) """ if not settings.dynamic_array_additions or isinstance( module, compiled.CompiledObject): return [] def check_additions(arglist, add_name): params = list(param.Arguments(evaluator, arglist).unpack()) result = [] if add_name in ['insert']: params = params[1:] if add_name in ['append', 'add', 'insert']: for key, nodes in params: result += unite(evaluator.eval_element(node) for node in nodes) elif add_name in ['extend', 'update']: for key, nodes in params: iterators = unite( evaluator.eval_element(node) for node in nodes) result += get_iterator_types(iterators) return result from jedi.evaluate import representation as er, param def get_execution_parent(element): """ Used to get an Instance/FunctionExecution parent """ if isinstance(element, Array): node = element.atom else: # Is an Instance with an # Arguments([AlreadyEvaluated([ArrayInstance])]) inside # Yeah... I know... It's complicated ;-) node = list(element.var_args.argument_node[0])[0].var_args.trailer if isinstance(node, er.InstanceElement): return node return node.get_parent_until(er.FunctionExecution) temp_param_add, settings.dynamic_params_for_other_modules = \ settings.dynamic_params_for_other_modules, False search_names = ['append', 'extend', 'insert' ] if is_list else ['add', 'update'] comp_arr_parent = get_execution_parent(compare_array) added_types = [] for add_name in search_names: try: possible_names = module.used_names[add_name] except KeyError: continue else: for name in possible_names: # Check if the original scope is an execution. If it is, one # can search for the same statement, that is in the module # dict. Executions are somewhat special in jedi, since they # literally copy the contents of a function. if isinstance(comp_arr_parent, er.FunctionExecution): if comp_arr_parent.start_pos < name.start_pos < comp_arr_parent.end_pos: name = comp_arr_parent.name_for_position( name.start_pos) else: # Don't check definitions that are not defined in the # same function. This is not "proper" anyway. It also # improves Jedi's speed for array lookups, since we # don't have to check the whole source tree anymore. continue trailer = name.parent power = trailer.parent trailer_pos = power.children.index(trailer) try: execution_trailer = power.children[trailer_pos + 1] except IndexError: continue else: if execution_trailer.type != 'trailer' \ or execution_trailer.children[0] != '(' \ or execution_trailer.children[1] == ')': continue power = helpers.call_of_name(name, cut_own_trailer=True) # InstanceElements are special, because they don't get copied, # but have this wrapper around them. if isinstance(comp_arr_parent, er.InstanceElement): power = er.get_instance_el(evaluator, comp_arr_parent.instance, power) if evaluator.recursion_detector.push_stmt(power): # Check for recursion. Possible by using 'extend' in # combination with function calls. continue if compare_array in evaluator.eval_element(power): # The arrays match. Now add the results added_types += check_additions( execution_trailer.children[1], add_name) evaluator.recursion_detector.pop_stmt() # reset settings settings.dynamic_params_for_other_modules = temp_param_add return added_types
def goto(self, name): def resolve_implicit_imports(names): for name in names: if isinstance(name.parent, helpers.FakeImport): # Those are implicit imports. s = imports.ImportWrapper(self, name) for n in s.follow(is_goto=True): yield n yield name stmt = name.get_definition() par = name.parent if par.type == 'argument' and par.children[1] == '=' and par.children[0] == name: # Named param goto. trailer = par.parent if trailer.type == 'arglist': trailer = trailer.parent if trailer.type != 'classdef': if trailer.type == 'decorator': types = self.eval_element(trailer.children[1]) else: i = trailer.parent.children.index(trailer) to_evaluate = trailer.parent.children[:i] types = self.eval_element(to_evaluate[0]) for trailer in to_evaluate[1:]: types = self.eval_trailer(types, trailer) param_names = [] for typ in types: try: params = typ.params except AttributeError: pass else: param_names += [param.name for param in params if param.name.value == name.value] return param_names elif isinstance(par, pr.ExprStmt) and name in par.get_defined_names(): # Only take the parent, because if it's more complicated than just # a name it's something you can "goto" again. return [name] elif isinstance(par, (pr.Param, pr.Function, pr.Class)) and par.name is name: return [name] elif isinstance(stmt, pr.Import): return imports.ImportWrapper(self, name).follow(is_goto=True) elif par.type == 'dotted_name': # Is a decorator. index = par.children.index(name) if index > 0: new_dotted = helpers.deep_ast_copy(par) new_dotted.children[index - 1:] = [] types = self.eval_element(new_dotted) return resolve_implicit_imports(iterable.unite( self.find_types(typ, name, is_goto=True) for typ in types )) scope = name.get_parent_scope() if pr.is_node(name.parent, 'trailer'): call = helpers.call_of_name(name, cut_own_trailer=True) types = self.eval_element(call) return resolve_implicit_imports(iterable.unite( self.find_types(typ, name, is_goto=True) for typ in types )) else: if stmt.type != 'expr_stmt': # We only need to adjust the start_pos for statements, because # there the name cannot be used. stmt = name return self.find_types(scope, name, stmt.start_pos, search_global=True, is_goto=True)