How to use the gast.Assign function in gast

To help you get started, we’ve selected a few gast examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github serge-sans-paille / pythran / pythran / transformations / remove_nested_functions.py View on Github external
if (isinstance(node.func, ast.Name) and
                        node.func.id == former_name):
                    node.func.id = new_name
                    node.args = (
                        [ast.Name(iin, ast.Load(), None, None)
                         for iin in sorted(ii)] +
                        node.args
                        )
                return node
        Renamer().visit(node)

        node.name = new_name
        self.global_declarations[node.name] = node
        proxy_call = ast.Name(new_name, ast.Load(), None, None)

        new_node = ast.Assign(
            [ast.Name(former_name, ast.Store(), None, None)],
            ast.Call(
                ast.Attribute(
                    ast.Name(mangle('functools'), ast.Load(), None, None),
                    "partial",
                    ast.Load()
                    ),
                [proxy_call] + binded_args,
                [],
                )
            )

        self.generic_visit(node)
        return new_node
github tensorflow / tensorflow / tensorflow / python / autograph / pyct / transformer.py View on Github external
if processing_expr_node:
      entry_expr_value = node.value

    if not anno.hasanno(node, anno.Basic.SKIP_PROCESSING):
      result = super(Base, self).visit(node)
    self.ctx.current_origin = parent_origin

    # Adjust for consistency: replacing the value of an Expr with
    # an Assign node removes the need for the Expr node.
    if processing_expr_node:
      if isinstance(result, gast.Expr) and result.value != entry_expr_value:
        # When the replacement is a list, it is assumed that the list came
        # from a template that contained a number of statements, which
        # themselves are standalone and don't require an enclosing Expr.
        if isinstance(result.value,
                      (list, tuple, gast.Assign, gast.AugAssign)):
          result = result.value

    # By default, all replacements receive the origin info of the replaced node.
    if result is not node and result is not None:
      nodes_to_adjust = result
      if isinstance(result, (list, tuple)):
        nodes_to_adjust = result
      else:
        nodes_to_adjust = (result,)
      for n in nodes_to_adjust:
        if not anno.hasanno(n, anno.Basic.ORIGIN):
          inherited_origin = anno.getanno(
              node, anno.Basic.ORIGIN, default=parent_origin)
          if inherited_origin is not None:
            anno.setanno(n, anno.Basic.ORIGIN, inherited_origin)
github serge-sans-paille / pythran / pythran / transformations / normalize_static_if.py View on Github external
ast.Name(return_n, ast.Store(), None),
                           ast.Name(cont_n, ast.Store(), None)]

            return [ast.Assign([ast.Tuple(fast_return, ast.Store())],
                               actual_call),
                    ast.If(cmpr,
                           [ast.Return(ast.Name(return_n, ast.Load(), None))],
                           cont_ass)]
        elif has_break or has_cont:
            cont_ass = self.make_control_flow_handlers(cont_n, status_n,
                                                       expected_return,
                                                       has_cont, has_break)

            fast_return = [ast.Name(status_n, ast.Store(), None),
                           ast.Name(cont_n, ast.Store(), None)]
            return [ast.Assign([ast.Tuple(fast_return, ast.Store())],
                               actual_call)] + cont_ass
        elif expected_return:
            return ast.Assign([ast.Tuple(expected_return, ast.Store())],
                              actual_call)
        else:
            return ast.Expr(actual_call)
github serge-sans-paille / pythran / pythran / transformations / normalize_static_if.py View on Github external
return [ast.Assign([ast.Tuple(fast_return, ast.Store())],
                               actual_call),
                    ast.If(cmpr,
                           [ast.Return(ast.Name(return_n, ast.Load(), None))],
                           cont_ass)]
        elif has_break or has_cont:
            cont_ass = self.make_control_flow_handlers(cont_n, status_n,
                                                       expected_return,
                                                       has_cont, has_break)

            fast_return = [ast.Name(status_n, ast.Store(), None),
                           ast.Name(cont_n, ast.Store(), None)]
            return [ast.Assign([ast.Tuple(fast_return, ast.Store())],
                               actual_call)] + cont_ass
        elif expected_return:
            return ast.Assign([ast.Tuple(expected_return, ast.Store())],
                              actual_call)
        else:
            return ast.Expr(actual_call)
github fluiddyn / transonic / transonic / analyses / blocks_if.py View on Github external
for user1 in user.users():
            if isinstance(user1.node, ast.Attribute):
                attribute = user1.node
                if attribute.attr == "is_transpiled":
                    parent = ancestors.parent(attribute)
                    if isinstance(parent, ast.If):
                        # it could be the begining of a block
                        if_node = parent
                        if len(parent.body) != 1:
                            # no it's not a block definition
                            continue
                        node = parent.body[0]
                        call = node.value
                        if isinstance(node, ast.Expr):
                            results = []
                        elif isinstance(node, ast.Assign):
                            results = [target.id for target in node.targets]
                        else:
                            # no it's not a block definition
                            continue

                        attribute = call.func
                        ts_node = attribute.value
                        if (
                            ts_node not in nodes_using_ts
                            or attribute.attr != "use_block"
                        ):
                            # no it's not a block definition
                            continue

                        try:
                            # gast >= 0.3.0 (py3.8)
github serge-sans-paille / pythran / pythran / optimizations / forward_substitution.py View on Github external
return node

        # multiple definition, which one should we forward?
        if sum(1 for d in self.locals if d.name() == dnode.id) > 1:
            return node

        # either a constant or a value
        fwd = (dnode in self.literals and
               isfinite(self.lazyness_analysis[dnode.id]))
        fwd |= self.lazyness_analysis[dnode.id] == 1

        if not fwd:
            return node

        parent = self.ancestors[dnode][-1]
        if isinstance(parent, ast.Assign):
            value = parent.value
            if dnode in self.literals:
                self.update = True
                if len(defuse.users()) == 1:
                    self.to_remove.add(parent)
                    return value
                else:
                    # FIXME: deepcopy here creates an unknown node
                    # for alias computations
                    return value
            elif len(parent.targets) == 1:
                self.update = True
                self.to_remove.add(parent)
                return value

        return node
github serge-sans-paille / pythran / pythran / types / tog.py View on Github external
del env[target.id]
                else:
                    raise PythranTypeError(
                        "Invalid del: unbound identifier `{}`".format(
                            target.id),
                        node)
            else:
                analyse(target, env, non_generic)
        return env
    elif isinstance(node, gast.Print):
        if node.dest is not None:
            analyse(node.dest, env, non_generic)
        for value in node.values:
            analyse(value, env, non_generic)
        return env
    elif isinstance(node, gast.Assign):
        defn_type = analyse(node.value, env, non_generic)
        for target in node.targets:
            target_type = analyse(target, env, non_generic)
            try:
                unify(target_type, defn_type)
            except InferenceError:
                raise PythranTypeError(
                    "Invalid assignment from type `{}` to type `{}`".format(
                        target_type,
                        defn_type),
                    node)
        return env
    elif isinstance(node, gast.AugAssign):
        # FIMXE: not optimal: evaluates type of node.value twice
        fake_target = deepcopy(node.target)
        fake_target.ctx = gast.Load()
github serge-sans-paille / pythran / pythran / optimizations / loop_full_unrolling.py View on Github external
def unroll(elt, body):
            return [ast.Assign([deepcopy(node.target)], elt)] + body

gast

Python AST that abstracts the underlying Python version

BSD-3-Clause
Latest version published 30 days ago

Package Health Score

82 / 100
Full package analysis

Similar packages