예제 #1
0
    def _infer_constraints(
        self,
        applied_args: List[FuncArg],
    ) -> _Constraints:
        """Creates mapping of ``typevar`` to real type that we already know."""
        checker = self._ctx.api.expr_checker  # type: ignore
        kinds = [arg.kind for arg in applied_args]
        exprs = [arg.expression(self._ctx.context) for arg in applied_args]

        formal_to_actual = map_actuals_to_formals(
            kinds,
            [arg.name for arg in applied_args],
            self._fallback.arg_kinds,
            self._fallback.arg_names,
            lambda index: checker.accept(exprs[index]),
        )
        constraints = infer_constraints_for_callable(
            self._fallback,
            [arg.type for arg in applied_args],
            kinds,
            formal_to_actual,
        )
        return {
            constraint.type_var: constraint.target
            for constraint in constraints
        }
예제 #2
0
 def assert_vararg_map(
     self,
     caller_kinds: List[int],
     callee_kinds: List[int],
     expected: List[List[int]],
     vararg_type: Type,
 ) -> None:
     result = map_actuals_to_formals(caller_kinds, [], callee_kinds, [],
                                     lambda i: vararg_type)
     assert_equal(result, expected)
예제 #3
0
 def assert_map(
     self,
     caller_kinds_: List[Union[int, str]],
     callee_kinds_: List[Union[int, Tuple[int, str]]],
     expected: List[List[int]],
 ) -> None:
     caller_kinds, caller_names = expand_caller_kinds(caller_kinds_)
     callee_kinds, callee_names = expand_callee_kinds(callee_kinds_)
     result = map_actuals_to_formals(
         caller_kinds, caller_names, callee_kinds, callee_names,
         lambda i: AnyType(TypeOfAny.special_form))
     assert_equal(result, expected)
예제 #4
0
 def assert_vararg_map(self,
                       caller_kinds: List[int],
                       callee_kinds: List[int],
                       expected: List[List[int]],
                       vararg_type: Type,
                       ) -> None:
     result = map_actuals_to_formals(
         caller_kinds,
         [],
         callee_kinds,
         [],
         lambda i: vararg_type)
     assert_equal(result, expected)
예제 #5
0
 def assert_map(self,
                caller_kinds_: List[Union[int, str]],
                callee_kinds_: List[Union[int, Tuple[int, str]]],
                expected: List[List[int]],
                ) -> None:
     caller_kinds, caller_names = expand_caller_kinds(caller_kinds_)
     callee_kinds, callee_names = expand_callee_kinds(callee_kinds_)
     result = map_actuals_to_formals(
         caller_kinds,
         caller_names,
         callee_kinds,
         callee_names,
         lambda i: AnyType(TypeOfAny.special_form))
     assert_equal(result, expected)
예제 #6
0
파일: mypy_plugin.py 프로젝트: suned/pfun
def _intersection_hook(context: FunctionSigContext):
    has_intersection = 'pfun.Intersection' in str(context.default_signature)
    is_generic = context.default_signature.is_generic()
    if not has_intersection or not is_generic:
        return context.default_signature
    formal_to_actual = map_actuals_to_formals(
        context.context.arg_kinds, context.context.arg_names,
        context.default_signature.arg_kinds,
        context.default_signature.arg_names,
        lambda i: context.api.expr_checker.accept(context.args[0][i]))
    callee = freshen_function_type_vars(context.default_signature)
    callee = (
        context.api.expr_checker.infer_function_type_arguments_using_context(
            callee, context.context))
    args = [arg for args in context.args for arg in args]
    callee = context.api.expr_checker.infer_function_type_arguments(
        callee, args, context.context.arg_kinds, formal_to_actual,
        context.context)
    intersection_translator = TranslateIntersection(context.api,
                                                    context.context,
                                                    inferred=True)
    translated = callee.accept(intersection_translator)
    return translated