Exemple #1
0
 def py__iter__(self, contextualized_node=None):
     if self._is_homogenous():
         yield LazyKnownValues(
             self._generics_manager.get_index_and_execute(0))
     else:
         for v in self._generics_manager.to_tuple():
             yield LazyKnownValues(v.execute_annotation())
Exemple #2
0
    def get_yield_lazy_values(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.inference_state, 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 LazyKnownValues(types, min=0, max=float('inf'))
                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_value(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_value in ordered:
                    dct = {str(for_stmt.children[1].value): lazy_value.infer()}
                    with self.predefine_names(for_stmt, dct):
                        for yield_in_same_for_stmt in yields:
                            for result in self._get_yield_lazy_value(
                                    yield_in_same_for_stmt):
                                yield result
Exemple #3
0
 def iterate(self, contextualized_node=None, is_async=False):
     debug.dbg('iterate %s', self)
     if is_async:
         from medi.inference.lazy_value import LazyKnownValues
         # TODO if no __aiter__ values are there, error should be:
         # TypeError: 'async for' requires an object with __aiter__ method, got int
         return iter([
             LazyKnownValues(
                 self.py__getattribute__('__aiter__').execute_with_values(
                 ).py__getattribute__('__anext__').execute_with_values(
                 ).py__getattribute__('__await__').execute_with_values().
                 py__stop_iteration_returns())  # noqa
         ])
     return self.py__iter__(contextualized_node)
Exemple #4
0
    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.is_builtins_module():
            return []
        return [
            LazyKnownValues(
                self.inference_state.builtins_module.py__getattribute__(
                    'object'))
        ]
Exemple #5
0
def _execute_array_values(inference_state, 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 medi.inference.value.iterable import SequenceLiteralValue, FakeTuple, FakeList
    if isinstance(array, SequenceLiteralValue) and array.array_type in ('tuple', 'list'):
        values = []
        for lazy_value in array.py__iter__():
            objects = ValueSet.from_sets(
                _execute_array_values(inference_state, typ)
                for typ in lazy_value.infer()
            )
            values.append(LazyKnownValues(objects))
        cls = FakeTuple if array.array_type == 'tuple' else FakeList
        return {cls(inference_state, values)}
    else:
        return array.execute_annotation()
Exemple #6
0
 def iterate():
     for generator in self.execute_function_slots(iter_slot_names):
         if generator.is_instance() and not generator.is_compiled():
             # `__next__` logic.
             if self.inference_state.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 LazyKnownValues(
                     generator.execute_function_slots(next_slot_names))
             else:
                 debug.warning(
                     'Instance has no __next__ function in %s.',
                     generator)
         else:
             for lazy_value in generator.py__iter__():
                 yield lazy_value
Exemple #7
0
 def py__bases__(self):
     return [
         LazyKnownValues(
             self.inference_state.builtins_module.py__getattribute__(
                 'object'))
     ]
Exemple #8
0
 def py__iter__(self, contextualized_node=None):
     for lazy_context in self._wrapped_value.py__iter__(
             contextualized_node):
         yield lazy_context
     yield LazyKnownValues(self._assigned_values)
Exemple #9
0
 def unpack(self, funcdef=None):
     for values in self._values_list:
         yield None, LazyKnownValues(values)