def _clean_function_node(func: ast.FunctionDef): func.decorator_list = [] func.returns = None func.args.defaults = [] func.args = RemoveAnnotations().visit(func.args) ast.fix_missing_locations(func.args) return func
def visit_FunctionDef(self, node: ast.FunctionDef) -> Any: if is_name_to_change(node.name): if node.name.startswith('sample'): node.returns = ast.parse('Generator[Union[GExpr,Sample],Sample,None]').body[0].value elif node.name.startswith('evaluate_denotation'): node.returns = ast.parse('Generator[Union[GExpr,Any],Any,None]').body[0].value node.name = 'coro_' + node.name self.generic_visit(node) kw = meta(node)
def visit_FunctionDef(self, function: ast.FunctionDef): """ Visitor of the function node Includes the method in the scope of its module :param function: """ fun_args = self.visit(function.args) fun_rtype_symbol = self.visit(function.returns) if function.returns is not None else Type.none if fun_rtype_symbol is None: # it is a function with None return: Main(a: int) -> None: raise NotImplementedError if isinstance(fun_rtype_symbol, str): symbol = self.get_symbol(function.returns.id) fun_rtype_symbol = self.get_type(symbol) fun_return: IType = self.get_type(fun_rtype_symbol) fun_decorators: List[Method] = self._get_function_decorators(function) if Builtin.Metadata in fun_decorators: self._read_metadata_object(function) return Builtin.Metadata method = Method(args=fun_args, defaults=function.args.defaults, return_type=fun_return, origin_node=function, is_public=Builtin.Public in fun_decorators) self._current_method = method self._scope_stack.append(SymbolScope()) # don't evaluate constant expression - for example: string for documentation from boa3.constants import SYS_VERSION_INFO if SYS_VERSION_INFO >= (3, 8): function.body = [stmt for stmt in function.body if not (isinstance(stmt, ast.Expr) and isinstance(stmt.value, ast.Constant))] else: function.body = [stmt for stmt in function.body if not (isinstance(stmt, ast.Expr) and (hasattr(stmt.value, 'n') or hasattr(stmt.value, 's')) )] for stmt in function.body: self.visit(stmt) self.__include_callable(function.name, method) method_scope = self._scope_stack.pop() global_scope_symbols = self._scope_stack[0].symbols if len(self._scope_stack) > 0 else {} for var_id, var in method_scope.symbols.items(): if isinstance(var, Variable) and var_id not in self._annotated_variables: method.include_variable(var_id, Variable(UndefinedType, var.origin)) else: method.include_symbol(var_id, var) self._annotated_variables.clear() self._current_method = None
def test_get_function_type(self) -> None: """Test get_function_type returns the right type""" self.assertEqual( get_function_type( FunctionDef( args=arguments( args=[set_arg("something else")], arg=None, ), arguments_args=None, identifier_name=None, stmt=None, ) ), "static", ) self.assertEqual( get_function_type( FunctionDef( args=arguments(args=[], arg=None), arguments_args=None, identifier_name=None, stmt=None, ) ), "static", ) self.assertEqual( get_function_type( FunctionDef( args=arguments( args=[set_arg("self")], arg=None, ), arguments_args=None, identifier_name=None, stmt=None, ) ), "self", ) self.assertEqual( get_function_type( FunctionDef( args=arguments( args=[set_arg("cls")], arg=None, ), arguments_args=None, identifier_name=None, stmt=None, ) ), "cls", )
def visit_FunctionDef(self, function: ast.FunctionDef): """ Visitor of the function node Includes the method in the scope of its module :param function: """ fun_args = self.visit(function.args) fun_rtype_symbol = self.visit( function.returns) if function.returns is not None else Type.none if fun_rtype_symbol is None: # it is a function with None return: Main(a: int) -> None: raise NotImplementedError if isinstance(fun_rtype_symbol, str): symbol = self.get_symbol(function.returns.id) fun_rtype_symbol = self.get_type(symbol) fun_return: IType = self.get_type(fun_rtype_symbol) fun_decorators: List[Method] = self._get_function_decorators(function) if Builtin.Metadata in fun_decorators: self._read_metadata_object(function) return Builtin.Metadata method = Method(args=fun_args, defaults=function.args.defaults, return_type=fun_return, origin_node=function, is_public=Builtin.Public in fun_decorators) self._current_method = method # don't evaluate constant expression - for example: string for documentation from boa3.constants import SYS_VERSION_INFO if SYS_VERSION_INFO >= (3, 8): function.body = [ stmt for stmt in function.body if not (isinstance(stmt, ast.Expr) and isinstance(stmt.value, ast.Constant)) ] else: function.body = [ stmt for stmt in function.body if not (isinstance(stmt, ast.Expr) and (hasattr(stmt.value, 'n') or hasattr(stmt.value, 's'))) ] for stmt in function.body: self.visit(stmt) self.__include_callable(function.name, method) self._current_method = None
def add_function_doc_to_ast(ast_function: ast.FunctionDef) -> None: """<#TODO Description> Parameters ---------- ast_function : ast <#TODO Description> Returns ------- None : <#TODO return description> Examples -------- >>> from crawto-quality import crawto_doc >>> add_function_doc_to_ast(ast_function=<#TODO Example Value>) <#TODO Method Return Value> """ functiondef = CrawtoFunction(**ast_function.__dict__, doc_string=ast.get_docstring(ast_function)) function_docstring = ast.parse(functiondef.docs) expr = function_docstring.body[0] new_function_doc = [expr] if ast.get_docstring(ast_function): ast_function.body.pop(0) ast_function.body = new_function_doc + ast_function.body
def getter(item): """Construct the getter function. partof: #SPC-asts.getter """ func_name = f"{item.var}_getter" self_arg = arg(arg="self", annotation=None) func_args = arguments( args=[self_arg], kwonlyargs=[], vararg=None, kwarg=None, defaults=[], kw_defaults=[], ) inst_var = Attribute(value=Name(id="self", ctx=ast.Load()), attr=f"_{item.var}", ctx=ast.Load()) ret_stmt = Return(value=inst_var) func_node = FunctionDef(name=func_name, args=func_args, body=[ret_stmt], decorator_list=[], returns=None) mod_node = Module(body=[func_node]) return ast_to_func(mod_node, func_name)
def setter(item): """Construct the setter function. partof: #SPC-asts.setter """ func_name = f"{item.var}_setter" self_arg = arg(arg="self", annotation=None) new_arg = arg(arg="new", annotation=None) func_args = arguments( args=[self_arg, new_arg], kwonlyargs=[], vararg=None, kwarg=None, defaults=[], kw_defaults=[], ) func_node = FunctionDef( name=func_name, args=func_args, body=[setter_body(item)], decorator_list=[], returns=None, ) mod_node = Module(body=[func_node]) return ast_to_func(mod_node, func_name)
def visit_FunctionDef(self, node: ast.FunctionDef) -> Optional[ast.AST]: self.visit(node.args) for child in node.body: self.visit(child) if node.returns is not None: node.returns = None return node
def test_from_argparse_ast_empty(self) -> None: """ Tests `argparse_ast` empty condition """ self.assertEqual( emit.to_code( emit.argparse_function( parse.argparse_ast( FunctionDef( body=[], arguments_args=None, identifier_name=None, stmt=None, )), emit_default_doc=True, )).rstrip("\n"), "def set_cli_args(argument_parser):\n{tab}{body}".format( tab=tab, body=tab.join(( '"""\n', "Set CLI arguments\n\n", ":param argument_parser: argument parser\n", ":type argument_parser: ```ArgumentParser```\n\n", ":returns: argument_parser\n", ":rtype: ```ArgumentParser```\n", '"""\n', "argument_parser.description = ''\n", "return argument_parser", )), ), )
def getLambdaMethod(methodname, body, args_node, file, id): # methodname = _lambda_0 global lambdaID fullbody = [] fullbody.extend(getTracers(file, args_node, id)) fullbody.append(getScopeTracer(file, methodname, id)) fullbody.append( Return( value=Call( func=Name(id='eval', ctx=Load()), args=[ Constant(value=to_source(body), kind=None), Call( func=Name(id='locals', ctx=Load()), args=[], keywords=[]), Name(id=methodname + '_locals', ctx=Load())], keywords=[] ) ) ) return FunctionDef( name=methodname + '_return', args=args_node, body=fullbody, decorator_list=[], returns=None, type_comment=None)
def visit_FunctionDef(self, node: FunctionDef): if _is_qdef(node): fix_location = partial(copy_location, old_node=node) node.body = self.generic_visit(Suite(node.body)).body new_nodes = [node] if _auto_adjoint(node): adjoint_implementation = fix_location( self._compute_adjoint(node)) adjoint_implementation.body.reverse() new_nodes.append(adjoint_implementation) new_nodes.extend( map(fix_location, _wire_adjoints(node, adjoint_implementation))) if _auto_controlled(node): controlled_implementation = fix_location( self._compute_controlled(node)) new_nodes.append(controlled_implementation) new_nodes.extend( map(fix_location, _wire_controlled(node, controlled_implementation))) return new_nodes self.generic_visit(node) return node
def single_row_expression_func(ra, ctx: Context): """ Build a function that calcs the expression and returns it """ func_name = unique_name('cmp') return_stmt = ast.Return(value=convert(ra, ctx)) # Make func def func_args = [ ast.arg(arg='row', annotation=None), ] func_def = FunctionDef(name=func_name, args=ast.arguments(args=func_args, vararg=None, kwonlyargs=[], kw_defaults=[], kwarg=None, defaults=[]), body=[ return_stmt, ], decorator_list=[], returns=None) ast.fix_missing_locations(func_def) logger.debug(astor.to_source(func_def)) return func_def
def make_function(name, args): my_args = arguments( args=[arg(arg="self", annotation=None)] + [ # XXX for arrays the name is '' (empty string) # which would end up being nothing in the generated # source file. arg(arg=my_arg or "arg", annotation=None) for my_arg in args ], defaults=[], vararg=None, kwonlyargs=[], kw_defaults=[], kwarg=None, ) my_body = [ Return(value=Call( func=Attribute( value=Attribute( value=Attribute(value=Name(id="self"), attr="_contract"), attr="functions", ), attr=name, ), args=[Name(id=my_arg or "arg") for my_arg in args], keywords=[], )) ] return FunctionDef(name=name, args=my_args, body=my_body, decorator_list=[])
def _expand_view(self, node: ast.FunctionDef) -> Any: # add `contract_callback: Contract[return_type]` to method parameter callback_annotation = ast.Subscript( value=ast.Name(id='Contract', ctx=ast.Load()), slice=ast.Index(value=node.returns), ctx=ast.Load()) callback_argument = ast.arg(arg="__callback__", annotation=callback_annotation) node.args.args.append(callback_argument) # remove the return type annotation node.returns = None # transform all return expressions into `transaction` function call node.body = self._transform_block(node.body) return node
def merge(self, kernel): funcname = self.funcname + kernel.funcname func_ast = FunctionDef(name=funcname, args=self.py_ast.args, body=self.py_ast.body + kernel.py_ast.body, decorator_list=[], lineno=1, col_offset=0) return Kernel(self.fieldset, self.ptype, pyfunc=None, funcname=funcname, funccode=self.funccode + kernel.funccode, py_ast=func_ast, funcvars=self.funcvars + kernel.funcvars)
def visit_FunctionDef(self, node: FunctionDef) -> AST: return FunctionDef( name=node.name, args=node.args, body=[subtree_to_be_injected] + node.body, decorator_list=node.decorator_list, returns=node.returns )
def visit_FunctionDef(self, node: FunctionDef) -> FunctionDef: self.generic_visit(node) if len(node.body) and isinstance(node.body[-1], Expr): node.body[-1] = Return(value=node.body[-1].value) fix_missing_locations(node.body[-1]) return node
def visit_FunctionDef(self, node: ast.FunctionDef) -> ast.FunctionDef: """ Any `def name` definition. """ self.register_docstring(node) node.name = self.add_placeholder(node.name) self.generic_visit(node) return node
def visit_FunctionDef(self, node: ast.FunctionDef): if node.name == self.curr.__name__: node.decorator_list = [ d for d in node.decorator_list if d.id != 'refine' ] for stmt in node.body: self.visit(stmt) return node
def visitFunctionDef(self, n: ast.FunctionDef, ismethod, *args): n.retic_ismethod = ismethod # argbindings = [] # for arg in n.args.args: # argty = argvar(arg, n.name, 'arg') # argbindings.append((arg.arg, argty)) # if n.args.vararg or n.args.kwonlyargs or n.args.kwarg or n.args.defaults: # if n.args.defaults: # argbindings = argbindings[:-len(n.args.defaults)] # argsty = ctypes.ArbCAT() # else: # argsty = ctypes.PosCAT([v for k, v in argbindings]) sig = argspec.csignature(n.args, argvar, n.name) argsty = ctypes.SpecCAT(sig) if hasattr(n, 'retic_return_ctype'): retty = n.retic_return_ctype else: retty = n.retic_return_ctype = ctypes.CVar(n.name + 'return') funty = ctypes.CFunction(argsty, retty) n.retic_ctype = funty if n.decorator_list: for dec in n.decorator_list: if isinstance(dec, ast.Name): if dec.id == 'property': return {n.name: funty.to} elif dec.id == 'positional': if n.args.vararg or n.args.kwonlyargs or n.args.kwarg or n.args.defaults: raise Exception() funty.froms = ctypes.PosCAT([ argsty.spec.parameters[p].annotation for p in argsty.spec.parameters ]) elif isinstance(dec, ast.Attribute): if isinstance(dec.value, ast.Name) and dec.attr in [ 'setter', 'getter', 'deleter' ]: return {} return {n.name: funty}
def _make_call_meth(body, return_type, param_names): """ Construct a `__call__` method from the provided `body` :param body: The body, probably from a FunctionDef.body :type body: ```List[AST]``` :param return_type: The return type of the parent symbol (probably class). Used to fill in `__call__` return. :type return_type: ```Optional[str]``` :param param_names: Container of AST `id`s to match for rename :type param_names: ```Optional[Iterator[str]]``` :return: Internal function for `__call__` :rtype: ```FunctionDef``` """ body_len = len(body) return_ = (ast.fix_missing_locations( RewriteName(param_names).visit( Return(get_value(ast.parse(return_type[3:-3]).body[0]), expr=None))) if return_type is not None and len(return_type) > 6 and return_type.startswith("```") and return_type.endswith("```") else None) if body_len: if isinstance(body[0], Expr): doc_str = get_value(body[0].value) if isinstance(doc_str, str) and body_len > 0: body = (body[1:] if body_len > 1 else ([ set_value(doc_str.replace(":cvar", ":param")) if return_ is None else return_ ] if body_len == 1 else body)) # elif not isinstance(body[0], Return) and return_ is not None: # body.append(return_) # elif return_ is not None: # body = [return_] return FunctionDef(args=arguments( args=[set_arg("self")], defaults=[], kw_defaults=[], kwarg=None, kwonlyargs=[], posonlyargs=[], vararg=None, arg=None, ), body=body, decorator_list=[], name="__call__", returns=None, arguments_args=None, identifier_name=None, stmt=None, lineno=None, **maybe_type_comment)
def visit_FunctionDef(self, node: ast.FunctionDef) -> Any: # Skip the top function definition (handled outside of the resolver) if self.toplevel_function: self.toplevel_function = False node.decorator_list = [] # Skip decorators return self.generic_visit(node) for arg in ast.walk(node.args): if isinstance(arg, ast.arg): self.current_scope.add(arg.arg) return self.generic_visit(node)
def merge(self, kernel, kclass): funcname = self.funcname + kernel.funcname func_ast = None if self.py_ast is not None: func_ast = FunctionDef(name=funcname, args=self.py_ast.args, body=self.py_ast.body + kernel.py_ast.body, decorator_list=[], lineno=1, col_offset=0) delete_cfiles = self.delete_cfiles and kernel.delete_cfiles return kclass(self.fieldset, self.ptype, pyfunc=None, funcname=funcname, funccode=self.funccode + kernel.funccode, py_ast=func_ast, funcvars=self.funcvars + kernel.funcvars, c_include=self._c_include + kernel.c_include, delete_cfiles=delete_cfiles)
def visit_FunctionDef(self, node: FunctionDef) -> Optional[AST]: scope = node._pyo_scope # find unused vars for name in scope.locals_: if len(get_loads(scope, name)) == 0: for usage in scope.locals_[name]: if isinstance(usage, Name): # usage.id = _PYO_UNUSED usage._pyo_unused = True elif isinstance(usage, (Nonlocal, Global)): usage.names.remove(name) # TODO: elif IMport node = self.generic_visit(node) # remove passes node.body = [stmt for stmt in node.body if not isinstance(stmt, Pass)] if not node.body: node.body = [Pass()] return node
def visit_FunctionDef(self, function_node: ast.FunctionDef): # remove our decorator from the list new_decorators = [] for decorator in function_node.decorator_list: if decorator.id != 'decor': new_decorators.append(decorator) else: new_decorators.append(decorator) decorator.id = 'transformed_function' function_node.decorator_list[:] = new_decorators return function_node
def visit_FunctionDef(self, node: ast.FunctionDef): for i in range(len(node.args.args)): name = node.args.args[i].arg node.args.args[i].arg = self.get_new_sym(name) node.name = self.get_new_sym(node.name) # recursively change the symbols within the function for i in node.body: self.generic_visit(i) return node
def dgemmify(func): """ This method takes a kernel function and uses DotOpFinder to convert any references to Array.dot (which is numpy.dot) to calls to scipy.linalg.blas.dgemm. :param: func (Function): the function to do this conversion on :return: a Function that does the same thing that func does, except with dgemm calls instead of dot calls. """ tree = get_ast(func) mod_tree = DotOpFinder().visit(tree) # Transpose finding Transpositioner = TranspositionFinder() mod_tree = Transpositioner.visit(mod_tree) mod_tree = NodeDeletor(Transpositioner.marked_for_deletion).visit(mod_tree) # place the modified tree into a clean FunctionDef if sys.version_info >= (3, 0): mod_tree = Module([ FunctionDef(func.__name__, mod_tree.body[0].args, mod_tree.body[0].body, [], None) ]) else: mod_tree = Module([ FunctionDef(func.__name__, mod_tree.body[0].args, mod_tree.body[0].body, []) ]) mod_tree = fix_missing_locations(mod_tree) print(dump(mod_tree)) # compile the function and add it to current local namespace new_func_code = compile(mod_tree, filename=inspect.getsourcefile(func), mode='exec') exec(new_func_code) return locals()[func.__name__]
def visit_FunctionDef(self, node: ast.FunctionDef) -> ast.AST: with_item = ast.withitem(context_expr=ast.Call(func=ast.Name( id='SoftAssertions', ctx=ast.Load()), args=[], keywords=[]), optional_vars=ast.Name(id='ctx', ctx=ast.Store())) with_stmt = ast.With(items=[with_item], body=node.body) node.body = [with_stmt] new_node = fix_node(node) return self.generic_visit(new_node)
def visitFunctionDef(self, n: ast.FunctionDef, ismethod, *args): n.retic_ismethod = ismethod # argbindings = [] # for arg in n.args.args: # argty = argvar(arg, n.name, 'arg') # argbindings.append((arg.arg, argty)) # if n.args.vararg or n.args.kwonlyargs or n.args.kwarg or n.args.defaults: # if n.args.defaults: # argbindings = argbindings[:-len(n.args.defaults)] # argsty = ctypes.ArbCAT() # else: # argsty = ctypes.PosCAT([v for k, v in argbindings]) sig = argspec.csignature(n.args, argvar, n.name) argsty = ctypes.SpecCAT(sig) if hasattr(n, 'retic_return_ctype'): retty = n.retic_return_ctype else: retty = n.retic_return_ctype = ctypes.CVar(n.name + 'return') funty = ctypes.CFunction(argsty, retty) n.retic_ctype = funty if n.decorator_list: for dec in n.decorator_list: if isinstance(dec, ast.Name): if dec.id == 'property': return {n.name: funty.to} elif dec.id == 'positional': if n.args.vararg or n.args.kwonlyargs or n.args.kwarg or n.args.defaults: raise Exception() funty.froms = ctypes.PosCAT([argsty.spec.parameters[p].annotation for p in argsty.spec.parameters]) elif isinstance(dec, ast.Attribute): if isinstance(dec.value, ast.Name) and dec.attr in ['setter', 'getter', 'deleter']: return {} return {n.name: funty}
def compile_factory(fff: FlatFunctionFactory): arguments = [*fff.arguments.keys()] funname = fff.funname unpacking = [] for i, (arg_group_name, arg_group) in enumerate(fff.arguments.items()): for pos, sym in enumerate(arg_group): rhs = Subscript(value=Name(id=arg_group_name, ctx=Load()), slice=Index(Num(pos)), ctx=Load()) val = Assign(targets=[Name(id=sym, ctx=Store())], value=rhs) unpacking.append(val) body = [] for (k, neq) in fff.preamble.items(): val = parse_string(neq).value line = Assign(targets=[Name(id=k, ctx=Store())], value=val) body.append(line) for n, (k, neq) in enumerate(fff.content.items()): # should the result of parse_string always of type Expr ? val = parse_string(neq).value line = Assign(targets=[Name(id=k, ctx=Store())], value=val) body.append(line) # for n, (lhs, neq) in enumerate(fff.content.items()): line = Assign(targets=[ Subscript(value=Name(id='out', ctx=Load()), slice=Index(Num(n)), ctx=Store()) ], value=Name(id=lhs, ctx=Load())) body.append(line) f = FunctionDef(name=funname, args=ast_arguments(args=[arg(arg=a) for a in arguments] + [arg(arg='out')], vararg=None, kwarg=None, kwonlyargs=[], kw_defaults=[], defaults=[]), body=unpacking + body, decorator_list=[]) mod = Module(body=[f]) mmod = ast.fix_missing_locations(mod) return mmod
def visitFunctionDef(self, n: ast.FunctionDef, env, *args)->tydict: sig = argspec.signature(n.args, env) # for arg in n.args.args: # if arg.annotation: # argty = typeparser.typeparse(arg.annotation, env) # else: # argty = retic_ast.Dyn() # argbindings.append((arg.arg, argty)) # if n.args.vararg or n.args.kwonlyargs or n.args.kwarg or n.args.defaults: # if n.args.defaults: # argbindings = argbindings[:-len(n.args.defaults)] # argsty = retic_ast.ApproxNamedAT(argbindings) # else: # argsty = retic_ast.NamedAT(argbindings) argsty = retic_ast.SpecAT(sig) retty = typeparser.typeparse(n.returns, env) funty = retic_ast.Function(argsty, retty) n.retic_type = funty if n.decorator_list: for dec in n.decorator_list: if isinstance(dec, ast.Name): if dec.id == 'property': return {n.name: funty.to} elif dec.id == 'positional': if n.args.vararg or n.args.kwonlyargs or n.args.kwarg or n.args.defaults: raise exc.StaticTypeError(n, "Functions with the 'positional' decorator may only have regular function parameters (no defaults, keyword-only args, starargs, or kwargs)") funty.froms = retic_ast.PosAT([argsty.spec.parameters[p].annotation for p in argsty.spec.parameters]) elif isinstance(dec, ast.Attribute): if isinstance(dec.value, ast.Name) and dec.attr in ['setter', 'getter', 'deleter']: return {} return {n.name: funty}