Example #1
0
    def unpack(self, func=None):
        named_args = []
        for stars, el in self._split():
            if stars == 1:
                arrays = self._evaluator.eval_element(el)
                iterators = [
                    _iterate_star_args(self._evaluator, a, el, func)
                    for a in arrays
                ]
                iterators = list(iterators)
                for values in list(zip_longest(*iterators)):
                    yield None, [v for v in values if v is not None]
            elif stars == 2:
                arrays = self._evaluator.eval_element(el)
                dicts = [
                    _star_star_dict(self._evaluator, a, el, func)
                    for a in arrays
                ]
                for dct in dicts:
                    for key, values in dct.items():
                        yield key, values
            else:
                if pr.is_node(el, 'argument'):
                    named_args.append(
                        (el.children[0].value, (el.children[2], )))
                elif isinstance(el, (list, tuple)):
                    yield None, el
                else:
                    yield None, (el, )

        # Reordering var_args is necessary, because star args sometimes appear
        # after named argument, but in the actual order it's prepended.
        for key_arg in named_args:
            yield key_arg
Example #2
0
 def iterate(self, contextualized_node=None, is_async=False):
     from jedi.evaluate.lazy_context import get_merged_lazy_context
     type_iters = [c.iterate(contextualized_node, is_async=is_async) for c in self._set]
     for lazy_contexts in zip_longest(*type_iters):
         yield get_merged_lazy_context(
             [l for l in lazy_contexts if l is not None]
         )
Example #3
0
    def unpack(self, funcdef=None):
        named_args = []
        for star_count, el in unpack_arglist(self.argument_node):
            if star_count == 1:
                arrays = self.context.eval_node(el)
                iterators = [_iterate_star_args(self.context, a, el, funcdef)
                             for a in arrays]
                for values in list(zip_longest(*iterators)):
                    # TODO zip_longest yields None, that means this would raise
                    # an exception?
                    yield None, get_merged_lazy_context(
                        [v for v in values if v is not None]
                    )
            elif star_count == 2:
                arrays = self.context.eval_node(el)
                for dct in arrays:
                    for key, values in _star_star_dict(self.context, dct, el, funcdef):
                        yield key, values
            else:
                if el.type == 'argument':
                    c = el.children
                    if len(c) == 3:  # Keyword argument.
                        named_args.append((c[0].value, LazyTreeContext(self.context, c[2]),))
                    else:  # Generator comprehension.
                        # Include the brackets with the parent.
                        comp = iterable.GeneratorComprehension(
                            self._evaluator, self.context, self.argument_node.parent)
                        yield None, LazyKnownContext(comp)
                else:
                    yield None, LazyTreeContext(self.context, el)

        # Reordering arguments is necessary, because star args sometimes appear
        # after named argument, but in the actual order it's prepended.
        for named_arg in named_args:
            yield named_arg
Example #4
0
    def unpack(self, funcdef=None):
        named_args = []
        for star_count, el in self._split():
            if star_count == 1:
                arrays = self.context.eval_node(el)
                iterators = [_iterate_star_args(self.context, a, el, funcdef)
                             for a in arrays]
                for values in list(zip_longest(*iterators)):
                    # TODO zip_longest yields None, that means this would raise
                    # an exception?
                    yield None, get_merged_lazy_context(
                        [v for v in values if v is not None]
                    )
            elif star_count == 2:
                arrays = self.context.eval_node(el)
                for dct in arrays:
                    for key, values in _star_star_dict(self.context, dct, el, funcdef):
                        yield key, values
            else:
                if el.type == 'argument':
                    c = el.children
                    if len(c) == 3:  # Keyword argument.
                        named_args.append((c[0].value, LazyTreeContext(self.context, c[2]),))
                    else:  # Generator comprehension.
                        # Include the brackets with the parent.
                        comp = iterable.GeneratorComprehension(
                            self._evaluator, self.context, self.argument_node.parent)
                        yield None, LazyKnownContext(comp)
                else:
                    yield None, LazyTreeContext(self.context, el)

        # Reordering var_args is necessary, because star args sometimes appear
        # after named argument, but in the actual order it's prepended.
        for named_arg in named_args:
            yield named_arg
 def iterate(self, contextualized_node=None, is_async=False):
     from jedi.evaluate.lazy_context import get_merged_lazy_context
     type_iters = [c.iterate(contextualized_node, is_async=is_async) for c in self._set]
     for lazy_contexts in zip_longest(*type_iters):
         yield get_merged_lazy_context(
             [l for l in lazy_contexts if l is not None]
         )
Example #6
0
def py__iter__(evaluator, types, node=None):
    debug.dbg('py__iter__')
    type_iters = []
    for typ in types:
        try:
            iter_method = typ.py__iter__
        except AttributeError:
            if node is not None:
                analysis.add(evaluator, 'type-error-not-iterable', node,
                             message="TypeError: '%s' object is not iterable" % typ)
        else:
            type_iters.append(iter_method())
            #for result in iter_method():
                #yield result

    for t in zip_longest(*type_iters, fillvalue=set()):
        yield unite(t)
Example #7
0
def py__iter__(evaluator, types, node=None):
    debug.dbg('py__iter__')
    type_iters = []
    for typ in types:
        try:
            iter_method = typ.py__iter__
        except AttributeError:
            if node is not None:
                # TODO this context is probably not right.
                analysis.add(typ, 'type-error-not-iterable', node,
                             message="TypeError: '%s' object is not iterable" % typ)
        else:
            type_iters.append(iter_method())

    for lazy_contexts in zip_longest(*type_iters):
        yield context.get_merged_lazy_context(
            [l for l in lazy_contexts if l is not None]
        )
Example #8
0
def py__iter__(evaluator, types, contextualized_node=None):
    debug.dbg('py__iter__')
    type_iters = []
    for typ in types:
        try:
            iter_method = typ.py__iter__
        except AttributeError:
            if contextualized_node is not None:
                analysis.add(contextualized_node.context,
                             'type-error-not-iterable',
                             contextualized_node._node,
                             message="TypeError: '%s' object is not iterable" %
                             typ)
        else:
            type_iters.append(iter_method())

    for lazy_contexts in zip_longest(*type_iters):
        yield context.get_merged_lazy_context(
            [l for l in lazy_contexts if l is not None])
Example #9
0
def py__iter__(evaluator, types, node=None):
    debug.dbg('py__iter__')
    type_iters = []
    for typ in types:
        try:
            iter_method = typ.py__iter__
        except AttributeError:
            if node is not None:
                analysis.add(evaluator,
                             'type-error-not-iterable',
                             node,
                             message="TypeError: '%s' object is not iterable" %
                             typ)
        else:
            type_iters.append(iter_method())
            #for result in iter_method():
            #yield result

    for t in zip_longest(*type_iters, fillvalue=set()):
        yield unite(t)
Example #10
0
    def unpack(self, func=None):
        named_args = []
        for stars, el in self._split():
            if stars == 1:
                arrays = self._evaluator.eval_element(el)
                iterators = [
                    _iterate_star_args(self._evaluator, a, el, func)
                    for a in arrays
                ]
                iterators = list(iterators)
                for values in list(zip_longest(*iterators)):
                    yield None, [v for v in values if v is not None]
            elif stars == 2:
                arrays = self._evaluator.eval_element(el)
                dicts = [
                    _star_star_dict(self._evaluator, a, el, func)
                    for a in arrays
                ]
                for dct in dicts:
                    for key, values in dct.items():
                        yield key, values
            else:
                if pr.is_node(el, 'argument'):
                    c = el.children
                    if len(c) == 3:  # Keyword argument.
                        named_args.append((c[0].value, (c[2], )))
                    else:  # Generator comprehension.
                        # Include the brackets with the parent.
                        comp = iterable.GeneratorComprehension(
                            self._evaluator, self.argument_node.parent)
                        yield None, (iterable.AlreadyEvaluated([comp]), )
                elif isinstance(el, (list, tuple)):
                    yield None, el
                else:
                    yield None, (el, )

        # Reordering var_args is necessary, because star args sometimes appear
        # after named argument, but in the actual order it's prepended.
        for key_arg in named_args:
            yield key_arg
Example #11
0
    def unpack(self, func=None):
        named_args = []
        for stars, el in self._split():
            if stars == 1:
                arrays = self._evaluator.eval_element(el)
                iterators = [_iterate_star_args(self._evaluator, a, el, func)
                             for a in arrays]
                iterators = list(iterators)
                for values in list(zip_longest(*iterators)):
                    yield None, [v for v in values if v is not None]
            elif stars == 2:
                arrays = self._evaluator.eval_element(el)
                dicts = [_star_star_dict(self._evaluator, a, el, func)
                         for a in arrays]
                for dct in dicts:
                    for key, values in dct.items():
                        yield key, values
            else:
                if tree.is_node(el, 'argument'):
                    c = el.children
                    if len(c) == 3:  # Keyword argument.
                        named_args.append((c[0].value, (c[2],)))
                    else:  # Generator comprehension.
                        # Include the brackets with the parent.
                        comp = iterable.GeneratorComprehension(
                            self._evaluator, self.argument_node.parent)
                        yield None, (iterable.AlreadyEvaluated([comp]),)
                elif isinstance(el, (list, tuple)):
                    yield None, el
                else:
                    yield None, (el,)

        # Reordering var_args is necessary, because star args sometimes appear
        # after named argument, but in the actual order it's prepended.
        for key_arg in named_args:
            yield key_arg
Example #12
0
def _unpack_var_args(evaluator, var_args, func):
    """
    Yields a key/value pair, the key is None, if its not a named arg.
    """
    argument_list = []
    from jedi.evaluate.representation import InstanceElement
    if isinstance(func, InstanceElement):
        # Include self at this place.
        argument_list.append((None, [helpers.FakeStatement([func.instance])]))

    # `var_args` is typically an Array, and not a list.
    for stmt in _reorder_var_args(var_args):
        if not isinstance(stmt, pr.Statement):
            if stmt is None:
                argument_list.append((None, []))
                # TODO generate warning?
                continue
            old = stmt
            # generate a statement if it's not already one.
            stmt = helpers.FakeStatement([old])

        expression_list = stmt.expression_list()
        if not len(expression_list):
            continue
        # *args
        if expression_list[0] == '*':
            arrays = evaluator.eval_expression_list(expression_list[1:])
            iterators = [_iterate_star_args(evaluator, a, expression_list[1:], func)
                         for a in arrays]
            for values in list(zip_longest(*iterators)):
                argument_list.append((None, [v for v in values if v is not None]))
        # **kwargs
        elif expression_list[0] == '**':
            dct = {}
            for array in evaluator.eval_expression_list(expression_list[1:]):
                # Merge multiple kwargs dictionaries, if used with dynamic
                # parameters.
                s = _star_star_dict(evaluator, array, expression_list[1:], func)
                for name, (key, value) in s.items():
                    try:
                        dct[name][1].add(value)
                    except KeyError:
                        dct[name] = key, set([value])

            for key, values in dct.values():
                # merge **kwargs/*args also for dynamic parameters
                for i, p in enumerate(func.params):
                    if str(p.get_name()) == str(key) and not p.stars:
                        try:
                            k, vs = argument_list[i]
                        except IndexError:
                            pass
                        else:
                            if k is None:  # k would imply a named argument
                                # Don't merge if they orginate at the same
                                # place. -> type-error-multiple-values
                                if [v.parent for v in values] != [v.parent for v in vs]:
                                    vs.extend(values)
                                    break
                else:
                    # default is to merge
                    argument_list.append((key, values))
        # Normal arguments (including key arguments).
        else:
            if stmt.assignment_details:
                key_arr, op = stmt.assignment_details[0]
                # Filter error tokens
                key_arr = [x for x in key_arr if isinstance(x, pr.Call)]
                # named parameter
                if key_arr and isinstance(key_arr[0], pr.Call):
                    argument_list.append((key_arr[0].name, [stmt]))
            else:
                argument_list.append((None, [stmt]))
    return argument_list
Example #13
0
def _unpack_var_args(evaluator, var_args, func):
    """
    Yields a key/value pair, the key is None, if its not a named arg.
    """
    argument_list = []
    from jedi.evaluate.representation import InstanceElement
    if isinstance(func, InstanceElement):
        # Include self at this place.
        argument_list.append((None, [helpers.FakeStatement([func.instance])]))

    # `var_args` is typically an Array, and not a list.
    for stmt in _reorder_var_args(var_args):
        if not isinstance(stmt, pr.Statement):
            if stmt is None:
                argument_list.append((None, []))
                # TODO generate warning?
                continue
            old = stmt
            # generate a statement if it's not already one.
            stmt = helpers.FakeStatement([old])

        expression_list = stmt.expression_list()
        if not len(expression_list):
            continue
        # *args
        if expression_list[0] == '*':
            arrays = evaluator.eval_expression_list(expression_list[1:])
            iterators = [_iterate_star_args(evaluator, a, expression_list[1:], func)
                         for a in arrays]
            for values in list(zip_longest(*iterators)):
                argument_list.append((None, [v for v in values if v is not None]))
        # **kwargs
        elif expression_list[0] == '**':
            dct = {}
            for array in evaluator.eval_expression_list(expression_list[1:]):
                # Merge multiple kwargs dictionaries, if used with dynamic
                # parameters.
                s = _star_star_dict(evaluator, array, expression_list[1:], func)
                for name, (key, value) in s.items():
                    try:
                        dct[name][1].add(value)
                    except KeyError:
                        dct[name] = key, set([value])

            for key, values in dct.values():
                # merge **kwargs/*args also for dynamic parameters
                for i, p in enumerate(func.params):
                    if str(p.get_name()) == str(key) and not p.stars:
                        try:
                            k, vs = argument_list[i]
                        except IndexError:
                            pass
                        else:
                            if k is None:  # k would imply a named argument
                                # Don't merge if they orginate at the same
                                # place. -> type-error-multiple-values
                                if [v.parent for v in values] != [v.parent for v in vs]:
                                    vs.extend(values)
                                    break
                else:
                    # default is to merge
                    argument_list.append((key, values))
        # Normal arguments (including key arguments).
        else:
            if stmt.assignment_details:
                key_arr, op = stmt.assignment_details[0]
                # Filter error tokens
                key_arr = [x for x in key_arr if isinstance(x, pr.Call)]
                # named parameter
                if key_arr and isinstance(key_arr[0], pr.Call):
                    argument_list.append((key_arr[0].name, [stmt]))
            else:
                argument_list.append((None, [stmt]))
    return argument_list