示例#1
0
    def \
                     add_constructor_tvar_args(
self, fdef, typ, args, arg_kinds, init):
        """Add type variable arguments for __init__ of a generic type.

        Return tuple (new args, new kinds, new inits).
        """
        tv = []
        ntvars = len(fdef.info.type_vars)
        for n in range(ntvars):
            tv.append(Var(tvar_arg_name(n + 1)))
            typ = add_arg_type_after_self(typ, Any())
        args = [args[0]] + tv + args[1:]
        arg_kinds = [arg_kinds[0]] + [nodes.ARG_POS] * ntvars + arg_kinds[1:]
        init = [None] * ntvars + init
        return (args, arg_kinds, init, typ)
示例#2
0
    def add_constructor_tvar_args(
            self, fdef: FuncDef, typ: Type,
            args: List[Var], arg_kinds: List[int], 
            init: List[Node]) -> Tuple[List[Var], List[int], List[Node], Type]:
        """Add type variable arguments for __init__ of a generic type.

        Return tuple (new args, new kinds, new inits).
        """
        tv = [] # type: List[Var]
        ntvars = len(fdef.info.type_vars)
        for n in range(ntvars):
            tv.append(Var(tvar_arg_name(n + 1)))
            typ = add_arg_type_after_self(cast(Callable, typ), AnyType())
        args = [args[0]] + tv + args[1:]
        arg_kinds = [arg_kinds[0]] + [nodes.ARG_POS] * ntvars + arg_kinds[1:]
        init = List[Node]([None]) * ntvars + init
        return (args, arg_kinds, init, typ)
示例#3
0
文件: transform.py 项目: ashleyh/mypy
    def prepend_generic_function_tvar_args(self, fdef: FuncDef) -> None:
        """Add implicit function type variable arguments if fdef is generic."""
        sig = cast(Callable, function_type(fdef))
        tvars = sig.variables
        if not fdef.type:
            fdef.type = sig

        tv = []  # type: List[Var]
        ntvars = len(tvars)
        if fdef.is_method():
            # For methods, add type variable arguments after the self arg.
            for n in range(ntvars):
                tv.append(Var(tvar_arg_name(-1 - n)))
                fdef.type = add_arg_type_after_self(cast(Callable, fdef.type), AnyType())
            fdef.args = [fdef.args[0]] + tv + fdef.args[1:]
        else:
            # For ordinary functions, prepend type variable arguments.
            for n in range(ntvars):
                tv.append(Var(tvar_arg_name(-1 - n)))
                fdef.type = prepend_arg_type(cast(Callable, fdef.type), AnyType())
            fdef.args = tv + fdef.args
        fdef.init = List[AssignmentStmt]([None]) * ntvars + fdef.init
示例#4
0
 def prepend_generic_function_tvar_args(self, fdef):
     """Add implicit function type variable arguments if fdef is generic."""
     sig = function_type(fdef)
     tvars = sig.variables.items
     if not fdef.type:
         fdef.type = sig
     
     tv = []
     ntvars = len(tvars)
     if fdef.is_method():
         # For methods, add type variable arguments after the self arg.
         for n in range(ntvars):
             tv.append(Var(tvar_arg_name(-1 - n)))
             fdef.type = add_arg_type_after_self(fdef.type, Any())
         fdef.args = [fdef.args[0]] + tv + fdef.args[1:]
     else:
         # For ordinary functions, prepend type variable arguments.
         for n in range(ntvars):
             tv.append(Var(tvar_arg_name(-1 - n)))
             fdef.type = prepend_arg_type(fdef.type, Any())
         fdef.args = tv + fdef.args
     fdef.init = [None] * ntvars + fdef.init
示例#5
0
    def prepend_generic_function_tvar_args(self, fdef: FuncDef) -> None:
        """Add implicit function type variable arguments if fdef is generic."""
        sig = cast(Callable, function_type(fdef))
        tvars = sig.variables
        if not fdef.type:
            fdef.type = sig

        tv = []  # type: List[Var]
        ntvars = len(tvars)
        if fdef.is_method():
            # For methods, add type variable arguments after the self arg.
            for n in range(ntvars):
                tv.append(Var(tvar_arg_name(-1 - n)))
                fdef.type = add_arg_type_after_self(cast(Callable, fdef.type),
                                                    AnyType())
            fdef.args = [fdef.args[0]] + tv + fdef.args[1:]
        else:
            # For ordinary functions, prepend type variable arguments.
            for n in range(ntvars):
                tv.append(Var(tvar_arg_name(-1 - n)))
                fdef.type = prepend_arg_type(cast(Callable, fdef.type),
                                             AnyType())
            fdef.args = tv + fdef.args
        fdef.init = List[AssignmentStmt]([None]) * ntvars + fdef.init
示例#6
0
        return fdef2
    
    tuple<Var[], int[], Node[], Type> \
                     add_constructor_tvar_args(
                             self, FuncDef fdef, Type typ,
                             Var[] args, int[] arg_kinds, 
                             Node[] init):
        """Add type variable arguments for __init__ of a generic type.

        Return tuple (new args, new kinds, new inits).
        """
        Var[] tv = []
        ntvars = len(fdef.info.type_vars)
        for n in range(ntvars):
            tv.append(Var(tvar_arg_name(n + 1)))
            typ = add_arg_type_after_self((Callable)typ, Any())
        args = [args[0]] + tv + args[1:]
        arg_kinds = [arg_kinds[0]] + [nodes.ARG_POS] * ntvars + arg_kinds[1:]
        init = <Node> [None] * ntvars + init
        return (args, arg_kinds, init, typ)
    
    FuncDef override_method_wrapper(self, FuncDef fdef):
        """Construct a method wrapper for an overridden method."""
        orig_fdef = fdef.info.base.get_method(fdef.name())
        return self.method_wrapper((FuncDef)orig_fdef, fdef, False, False)
    
    FuncDef dynamic_method_wrapper(self, FuncDef fdef):
        """Construct a dynamically typed method wrapper."""
        return self.method_wrapper(fdef, fdef, True, False)
    
    Node[] generic_method_wrappers(self, FuncDef fdef):