Ejemplo n.º 1
0
    def diff(self) -> CallableType:
        """Finds a diff between two functions' arguments."""
        intermediate_names = [
            arg.name
            for arg in FuncArg.from_callable(self._intermediate)
        ]
        new_function_args = []

        for index, arg in enumerate(FuncArg.from_callable(self._original)):
            should_be_copied = (
                arg.kind in {ARG_STAR, ARG_STAR2} or
                arg.name not in intermediate_names or
                # We need to treat unnamed args differently, because python3.8
                # has pos_only_args, all their names are `None`.
                # This is also true for `lambda` functions where `.name`
                # might be missing for some reason.
                (
                    not arg.name and not (
                        index < len(intermediate_names) and
                        # If this is also unnamed arg, then ignoring it.
                        not intermediate_names[index]
                    )
                )
            )
            if should_be_copied:
                new_function_args.append(arg)
        return Intermediate(self._original).with_signature(
            new_function_args,
        )
Ejemplo n.º 2
0
    def from_callable_sequence(
        self,
        pipeline_types: List[MypyType],
        pipeline_kinds: List[int],
        ctx: CallableContext,
    ) -> MypyType:
        """Pass pipeline functions to infer them one by one."""
        parameter = FuncArg(None, self._instance, ARG_POS)
        ret_type = ctx.default_return_type

        for pipeline, kind in zip(pipeline_types, pipeline_kinds):
            ret_type = self._proper_type(
                analyze_call(
                    cast(FunctionLike, pipeline),
                    [parameter],
                    ctx,
                    show_errors=True,
                ), )
            parameter = FuncArg(None, ret_type, kind)
        return ret_type
Ejemplo n.º 3
0
    def _applied_positional_args(
        self,
        applied_args: List[FuncArg],
    ) -> List[FuncArg]:
        callee_args = list(filter(
            lambda name: name.name is None,  # TODO: maybe use `kind` instead?
            applied_args,
        ))

        new_function_args = []
        for ind, arg in enumerate(FuncArg.from_callable(self._case_function)):
            if arg.kind in self._positional_kinds and ind < len(callee_args):
                new_function_args.append(arg)
        return new_function_args
Ejemplo n.º 4
0
    def factory(ctx: FunctionContext) -> MypyType:
        if not (sym and sym.type and isinstance(sym.type, CallableType)):
            return ctx.default_return_type

        arg = ctx.api.expr_checker.accept(ctx.args[0][0])  # type: ignore
        tp = analtype.analyze_call(
            sym.type,
            [FuncArg(None, arg, ARG_POS)],
            ctx,
            show_errors=False,
        )

        if not (isinstance(arg, CallableType)
                and isinstance(tp, CallableType)):
            return ctx.default_return_type
        if not isinstance(tp.ret_type, CallableType):
            return ctx.default_return_type
        return _change_decorator_function_type(tp.ret_type, arg)
Ejemplo n.º 5
0
    def _applied_named_args(
        self,
        applied_args: List[FuncArg],
    ) -> List[FuncArg]:
        callee_args = list(
            filter(
                lambda name: name.name is not None,
                applied_args,
            ))

        new_function_args = []
        for arg in FuncArg.from_callable(self._case_function):
            has_named_arg_def = any(
                # Argument can either be used as a named argument
                # or passed to `**kwrgs` if it exists.
                arg.name == rdc.name or arg.kind == ARG_STAR2
                for rdc in callee_args)
            if callee_args and has_named_arg_def:
                new_function_args.append(arg)
        return new_function_args
Ejemplo n.º 6
0
    def __init__(self, original: CallableType, ctx: FunctionContext) -> None:
        """
        Saving the things we need.

        Args:
            original: original function that was passed to ``@curry``.
            ctx: function context.

        """
        self._original = original
        self._ctx = ctx
        self._overloads: List[CallableType] = []
        self._args = FuncArg.from_callable(self._original)

        # We need to get rid of generics here.
        # Because, otherwise `detach_callable` with add
        # unused variables to intermediate callables.
        self._default = cast(
            CallableType, self._ctx.default_return_type,
        ).copy_modified(
            ret_type=AnyType(TypeOfAny.implementation_artifact),
        )
Ejemplo n.º 7
0
 def _generate_applied_args(self, arg_parts) -> Iterator[FuncArg]:
     yield from (FuncArg(name, typ, kind) for name, typ, kind in arg_parts)