def follow(self, is_goto=False): """ Returns the imported modules. """ if evaluate.follow_statement.push_stmt(self.import_stmt): # check recursion return [] if self.import_path: try: scope, rest = self._follow_file_system() except ModuleNotFound: debug.warning('Module not found: ' + str(self.import_stmt)) evaluate.follow_statement.pop_stmt() return [] scopes = [scope] scopes += remove_star_imports(scope) # follow the rest of the import (not FS -> classes, functions) if len(rest) > 1 or rest and self.is_like_search: scopes = [] if ['os', 'path'] == self.import_path[:2] \ and not self._is_relative_import(): # This is a huge exception, we follow a nested import # ``os.path``, because it's a very important one in Python # that is being achieved by messing with ``sys.modules`` in # ``os``. scopes = evaluate.follow_path(iter(rest), scope, scope) elif rest: if is_goto: scopes = itertools.chain.from_iterable( evaluate.find_name(s, rest[0], is_goto=True) for s in scopes) else: scopes = itertools.chain.from_iterable( evaluate.follow_path(iter(rest), s, s) for s in scopes) scopes = list(scopes) if self._is_nested_import(): scopes.append(self._get_nested_import(scope)) else: scopes = [ImportPath.GlobalNamespace] debug.dbg('after import', scopes) evaluate.follow_statement.pop_stmt() return scopes
def magic_function_scope(self): try: return self._magic_function_scope except AttributeError: # depth = 1 because this is not a module class Container(object): FunctionType = types.FunctionType source = _generate_code(Container, depth=0) parser = parsing.PyFuzzyParser(source, None) module = parser.module module.parent = self.scope typ = evaluate.follow_path(iter(['FunctionType']), module, module) s = self._magic_function_scope = typ.pop() return s
def follow(self, is_goto=False): """ Returns the imported modules. """ if evaluate.follow_statement.push_stmt(self.import_stmt): # check recursion return [] if self.import_path: try: scope, rest = self._follow_file_system() except ModuleNotFound: debug.warning('Module not found: ' + str(self.import_stmt)) evaluate.follow_statement.pop_stmt() return [] scopes = [scope] scopes += itertools.chain.from_iterable( remove_star_imports(s) for s in scopes) # follow the rest of the import (not FS -> classes, functions) if len(rest) > 1 or rest and self.is_like_search: scopes = [] elif rest: if is_goto: scopes = itertools.chain.from_iterable( evaluate.get_scopes_for_name(s, rest[0], is_goto=True) for s in scopes) else: scopes = itertools.chain.from_iterable( evaluate.follow_path(iter(rest), s, s) for s in scopes) scopes = list(scopes) if self.is_nested_import(): scopes.append(self.get_nested_import(scope)) else: scopes = [ImportPath.GlobalNamespace] debug.dbg('after import', scopes) evaluate.follow_statement.pop_stmt() return scopes
def get_return_types(self, evaluate_generator=False): """ Get the return types of a function. """ stmts = [] if self.base.parent == builtin.Builtin.scope \ and not isinstance(self.base, (Generator, Array)): func_name = str(self.base.name) # some implementations of builtins: if func_name == 'getattr': # follow the first param try: objects = self.follow_var_arg(0) names = self.follow_var_arg(1) except IndexError: debug.warning('getattr() called with to few args.') return [] for obj in objects: if not isinstance(obj, (Instance, Class, pr.Module)): debug.warning('getattr called without instance') continue for arr_name in names: if len(arr_name.var_args) != 1: debug.warning('jedi getattr is too simple') key = arr_name.var_args[0] stmts += evaluate.follow_path(iter([key]), obj, self.base) return stmts elif func_name == 'type': # otherwise it would be a metaclass if len(self.var_args) == 1: objects = self.follow_var_arg(0) return [o.base for o in objects if isinstance(o, Instance)] elif func_name == 'super': # TODO make this able to detect multiple inheritance supers accept = (pr.Function, ) func = self.var_args.get_parent_until(accept) if func.isinstance(*accept): cls = func.get_parent_until(accept + (pr.Class, ), include_current=False) if isinstance(cls, pr.Class): cls = Class(cls) su = cls.get_super_classes() if su: return [Instance(su[0])] return [] if self.base.isinstance(Class): # There maybe executions of executions. stmts = [Instance(self.base, self.var_args)] elif isinstance(self.base, Generator): return self.base.iter_content() else: # Don't do this with exceptions, as usual, because some deeper # exceptions could be catched - and I wouldn't know what happened. try: self.base.returns except (AttributeError, DecoratorNotFound): if hasattr(self.base, 'execute_subscope_by_name'): try: stmts = self.base.execute_subscope_by_name( '__call__', self.var_args) except KeyError: debug.warning("no __call__ func available", self.base) else: debug.warning("no execution possible", self.base) else: stmts = self._get_function_returns(evaluate_generator) debug.dbg('exec result: %s in %s' % (stmts, self)) return imports.strip_imports(stmts)
def get_return_types(self, evaluate_generator=False): """ Get the return types of a function. """ stmts = [] if self.base.parent == builtin.Builtin.scope \ and not isinstance(self.base, (Generator, Array)): func_name = str(self.base.name) # some implementations of builtins: if func_name == 'getattr': # follow the first param try: objects = self.follow_var_arg(0) names = self.follow_var_arg(1) except IndexError: debug.warning('getattr() called with to few args.') return [] for obj in objects: if not isinstance(obj, (Instance, Class, pr.Module)): debug.warning('getattr called without instance') continue for arr_name in names: if len(arr_name.var_args) != 1: debug.warning('jedi getattr is too simple') key = arr_name.var_args[0] stmts += evaluate.follow_path(iter([key]), obj, self.base) return stmts elif func_name == 'type': # otherwise it would be a metaclass if len(self.var_args) == 1: objects = self.follow_var_arg(0) return [o.base for o in objects if isinstance(o, Instance)] elif func_name == 'super': # TODO make this able to detect multiple inheritance supers accept = (pr.Function,) func = self.var_args.get_parent_until(accept) if func.isinstance(*accept): cls = func.get_parent_until(accept + (pr.Class,), include_current=False) if isinstance(cls, pr.Class): cls = Class(cls) su = cls.get_super_classes() if su: return [Instance(su[0])] return [] if self.base.isinstance(Class): # There maybe executions of executions. stmts = [Instance(self.base, self.var_args)] elif isinstance(self.base, Generator): return self.base.iter_content() else: # Don't do this with exceptions, as usual, because some deeper # exceptions could be catched - and I wouldn't know what happened. try: self.base.returns except (AttributeError, DecoratorNotFound): if hasattr(self.base, 'execute_subscope_by_name'): try: stmts = self.base.execute_subscope_by_name('__call__', self.var_args) except KeyError: debug.warning("no __call__ func available", self.base) else: debug.warning("no execution possible", self.base) else: stmts = self._get_function_returns(evaluate_generator) debug.dbg('exec result: %s in %s' % (stmts, self)) return imports.strip_imports(stmts)