Example #1
0
 def analyze_callable_type(self, t: UnboundType) -> Type:
     fallback = self.named_type('builtins.function')
     if len(t.args) == 0:
         # Callable (bare). Treat as Callable[..., Any].
         any_type = AnyType(from_omitted_generics=True,
                            line=t.line,
                            column=t.column)
         ret = CallableType([any_type, any_type],
                            [nodes.ARG_STAR, nodes.ARG_STAR2], [None, None],
                            ret_type=any_type,
                            fallback=fallback,
                            is_ellipsis_args=True)
     elif len(t.args) == 2:
         ret_type = t.args[1]
         if isinstance(t.args[0], TypeList):
             # Callable[[ARG, ...], RET] (ordinary callable type)
             analyzed_args = self.analyze_callable_args(t.args[0])
             if analyzed_args is None:
                 return AnyType()
             args, kinds, names = analyzed_args
             ret = CallableType(args,
                                kinds,
                                names,
                                ret_type=ret_type,
                                fallback=fallback)
         elif isinstance(t.args[0], EllipsisType):
             # Callable[..., RET] (with literal ellipsis; accept arbitrary arguments)
             ret = CallableType([AnyType(), AnyType()],
                                [nodes.ARG_STAR, nodes.ARG_STAR2],
                                [None, None],
                                ret_type=ret_type,
                                fallback=fallback,
                                is_ellipsis_args=True)
         else:
             self.fail(
                 'The first argument to Callable must be a list of types or "..."',
                 t)
             return AnyType()
     else:
         self.fail('Invalid function type', t)
         return AnyType()
     assert isinstance(ret, CallableType)
     return ret.accept(self)
Example #2
0
 def analyze_callable_type(self, t: UnboundType) -> Type:
     fallback = self.named_type('builtins.function')
     if len(t.args) == 0:
         # Callable (bare). Treat as Callable[..., Any].
         any_type = AnyType(TypeOfAny.from_omitted_generics,
                            line=t.line, column=t.column)
         ret = CallableType([any_type, any_type],
                            [nodes.ARG_STAR, nodes.ARG_STAR2],
                            [None, None],
                            ret_type=any_type,
                            fallback=fallback,
                            is_ellipsis_args=True)
     elif len(t.args) == 2:
         ret_type = t.args[1]
         if isinstance(t.args[0], TypeList):
             # Callable[[ARG, ...], RET] (ordinary callable type)
             analyzed_args = self.analyze_callable_args(t.args[0])
             if analyzed_args is None:
                 return AnyType(TypeOfAny.from_error)
             args, kinds, names = analyzed_args
             ret = CallableType(args,
                                kinds,
                                names,
                                ret_type=ret_type,
                                fallback=fallback)
         elif isinstance(t.args[0], EllipsisType):
             # Callable[..., RET] (with literal ellipsis; accept arbitrary arguments)
             ret = CallableType([AnyType(TypeOfAny.explicit),
                                 AnyType(TypeOfAny.explicit)],
                                [nodes.ARG_STAR, nodes.ARG_STAR2],
                                [None, None],
                                ret_type=ret_type,
                                fallback=fallback,
                                is_ellipsis_args=True)
         else:
             self.fail('The first argument to Callable must be a list of types or "..."', t)
             return AnyType(TypeOfAny.from_error)
     else:
         self.fail('Please use "Callable[[<parameters>], <return type>]" or "Callable"', t)
         return AnyType(TypeOfAny.from_error)
     assert isinstance(ret, CallableType)
     return ret.accept(self)
Example #3
0
 def analyze_callable_type(self, t: UnboundType) -> Type:
     fallback = self.builtin_type('builtins.function')
     if len(t.args) == 0:
         # Callable (bare). Treat as Callable[..., Any].
         ret = CallableType([AnyType(), AnyType()],
                            [nodes.ARG_STAR, nodes.ARG_STAR2], [None, None],
                            ret_type=AnyType(),
                            fallback=fallback,
                            is_ellipsis_args=True)
     elif len(t.args) == 2:
         ret_type = t.args[1]
         if isinstance(t.args[0], TypeList):
             # Callable[[ARG, ...], RET] (ordinary callable type)
             args = t.args[0].items
             ret = CallableType(args, [nodes.ARG_POS] * len(args),
                                [None] * len(args),
                                ret_type=ret_type,
                                fallback=fallback)
         elif isinstance(t.args[0], EllipsisType):
             # Callable[..., RET] (with literal ellipsis; accept arbitrary arguments)
             ret = CallableType([AnyType(), AnyType()],
                                [nodes.ARG_STAR, nodes.ARG_STAR2],
                                [None, None],
                                ret_type=ret_type,
                                fallback=fallback,
                                is_ellipsis_args=True)
         else:
             self.fail(
                 'The first argument to Callable must be a list of types or "..."',
                 t)
             return AnyType()
     else:
         self.fail('Invalid function type', t)
         return AnyType()
     assert isinstance(ret, CallableType)
     return ret.accept(self)
Example #4
0
def convert_class_tvars_to_func_tvars(callable: CallableType,
                                      num_func_tvars: int) -> CallableType:
    return cast(CallableType, callable.accept(TvarTranslator(num_func_tvars)))
Example #5
0
    def analyze_callable_type(self, t: UnboundType) -> Type:
        fallback = self.builtin_type('builtins.function')
        if len(t.args) == 0:
            # Callable (bare). Treat as Callable[..., Any].
            ret = CallableType([AnyType(), AnyType()],
                               [nodes.ARG_STAR, nodes.ARG_STAR2], [None, None],
                               ret_type=AnyType(),
                               fallback=fallback,
                               is_ellipsis_args=True)
        elif len(t.args) == 2:
            ret_type = t.args[1]
            if isinstance(t.args[0], TypeList):
                # Callable[[ARG, ...], RET] (ordinary callable type)
                args = []  # type: List[Type]
                names = []  # type: List[str]
                kinds = []  # type: List[int]
                for arg in t.args[0].items:
                    if isinstance(arg, CallableArgument):
                        args.append(arg.typ)
                        names.append(arg.name)
                        if arg.constructor is None:
                            return AnyType()
                        found = self.lookup(arg.constructor, arg)
                        if found is None:
                            # Looking it up already put an error message in
                            return AnyType()
                        elif found.fullname not in ARG_KINDS_BY_CONSTRUCTOR:
                            self.fail(
                                'Invalid argument constructor "{}"'.format(
                                    found.fullname), arg)
                            return AnyType()
                        else:
                            kind = ARG_KINDS_BY_CONSTRUCTOR[found.fullname]
                            kinds.append(kind)
                            if arg.name is not None and kind in {
                                    ARG_STAR, ARG_STAR2
                            }:
                                self.fail(
                                    "{} arguments should not have names".
                                    format(arg.constructor), arg)
                                return AnyType()
                    else:
                        args.append(arg)
                        names.append(None)
                        kinds.append(ARG_POS)

                check_arg_names(names, [t] * len(args), self.fail, "Callable")
                check_arg_kinds(kinds, [t] * len(args), self.fail)
                ret = CallableType(args,
                                   kinds,
                                   names,
                                   ret_type=ret_type,
                                   fallback=fallback)
            elif isinstance(t.args[0], EllipsisType):
                # Callable[..., RET] (with literal ellipsis; accept arbitrary arguments)
                ret = CallableType([AnyType(), AnyType()],
                                   [nodes.ARG_STAR, nodes.ARG_STAR2],
                                   [None, None],
                                   ret_type=ret_type,
                                   fallback=fallback,
                                   is_ellipsis_args=True)
            else:
                self.fail(
                    'The first argument to Callable must be a list of types or "..."',
                    t)
                return AnyType()
        else:
            self.fail('Invalid function type', t)
            return AnyType()
        assert isinstance(ret, CallableType)
        return ret.accept(self)
Example #6
0
def convert_class_tvars_to_func_tvars(callable: CallableType,
                                      num_func_tvars: int) -> CallableType:
    return cast(CallableType, callable.accept(TvarTranslator(num_func_tvars)))