def py__iter__(self, contextualized_node=None): if self._is_homogenous(): yield LazyKnownContexts(self._get_getitem_contexts(0).execute_annotation()) else: if isinstance(self._index_context, SequenceLiteralContext): for i in range(self._index_context.py__len__()): yield LazyKnownContexts(self._get_getitem_contexts(i).execute_annotation())
def _imitate_items(self): items = ContextSet.from_iterable( FakeSequence( self.evaluator, 'tuple' (LazyKnownContexts(keys), LazyKnownContexts(values)) ) for keys, values in self._iterate() ) return create_evaluated_sequence_set(self.evaluator, items, sequence_type='list')
def _imitate_items(self): lazy_contexts = [ LazyKnownContext( FakeSequence( self.evaluator, u'tuple', [LazyKnownContexts(key), LazyKnownContexts(value)])) for key, value in self._iterate() ] return ContextSet(FakeSequence(self.evaluator, u'list', lazy_contexts))
def py__iter__(self, contextualized_node=None): """ While values returns the possible values for any array field, this function returns the value for a certain index. """ if self.array_type == u'dict': # Get keys. types = NO_CONTEXTS for k, _ in self.get_tree_entries(): types |= self._defining_context.eval_node(k) # We don't know which dict index comes first, therefore always # yield all the types. for _ in types: yield LazyKnownContexts(types) else: for node in self.get_tree_entries(): if node == ':' or node.type == 'subscript': # TODO this should probably use at least part of the code # of eval_subscript_list. yield LazyKnownContext( Slice(self._defining_context, None, None, None)) else: yield LazyTreeContext(self._defining_context, node) for addition in check_array_additions(self._defining_context, self): yield addition
def get_yield_lazy_contexts(self, is_async=False): # TODO: if is_async, wrap yield statements in Awaitable/async_generator_asend for_parents = [(y, tree.search_ancestor(y, 'for_stmt', 'funcdef', 'while_stmt', 'if_stmt')) for y in get_yield_exprs(self.evaluator, self.tree_node) ] # Calculate if the yields are placed within the same for loop. yields_order = [] last_for_stmt = None for yield_, for_stmt in for_parents: # For really simple for loops we can predict the order. Otherwise # we just ignore it. parent = for_stmt.parent if parent.type == 'suite': parent = parent.parent if for_stmt.type == 'for_stmt' and parent == self.tree_node \ and parser_utils.for_stmt_defines_one_name(for_stmt): # Simplicity for now. if for_stmt == last_for_stmt: yields_order[-1][1].append(yield_) else: yields_order.append((for_stmt, [yield_])) elif for_stmt == self.tree_node: yields_order.append((None, [yield_])) else: types = self.get_return_values(check_yields=True) if types: yield LazyKnownContexts(types) return last_for_stmt = for_stmt for for_stmt, yields in yields_order: if for_stmt is None: # No for_stmt, just normal yields. for yield_ in yields: for result in self._get_yield_lazy_context(yield_): yield result else: input_node = for_stmt.get_testlist() cn = ContextualizedNode(self, input_node) ordered = cn.infer().iterate(cn) ordered = list(ordered) for lazy_context in ordered: dct = { str(for_stmt.children[1].value): lazy_context.infer() } with helpers.predefine_names(self, for_stmt, dct): for yield_in_same_for_stmt in yields: for result in self._get_yield_lazy_context( yield_in_same_for_stmt): yield result
def py__bases__(self): args = self._get_bases_arguments() if args is not None: lst = [value for key, value in args.unpack() if key is None] if lst: return lst if self.py__name__() == 'object' \ and self.parent_context == self.evaluator.builtins_module: return [] return [LazyKnownContexts( self.evaluator.builtins_module.py__getattribute__('object') )]
def iterate(self, contextualized_node=None, is_async=False): debug.dbg('iterate %s', self) if is_async: from jedi.evaluate.lazy_context import LazyKnownContexts # TODO if no __aiter__ contexts are there, error should be: # TypeError: 'async for' requires an object with __aiter__ method, got int return iter([ LazyKnownContexts( self.py__getattribute__('__aiter__').execute_evaluated( ).py__getattribute__('__anext__').execute_evaluated( ).py__getattribute__('__await__').execute_evaluated(). py__stop_iteration_returns()) # noqa ]) return self.py__iter__(contextualized_node)
def py__bases__(self): arglist = self.tree_node.get_super_arglist() if arglist: from jedi.evaluate import arguments args = arguments.TreeArguments(self.evaluator, self.parent_context, arglist) lst = [value for key, value in args.unpack() if key is None] if lst: return lst if self.py__name__() == 'object' \ and self.parent_context == self.evaluator.builtins_module: return [] return [LazyKnownContexts( self.evaluator.builtins_module.py__getattribute__('object') )]
def _execute_array_values(evaluator, array): """ Tuples indicate that there's not just one return value, but the listed ones. `(str, int)` means that it returns a tuple with both types. """ from jedi.evaluate.context.iterable import SequenceLiteralContext, FakeSequence if isinstance(array, SequenceLiteralContext): values = [] for lazy_context in array.py__iter__(): objects = ContextSet.from_sets( _execute_array_values(evaluator, typ) for typ in lazy_context.infer()) values.append(LazyKnownContexts(objects)) return {FakeSequence(evaluator, array.array_type, values)} else: return array.execute_evaluated()
def py__call__(self, item_context_set): context_set = NO_CONTEXTS for args_context in self._args_context_set: lazy_contexts = list(args_context.py__iter__()) if len(lazy_contexts) == 1: # TODO we need to add the contextualized context. context_set |= item_context_set.get_item(lazy_contexts[0].infer(), None) else: context_set |= ContextSet([iterable.FakeSequence( self._wrapped_context.evaluator, 'list', [ LazyKnownContexts(item_context_set.get_item(lazy_context.infer(), None)) for lazy_context in lazy_contexts ], )]) return context_set
def iterate(): for generator in self.execute_function_slots(iter_slot_names): if generator.is_instance(): # `__next__` logic. if self.evaluator.environment.version_info.major == 2: name = u'next' else: name = u'__next__' next_slot_names = generator.get_function_slot_names(name) if next_slot_names: yield LazyKnownContexts( generator.execute_function_slots(next_slot_names) ) else: debug.warning('Instance has no __next__ function in %s.', generator) else: for lazy_context in generator.py__iter__(): yield lazy_context
def py__iter__(self): """ While values returns the possible values for any array field, this function returns the value for a certain index. """ if self.array_type == 'dict': # Get keys. types = ContextSet() for k, _ in self._items(): types |= self._defining_context.eval_node(k) # We don't know which dict index comes first, therefore always # yield all the types. for _ in types: yield LazyKnownContexts(types) else: for node in self._items(): yield LazyTreeContext(self._defining_context, node) for addition in check_array_additions(self._defining_context, self): yield addition
def py__iter__(self): iter_slot_names = self.get_function_slot_names(u'__iter__') if not iter_slot_names: debug.warning('No __iter__ on %s.' % self) return for generator in self.execute_function_slots(iter_slot_names): if isinstance(generator, AbstractInstanceContext): # `__next__` logic. if self.evaluator.environment.version_info.major == 2: name = u'next' else: name = u'__next__' iter_slot_names = generator.get_function_slot_names(name) if iter_slot_names: yield LazyKnownContexts( generator.execute_function_slots(iter_slot_names)) else: debug.warning('Instance has no __next__ function in %s.', generator) else: for lazy_context in generator.py__iter__(): yield lazy_context
def _values(self): return ContextSet( FakeSequence(self.evaluator, u'tuple', [LazyKnownContexts(self.dict_values())]))
def _imitate_values(self): lazy_context = LazyKnownContexts(self.dict_values()) return ContextSet(FakeSequence(self.evaluator, u'list', [lazy_context]))
def py__iter__(self): for keys, values in self._iterate(): yield LazyKnownContexts(keys)
def py__iter__(self): for set_ in self._iterate(): yield LazyKnownContexts(set_)
def unpack(self, funcdef=None): for values in self._values_list: yield None, LazyKnownContexts(values)
def py__iter__(self, contextualized_node=None): for set_ in self._iterate(): yield LazyKnownContexts(set_)
def py__iter__(self, contextualized_node=None): for keys, values in self._iterate(): yield LazyKnownContexts(keys)
def py__bases__(self): return [ LazyKnownContexts( self.evaluator.builtins_module.py__getattribute__('object')) ]