Пример #1
0
    def execute(self, obj, params=(), evaluate_generator=False):
        if obj.isinstance(er.Function):
            obj = obj.get_decorated_func()

        debug.dbg('execute: %s %s', obj, params)
        try:
            return stdlib.execute(self, obj, params)
        except stdlib.NotInStdLib:
            pass

        if isinstance(obj, iterable.GeneratorMethod):
            return obj.execute()
        elif obj.isinstance(compiled.CompiledObject):
            if obj.is_executable_class():
                return [er.Instance(self, obj, params)]
            else:
                return list(obj.execute_function(self, params))
        elif obj.isinstance(er.Class):
            # There maybe executions of executions.
            return [er.Instance(self, obj, params)]
        else:
            stmts = []
            if obj.isinstance(er.Function):
                stmts = er.FunctionExecution(self, obj, params).get_return_types(evaluate_generator)
            else:
                if hasattr(obj, 'execute_subscope_by_name'):
                    try:
                        stmts = obj.execute_subscope_by_name('__call__', params)
                    except KeyError:
                        debug.warning("no __call__ func available %s", obj)
                else:
                    debug.warning("no execution possible %s", obj)

            debug.dbg('execute result: %s in %s', stmts, obj)
            return imports.follow_imports(self, stmts)
Пример #2
0
def builtins_reversed(evaluator, sequences, obj):
    # Unpack the iterator values
    objects = tuple(iterable.get_iterator_types(sequences))
    rev = [iterable.AlreadyEvaluated([o]) for o in reversed(objects)]
    # Repack iterator values and then run it the normal way. This is
    # necessary, because `reversed` is a function and autocompletion
    # would fail in certain cases like `reversed(x).__iter__` if we
    # just returned the result directly.
    rev = iterable.AlreadyEvaluated(
        [iterable.FakeSequence(evaluator, rev, 'list')])
    return [er.Instance(evaluator, obj, param.Arguments(evaluator, [rev]))]
Пример #3
0
def _literals_to_types(evaluator, result):
    # Changes literals ('a', 1, 1.0, etc) to its type instances (str(),
    # int(), float(), etc).
    for i, r in enumerate(result):
        if is_literal(r):
            # Literals are only valid as long as the operations are
            # correct. Otherwise add a value-free instance.
            cls = builtin.get_by_name(r.name)
            from jedi.evaluate import representation as er
            result[i] = er.Instance(evaluator, cls)
    return list(set(result))
Пример #4
0
def builtins_reversed(evaluator, obj, params):
    objects = _follow_param(evaluator, params, 0)
    if objects:
        # unpack the iterator values
        objects = iterable.get_iterator_types(objects)
        rev = reversed(objects)
        # Repack iterator values and then run it the normal way. This is necessary,
        # because `reversed` is a function and autocompletion would fail in certain
        # cases like `reversed(x).__iter__` if we just returned the result
        # directly.
        stmts = [FakeStatement([r]) for r in rev]
        objects = (FakeArray(stmts, objects[0].parent), )
    return [er.Instance(evaluator, obj, objects)]
Пример #5
0
def builtins_reversed(evaluator, sequences, obj, arguments):
    # While we could do without this variable (just by using sequences), we
    # want static analysis to work well. Therefore we need to generated the
    # values again.
    first_arg = next(arguments.as_tuple())[0]
    ordered = list(iterable.py__iter__(evaluator, sequences, first_arg))

    rev = [iterable.AlreadyEvaluated(o) for o in reversed(ordered)]
    # Repack iterator values and then run it the normal way. This is
    # necessary, because `reversed` is a function and autocompletion
    # would fail in certain cases like `reversed(x).__iter__` if we
    # just returned the result directly.
    rev = iterable.AlreadyEvaluated(
        [iterable.FakeSequence(evaluator, rev, 'list')]
    )
    return set([er.Instance(evaluator, obj, param.Arguments(evaluator, [rev]))])
Пример #6
0
def _eval_param(evaluator, param, scope):
    res_new = set()
    func = param.get_parent_scope()

    cls = func.parent.get_parent_until((tree.Class, tree.Function))

    from jedi.evaluate.param import ExecutedParam, Arguments
    if isinstance(cls, tree.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(scope, er.InstanceElement):
            res_new.add(scope.instance)
        else:
            inst = er.Instance(evaluator,
                               evaluator.wrap(cls),
                               Arguments(evaluator, ()),
                               is_generated=True)
            res_new.add(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 pep0484 and docstring knowledge.
    pep0484_hints = pep0484.follow_param(evaluator, param)
    doc_params = docstrings.follow_param(evaluator, param)
    if pep0484_hints or doc_params:
        return list(set(pep0484_hints) | set(doc_params))

    if isinstance(param, ExecutedParam):
        return res_new | param.eval(evaluator)
    else:
        # 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 = list(evaluator.find_types(evaluator.BUILTINS, t))[0]
                res_new = evaluator.execute(typ)
        if param.default:
            res_new |= evaluator.eval_element(param.default)
        return res_new
Пример #7
0
def _eval_param(evaluator, param, scope):
    res_new = []
    func = param.parent

    cls = func.parent.get_parent_until((pr.Class, pr.Function))

    from jedi.evaluate.param import ExecutedParam, Arguments
    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(scope, er.InstanceElement):
            res_new.append(scope.instance)
        else:
            inst = er.Instance(evaluator, er.wrap(evaluator, cls),
                               Arguments(evaluator, ()), 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 isinstance(param, ExecutedParam):
        return res_new + param.eval(evaluator)
    else:
        # 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 param.default:
            res_new += evaluator.eval_element(param.default)
        return res_new