コード例 #1
0
ファイル: transformtype.py プロジェクト: adamhaney/mypy
    def make_superclass_constructor_call(
            self, info: TypeInfo, callee_type: Callable) -> ExpressionStmt:
        """Construct a statement that calls the superclass constructor.

        In particular, it passes any type variables arguments as needed.
        """
        callee = SuperExpr('__init__')
        callee.info = info
        
        # We do not handle generic constructors. Either pass runtime
        # type variables from the current scope or perhaps require
        # explicit constructor in this case.
        
        selftype = self_type(info)    
        
        # FIX overloading
        # FIX default args / varargs
        
        # Map self type to the superclass context.
        base = info.mro[1]
        selftype = map_instance_to_supertype(selftype, base)
        
        super_init = cast(FuncDef, base.get_method('__init__'))
        
        # Add constructor arguments.
        args = [] # type: List[Node]
        for n in range(1, callee_type.min_args):            
            args.append(NameExpr(super_init.args[n].name()))
            self.tf.set_type(args[-1], callee_type.arg_types[n])

        # Store callee type after stripping away the 'self' type.
        self.tf.set_type(callee, nodes.method_callable(callee_type))
        
        call = CallExpr(callee, args, [nodes.ARG_POS] * len(args))
        return ExpressionStmt(call)
コード例 #2
0
def _add_attr_access_to_module(module: MypyFile, class_info: TypeInfo,
                               attr_name: str) -> None:
    """
    Adds a statement that accesses a given `attr_name` of a type (specified via `class_info`) as the last
    statement in a `module`.
    """

    module.defs.append(
        ExpressionStmt(
            MemberExpr(
                CastExpr(NameExpr('None'), Instance(class_info, [])),
                attr_name,
            )))
コード例 #3
0
ファイル: transformtype.py プロジェクト: adamhaney/mypy
    def make_generic_wrapper_init(self, info: TypeInfo) -> FuncDef:
        """Build constructor of a generic wrapper class."""
        nslots = num_slots(info)
        
        cdefs = [] # type: List[Node]
        
        # Build superclass constructor call.
        base = info.mro[1]
        if base.fullname() != 'builtins.object' and self.tf.is_java:
            s = SuperExpr('__init__')
            cargs = [NameExpr('__o')] # type: List[Node]
            for n in range(num_slots(base)):
                cargs.append(NameExpr(tvar_arg_name(n + 1)))
            for n in range(num_slots(base)):
                cargs.append(NameExpr(tvar_arg_name(n + 1, BOUND_VAR)))
            c = CallExpr(s, cargs, [nodes.ARG_POS] * len(cargs))
            cdefs.append(ExpressionStmt(c))
        
        # Create initialization of the wrapped object.
        cdefs.append(AssignmentStmt([MemberExpr(
                                         self_expr(),
                                         self.object_member_name(info),
                                         direct=True)],
                                    NameExpr('__o')))
        
        # Build constructor arguments.
        args = [Var('self'), Var('__o')]
        init = [None, None] # type: List[Node]
        
        for alt in [False, BOUND_VAR]:
            for n in range(nslots):
                args.append(Var(tvar_arg_name(n + 1, alt)))
                init.append(None)

        nargs = nslots * 2 + 2
        fdef = FuncDef('__init__',
                       args,
                       [nodes.ARG_POS] * nargs,
                       init,
                       Block(cdefs),
                       Callable( [AnyType()] * nargs,
                                [nodes.ARG_POS] * nargs, [None] * nargs,
                                Void(),
                                is_type_obj=False))
        fdef.info = info
        
        self.make_wrapper_slot_initializer(fdef)
        
        return fdef
コード例 #4
0
ファイル: transformfunc.py プロジェクト: prodigeni/mypy
    def call_wrapper(self, fdef: FuncDef, is_dynamic: bool,
                     is_wrapper_class: bool, target_ann: Callable,
                     cur_ann: Callable, target_suffix: str,
                     bound_sig: Callable) -> Node:
        """Return the body of wrapper method.

        The body contains only a call to the wrapped method and a
        return statement (if the call returns a value). Arguments are coerced
        to the target signature.
        """
        args = self.call_args(fdef.args,
                              target_ann,
                              cur_ann,
                              is_dynamic,
                              is_wrapper_class,
                              bound_sig,
                              ismethod=fdef.is_method())
        selfarg = args[0]
        args = args[1:]

        member = fdef.name() + target_suffix
        if not is_wrapper_class:
            callee = MemberExpr(selfarg, member)
        else:
            callee = MemberExpr(
                MemberExpr(self_expr(), self.tf.object_member_name()), member)

        call = CallExpr(callee, args, [nodes.ARG_POS] * len(args),
                        [None] * len(args))  # type: Node
        if bound_sig:
            call = self.tf.coerce(call,
                                  bound_sig.ret_type, target_ann.ret_type,
                                  self.tf.type_context(), is_wrapper_class)
            call = self.tf.coerce(call, cur_ann.ret_type, bound_sig.ret_type,
                                  self.tf.type_context(), is_wrapper_class)
        else:
            call = self.tf.coerce(call, cur_ann.ret_type, target_ann.ret_type,
                                  self.tf.type_context(), is_wrapper_class)
        if not isinstance(target_ann.ret_type, Void):
            return ReturnStmt(call)
        else:
            return ExpressionStmt(call)
コード例 #5
0
ファイル: fastparse.py プロジェクト: tony/mypy
 def visit_Expr(self, n: ast35.Expr) -> Node:
     value = self.visit(n.value)
     return ExpressionStmt(value)
コード例 #6
0
 def visit_Expr(self, n: ast27.Expr) -> ExpressionStmt:
     value = self.visit(n.value)
     stmt = ExpressionStmt(value)
     return self.set_line(stmt, n)
コード例 #7
0
ファイル: fastparse2.py プロジェクト: smalias/mypy
 def visit_Expr(self, n: ast27.Expr) -> ExpressionStmt:
     value = self.visit(n.value)
     return ExpressionStmt(value)
コード例 #8
0
 def visit_expression_stmt(self, node: ExpressionStmt) -> Node:
     return ExpressionStmt(self.node(node.expr))
コード例 #9
0
 def visit_expression_stmt(self, node: ExpressionStmt) -> ExpressionStmt:
     return ExpressionStmt(self.expr(node.expr))
コード例 #10
0
ファイル: fastparse.py プロジェクト: truepositiontruefix/mypy
 def visit_Expr(self, expr):
     value = self.visit(expr.value)
     return ExpressionStmt(value)
コード例 #11
0
 def visit_Expr(self, n: ast3.Expr) -> ExpressionStmt:
     value = self.visit(n.value)
     node = ExpressionStmt(value)
     return self.set_line(node, n)