Example #1
0
    def visit_Assign(self, node):
        if self._disable:
            return node

        target = node.targets[0]
        if isinstance(target, ast.Tuple) and isinstance(node.value, ast.Tuple):
            names = [
                get_id(elt) for elt in target.elts
                if isinstance(elt, ast.Name)
            ]
            has_ignored = "_" in names
            if self._unpack and has_ignored:
                return self._visit_assign_unpack_all(node)
            if not has_ignored:
                return node

            body = [node]
            to_eval = []
            for i in range(len(target.elts)):
                if names[i] == "_":
                    del target.elts[i]
                    to_eval.append(node.value.elts[i])
                    del node.value.elts[i]
            # TODO: Evaluation order - we may have to split the tuple assignment to get
            # it right. For now, keep it simple
            body = [ast.Expr(value=e) for e in to_eval] + body
            return create_ast_block(body=body, at_node=node)
        return node
Example #2
0
 def _visit_assign_unpack_all(self, node):
     keep_ignored = self._language == "go"
     body = []
     target = node.targets[0]
     for i in range(len(target.elts)):
         elt = target.elts[i]
         if isinstance(elt, ast.Name):
             name = get_id(elt)
             if name == "_" and not keep_ignored:
                 body.append(ast.Expr(value=node.value.elts[i]))
                 body[-1].unused = True
                 continue
         body.append(
             ast.Assign(targets=[target.elts[i]], value=node.value.elts[i]))
     return create_ast_block(body=body, at_node=node)
Example #3
0
 def visit_With(self, node):
     self.generic_visit(node)
     stmts = []
     for i in node.items:
         if i.optional_vars:
             target = i.optional_vars
         else:
             target = ast.Name(id=self._get_temp(), lineno=node.lineno)
         stmt = ast.Assign(targets=[target],
                           value=i.context_expr,
                           lineno=node.lineno)
         stmts.append(stmt)
     node.body = stmts + node.body
     ret = create_ast_block(body=node.body, at_node=node)
     # Hint to UnpackScopeRewriter below to leave the new scope alone
     ret.unpack = False
     return ret
Example #4
0
    def visit_Assign(self, node):
        target = node.targets[0]  # Assumes all targets have same annotation
        if isinstance(target, ast.Subscript):
            return node
        annotation = getattr(target, "annotation", False)
        if not annotation:
            return node

        if isinstance(annotation, ast.ClassDef):
            annotation = ast.Name(id=get_id(annotation))

        col_offset = getattr(node, "col_offset", None)

        assigns = []
        for assign_target in node.targets:
            definition = node.scopes.parent_scopes.find(get_id(assign_target))
            if definition is None:
                definition = node.scopes.find(get_id(assign_target))
            if definition is not assign_target:
                previous_type = get_inferred_type(definition)
                if get_id(previous_type) == get_id(annotation):
                    if len(node.targets) == 1:
                        return node
                    else:
                        new_node = ast.Assign(
                            targets=[assign_target],
                            value=node.value,
                            lineno=node.lineno,
                            col_offset=col_offset,
                        )
                        assigns.append(new_node)
                        continue
            new_node = ast.AnnAssign(
                target=assign_target,
                value=node.value,
                lineno=node.lineno,
                col_offset=col_offset,
                simple=True,
                annotation=annotation,
            )
            assigns.append(new_node)

        if len(assigns) == 1:
            return assigns[0]

        return create_ast_block(body=assigns, at_node=node)
Example #5
0
    def visit_Compare(self, node):
        left = self.visit(node.left)
        ops = [self.visit(op) for op in node.ops]
        comparators = [self.visit(cmp) for cmp in node.comparators]
        right = comparators[0]
        if isinstance(right, ast.List):
            tmp = ast.Name(id=self._get_temp(), lineno=node.lineno)
            comparators[0] = tmp
            return create_ast_block(
                body=[
                    ast.Assign(targets=[tmp], value=right, lineno=node.lineno),
                    ast.Compare(left, ops, comparators, lineno=node.lineno),
                ],
                at_node=node,
            )

        return node
Example #6
0
 def visit_Assign(self, node):
     if self._disable:
         return node
     target = node.targets[0]
     if isinstance(target, ast.Tuple) and not (isinstance(
             target.elts[0], ast.Name)):
         temps = []
         orig = [None] * len(target.elts)
         body = [node]
         for i in range(len(target.elts)):
             temps.append(ast.Name(id=self._get_temp(), lineno=node.lineno))
             # The irony!
             target.elts[i], orig[i] = temps[i], target.elts[i]
             body.append(
                 ast.Assign(targets=[orig[i]],
                            value=temps[i],
                            lineno=node.lineno))
         return create_ast_block(body=body, at_node=node)
     return node
Example #7
0
    def visit_Call(self, node):
        fname = self.visit(node.func)

        if (get_id(fname) == "print" and len(node.args) == 1
                and not (isinstance(node.args[0], ast.Name)
                         or isinstance(node.args[0], ast.Constant))):
            tmp = ast.Name(id=self._get_temp(), lineno=node.lineno)
            ret = create_ast_block(
                body=[
                    ast.Assign(targets=[tmp],
                               value=node.args[0],
                               lineno=node.lineno),
                    node,
                ],
                at_node=node,
            )
            node.args[0] = tmp
            ret.body_vars = [tmp]
            return ret

        return node