def _names_to_types(self, names, resolve_decorator): types = [] # Add isinstance and other if/assert knowledge. flow_scope = self.scope evaluator = self._evaluator while flow_scope: # TODO check if result is in scope -> no evaluation necessary n = check_flow_information(evaluator, flow_scope, self.name_str, self.position) if n: return n flow_scope = flow_scope.parent for name in names: typ = name.parent if typ.isinstance(pr.ForFlow): types += self._handle_for_loops(typ) elif isinstance(typ, pr.Param): types += self._eval_param(typ) elif typ.isinstance(pr.Statement): if typ.is_global(): # global keyword handling. types += evaluator.find_types(typ.parent.parent, str(name)) else: types += self._remove_statements(typ) else: if isinstance(typ, pr.Class): typ = er.Class(evaluator, typ) elif isinstance(typ, pr.Function): typ = er.Function(evaluator, typ) if typ.isinstance(er.Function) and resolve_decorator: typ = typ.get_decorated_func() types.append(typ) return types
def wrap(self, element): if isinstance(element, tree.Class): return er.Class(self, element) elif isinstance(element, tree.Function): if isinstance(element, tree.Lambda): return er.LambdaWrapper(self, element) else: return er.Function(self, element) elif isinstance(element, (tree.Module)) \ and not isinstance(element, er.ModuleWrapper): return er.ModuleWrapper(self, element) else: return element
def builtins_super(evaluator, obj, params): # TODO make this able to detect multiple inheritance super accept = (pr.Function, ) func = params.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 = er.Class(evaluator, cls) su = cls.get_super_classes() if su: return evaluator.execute(su[0]) return []
def builtins_super(evaluator, types, objects, scope): # TODO make this able to detect multiple inheritance super accept = (tree.Function, er.FunctionExecution) if scope.isinstance(*accept): wanted = (tree.Class, er.Instance) cls = scope.get_parent_until(accept + wanted, include_current=False) if isinstance(cls, wanted): if isinstance(cls, tree.Class): cls = er.Class(evaluator, cls) elif isinstance(cls, er.Instance): cls = cls.base su = cls.py__bases__(evaluator) if su: return evaluator.execute(su[0]) return []
def _follow_statements_imports(self): stripped = self._definition if isinstance(stripped, pr.Name): stripped = stripped.parent # We should probably work in `Finder._names_to_types` here. if isinstance(stripped, pr.Function): stripped = er.Function(self._evaluator, stripped) elif isinstance(stripped, pr.Class): stripped = er.Class(self._evaluator, stripped) if stripped.isinstance(pr.Statement): return self._evaluator.eval_statement(stripped) elif stripped.isinstance(pr.Import): return imports.strip_imports(self._evaluator, [stripped]) else: return [stripped]
def wrap(self, element): if isinstance(element, (er.Wrapper, er.InstanceElement, er.ModuleWrapper, er.FunctionExecution, er.Instance, compiled.CompiledObject)) or element is None: # TODO this is so ugly, please refactor. id:211 gh:212 return element if element.type == 'classdef': return er.Class(self, element) elif element.type == 'funcdef': return er.Function(self, element) elif element.type == 'lambda': return er.LambdaWrapper(self, element) elif element.type == 'file_input': return er.ModuleWrapper(self, element) else: return element
def _eval_param(self, param): evaluator = self._evaluator res_new = [] func = param.parent cls = func.parent.get_parent_until((pr.Class, pr.Function)) from jedi.evaluate.param import ExecutedParam if isinstance(cls, pr.Class) and param.position_nr == 0 \ and not isinstance(param, ExecutedParam): # This is where we add self - if it has never been # instantiated. if isinstance(self.scope, er.InstanceElement): res_new.append(self.scope.instance) else: for inst in evaluator.execute(er.Class(evaluator, cls)): inst.is_generated = True res_new.append(inst) return res_new # Instances are typically faked, if the instance is not called from # outside. Here we check it for __init__ functions and return. if isinstance(func, er.InstanceElement) \ and func.instance.is_generated and str(func.name) == '__init__': param = func.var.params[param.position_nr] # Add docstring knowledge. doc_params = docstrings.follow_param(evaluator, param) if doc_params: return doc_params if not param.is_generated: # Param owns no information itself. res_new += dynamic.search_params(evaluator, param) if not res_new: if param.stars: t = 'tuple' if param.stars == 1 else 'dict' typ = evaluator.find_types(compiled.builtin, t)[0] res_new = evaluator.execute(typ) if not param.assignment_details: # this means that there are no default params, # so just ignore it. return res_new return res_new + evaluator.eval_statement(param, seek_name=unicode(self.name_str))
def collections_namedtuple(evaluator, obj, arguments): """ Implementation of the namedtuple function. This has to be done by processing the namedtuple class template and evaluating the result. .. note:: |jedi| only supports namedtuples on Python >2.6. """ # Namedtuples are not supported on Python 2.6 if not hasattr(collections, '_class_template'): return set() # Process arguments # TODO here we only use one of the types, we should use all. name = list(_follow_param(evaluator, arguments, 0))[0].obj _fields = list(_follow_param(evaluator, arguments, 1))[0] if isinstance(_fields, compiled.CompiledObject): fields = _fields.obj.replace(',', ' ').split() elif isinstance(_fields, iterable.Array): try: fields = [v.obj for v in unite(_fields.py__iter__())] except AttributeError: return set() else: return set() # Build source source = collections._class_template.format( typename=name, field_names=fields, num_fields=len(fields), arg_list=', '.join(fields), repr_fmt=', '.join( collections._repr_template.format(name=name) for name in fields), field_defs='\n'.join( collections._field_template.format(index=index, name=name) for index, name in enumerate(fields))) # Parse source generated_class = ParserWithRecovery(evaluator.grammar, unicode(source)).module.subscopes[0] return set([er.Class(evaluator, generated_class)])