Пример #1
0
def infer_param(execution_context, param):
    contexts = _infer_param(execution_context, param)
    evaluator = execution_context.evaluator
    if param.star_count == 1:
        tuple_ = builtin_from_name(evaluator, 'tuple')
        return ContextSet([GenericClass(
            tuple_,
            generics=(contexts,),
        ) for c in contexts])
    elif param.star_count == 2:
        dct = builtin_from_name(evaluator, 'dict')
        return ContextSet([GenericClass(
            dct,
            generics=(ContextSet([builtin_from_name(evaluator, 'str')]), contexts),
        ) for c in contexts])
        pass
    return contexts
Пример #2
0
    def infer(self):
        """
        Created to be used by inheritance.
        """
        evaluator = self.evaluator
        is_coroutine = self.tree_node.parent.type in ('async_stmt',
                                                      'async_funcdef')
        is_generator = bool(get_yield_exprs(evaluator, self.tree_node))
        from jedi.evaluate.gradual.typing import GenericClass

        if is_coroutine:
            if is_generator:
                if evaluator.environment.version_info < (3, 6):
                    return NO_CONTEXTS
                async_generator_classes = evaluator.typing_module \
                    .py__getattribute__('AsyncGenerator')

                yield_contexts = self.merge_yield_contexts(is_async=True)
                # The contravariant doesn't seem to be defined.
                generics = (yield_contexts.py__class__(), NO_CONTEXTS)
                return ContextSet(
                    # In Python 3.6 AsyncGenerator is still a class.
                    GenericClass(c, generics)
                    for c in async_generator_classes).execute_annotation()
            else:
                if evaluator.environment.version_info < (3, 5):
                    return NO_CONTEXTS
                async_classes = evaluator.typing_module.py__getattribute__(
                    'Coroutine')
                return_contexts = self.get_return_values()
                # Only the first generic is relevant.
                generics = (return_contexts.py__class__(), NO_CONTEXTS,
                            NO_CONTEXTS)
                return ContextSet(
                    GenericClass(c, generics)
                    for c in async_classes).execute_annotation()
        else:
            if is_generator:
                return ContextSet([iterable.Generator(evaluator, self)])
            else:
                return self.get_return_values()
Пример #3
0
    def define_generics(self, type_var_dict):
        from jedi.evaluate.gradual.typing import GenericClass

        def remap_type_vars():
            """
            The TypeVars in the resulting classes have sometimes different names
            and we need to check for that, e.g. a signature can be:

            def iter(iterable: Iterable[_T]) -> Iterator[_T]: ...

            However, the iterator is defined as Iterator[_T_co], which means it has
            a different type var name.
            """
            for type_var in self.list_type_vars():
                yield type_var_dict.get(type_var.py__name__(), NO_CONTEXTS)

        if type_var_dict:
            return ContextSet(
                [GenericClass(self, generics=tuple(remap_type_vars()))])
        return ContextSet({self})
Пример #4
0
 def _get_wrapped_context(self):
     from jedi.evaluate.gradual.typing import GenericClass
     klass = compiled.builtin_from_name(self.evaluator, self.array_type)
     c, = GenericClass(klass, self._get_generics()).execute_annotation()
     return c