Python ast.arguments() Examples
The following are 30
code examples of ast.arguments().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
ast
, or try the search function
.
Example #1
Source File: _343.py From codetransformer with GNU General Public License v2.0 | 7 votes |
def make_function_arguments(args, kwonly, varargs, varkwargs, defaults, kw_defaults, annotations): """ Make an ast.arguments from the args parsed out of a code object. """ return ast.arguments( args=[ast.arg(arg=a, annotation=annotations.get(a)) for a in args], kwonlyargs=[ ast.arg(arg=a, annotation=annotations.get(a)) for a in kwonly ], defaults=defaults, kw_defaults=list(map(kw_defaults.get, kwonly)), vararg=None if varargs is None else ast.arg( arg=varargs, annotation=annotations.get(varargs), ), kwarg=None if varkwargs is None else ast.arg( arg=varkwargs, annotation=annotations.get(varkwargs) ), )
Example #2
Source File: test_ast.py From ironpython3 with Apache License 2.0 | 6 votes |
def test_nodeclasses(self): # IronPyhon performs argument typechecking l=ast.Str('A') o=ast.Mult() r=ast.Num('13') x=ast.BinOp(l,o,r,lineno=42) self.assertEqual(x.left, l) self.assertEqual(x.op, o) self.assertEqual(x.right, r) self.assertEqual(x.lineno, 42) # node raises exception when not given enough arguments self.assertRaises(TypeError, ast.BinOp, l, o) # can set attributes through kwargs too x = ast.BinOp(left=l, op=o, right=r, lineno=42) self.assertEqual(x.left, l) self.assertEqual(x.op, o) self.assertEqual(x.right, r) self.assertEqual(x.lineno, 42) # this used to fail because Sub._fields was None x = ast.Sub()
Example #3
Source File: test_ast.py From ironpython2 with Apache License 2.0 | 6 votes |
def test_AST_objects(self): x = ast.AST() self.assertEqual(x._fields, ()) with self.assertRaises(AttributeError): x.vararg with self.assertRaises(AttributeError): x.foobar = 21 with self.assertRaises(AttributeError): ast.AST(lineno=2) with self.assertRaises(TypeError): # "_ast.AST constructor takes 0 positional arguments" ast.AST(2)
Example #4
Source File: test_ast.py From ironpython2 with Apache License 2.0 | 6 votes |
def test_nodeclasses(self): # IronPyhon performs argument typechecking l=ast.Str('A') o=ast.Mult() r=ast.Num('13') x=ast.BinOp(l,o,r,lineno=42) self.assertEqual(x.left, l) self.assertEqual(x.op, o) self.assertEqual(x.right, r) self.assertEqual(x.lineno, 42) # node raises exception when not given enough arguments self.assertRaises(TypeError, ast.BinOp, l, o) # can set attributes through kwargs too x = ast.BinOp(left=l, op=o, right=r, lineno=42) self.assertEqual(x.left, l) self.assertEqual(x.op, o) self.assertEqual(x.right, r) self.assertEqual(x.lineno, 42) # this used to fail because Sub._fields was None x = ast.Sub()
Example #5
Source File: test_ast.py From oss-ftp with MIT License | 6 votes |
def test_AST_objects(self): x = ast.AST() self.assertEqual(x._fields, ()) with self.assertRaises(AttributeError): x.vararg with self.assertRaises(AttributeError): x.foobar = 21 with self.assertRaises(AttributeError): ast.AST(lineno=2) with self.assertRaises(TypeError): # "_ast.AST constructor takes 0 positional arguments" ast.AST(2)
Example #6
Source File: namedlist.py From appcompatprocessor with Apache License 2.0 | 6 votes |
def _check_specific_to_fields(self, name): # these tests don't apply for the typename, just the fieldnames if name in self.seen_fields: raise ValueError('Encountered duplicate field name: ' '{0!r}'.format(name)) if name.startswith('_'): raise ValueError('Field names cannot start with an underscore: ' '{0!r}'.format(name)) ######################################################################## # Returns a function with name 'name', that calls another function 'chain_fn' # This is used to create the __init__ function with the right argument names and defaults, that # calls into _init to do the real work. # The new function takes args as arguments, with defaults as given.
Example #7
Source File: test_ast.py From BinderFilter with MIT License | 6 votes |
def test_AST_objects(self): x = ast.AST() self.assertEqual(x._fields, ()) with self.assertRaises(AttributeError): x.vararg with self.assertRaises(AttributeError): x.foobar = 21 with self.assertRaises(AttributeError): ast.AST(lineno=2) with self.assertRaises(TypeError): # "_ast.AST constructor takes 0 positional arguments" ast.AST(2)
Example #8
Source File: test_ast.py From gcblue with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_AST_objects(self): x = ast.AST() self.assertEqual(x._fields, ()) with self.assertRaises(AttributeError): x.vararg with self.assertRaises(AttributeError): x.foobar = 21 with self.assertRaises(AttributeError): ast.AST(lineno=2) with self.assertRaises(TypeError): # "_ast.AST constructor takes 0 positional arguments" ast.AST(2)
Example #9
Source File: ast3.py From gast with BSD 3-Clause "New" or "Revised" License | 6 votes |
def visit_arguments(self, node): extra_args = [self._make_arg(node.vararg), [self._make_arg(n) for n in node.kwonlyargs], self._visit(node.kw_defaults), self._make_arg(node.kwarg), self._visit(node.defaults), ] if sys.version_info.minor >= 8: new_node = ast.arguments( [self._make_arg(arg) for arg in node.posonlyargs], [self._make_arg(n) for n in node.args], *extra_args ) else: new_node = ast.arguments( [self._make_arg(n) for n in node.args], *extra_args ) return new_node
Example #10
Source File: specific_code_generator.py From nni with MIT License | 6 votes |
def parse_nni_variable(code): """Parse `nni.variable` expression. Return the name argument and AST node of annotated expression. code: annotation string """ name, call = parse_annotation_function(code, 'variable') assert len(call.args) == 1, 'nni.variable contains more than one arguments' arg = call.args[0] assert type(arg) is ast.Call, 'Value of nni.variable is not a function call' assert type(arg.func) is ast.Attribute, 'nni.variable value is not a NNI function' assert type(arg.func.value) is ast.Name, 'nni.variable value is not a NNI function' assert arg.func.value.id == 'nni', 'nni.variable value is not a NNI function' name_str = astor.to_source(name).strip() keyword_arg = ast.keyword(arg='name', value=ast.Str(s=name_str)) arg.keywords.append(keyword_arg) if arg.func.attr == 'choice': convert_args_to_dict(arg) return name, arg
Example #11
Source File: code_generator.py From nni with MIT License | 6 votes |
def parse_nni_variable(code): """Parse `nni.variable` expression. Return the name argument and AST node of annotated expression. code: annotation string """ name, call = parse_annotation_function(code, 'variable') assert len(call.args) == 1, 'nni.variable contains more than one arguments' arg = call.args[0] assert type(arg) is ast.Call, 'Value of nni.variable is not a function call' assert type(arg.func) is ast.Attribute, 'nni.variable value is not a NNI function' assert type(arg.func.value) is ast.Name, 'nni.variable value is not a NNI function' assert arg.func.value.id == 'nni', 'nni.variable value is not a NNI function' name_str = astor.to_source(name).strip() keyword_arg = ast.keyword(arg='name', value=ast.Str(s=name_str)) arg.keywords.append(keyword_arg) if arg.func.attr == 'choice': convert_args_to_dict(arg) return name, arg
Example #12
Source File: _recompute.py From icontract with MIT License | 6 votes |
def visit_Call(self, node: ast.Call) -> Any: """Visit the function and the arguments and finally make the function call with them.""" func = self.visit(node=node.func) args = [] # type: List[Any] for arg_node in node.args: if isinstance(arg_node, ast.Starred): args.extend(self.visit(node=arg_node)) else: args.append(self.visit(node=arg_node)) kwargs = dict() # type: Dict[str, Any] for keyword in node.keywords: if keyword.arg is None: kw = self.visit(node=keyword.value) for key, val in kw.items(): kwargs[key] = val else: kwargs[keyword.arg] = self.visit(node=keyword.value) result = func(*args, **kwargs) self.recomputed_values[node] = result return result
Example #13
Source File: hgawk_grammar.py From histogrammar-python with Apache License 2.0 | 5 votes |
def p_varargslist_14(p): '''varargslist : fpdef EQUAL test''' # 1 2 3 p[0] = ast.arguments([p[1]], None, None, [p[3]], rule=inspect.currentframe().f_code.co_name) inherit_lineno(p[0], p[1])
Example #14
Source File: hgawk_grammar.py From histogrammar-python with Apache License 2.0 | 5 votes |
def p_varargslist_13(p): '''varargslist : fpdef EQUAL test COMMA DOUBLESTAR NAME''' # 1 2 3 4 5 6 p[0] = ast.arguments([p[1]], None, p[6][0], [p[3]], rule=inspect.currentframe().f_code.co_name) inherit_lineno(p[0], p[1])
Example #15
Source File: hgawk_grammar.py From histogrammar-python with Apache License 2.0 | 5 votes |
def p_varargslist_12(p): '''varargslist : fpdef EQUAL test COMMA STAR NAME COMMA DOUBLESTAR NAME''' # 1 2 3 4 5 6 7 8 9 p[0] = ast.arguments([p[1]], p[6][0], p[9][0], [p[3]], rule=inspect.currentframe().f_code.co_name) inherit_lineno(p[0], p[1])
Example #16
Source File: hgawk_grammar.py From histogrammar-python with Apache License 2.0 | 5 votes |
def p_varargslist_15(p): '''varargslist : fpdef EQUAL test COMMA''' # 1 2 3 4 p[0] = ast.arguments([p[1]], None, None, [p[3]], rule=inspect.currentframe().f_code.co_name) inherit_lineno(p[0], p[1])
Example #17
Source File: hgawk_grammar.py From histogrammar-python with Apache License 2.0 | 5 votes |
def p_varargslist_star_2(p): '''varargslist_star : COMMA fpdef EQUAL test''' # 1 2 3 4 p[0] = ast.arguments([p[2]], None, None, [p[4]], rule=inspect.currentframe().f_code.co_name) inherit_lineno(p[0], p[2])
Example #18
Source File: hgawk_grammar.py From histogrammar-python with Apache License 2.0 | 5 votes |
def p_varargslist_11(p): '''varargslist : fpdef EQUAL test COMMA STAR NAME''' # 1 2 3 4 5 6 p[0] = ast.arguments([p[1]], p[6][0], None, [p[3]], rule=inspect.currentframe().f_code.co_name) inherit_lineno(p[0], p[1])
Example #19
Source File: hgawk_grammar.py From histogrammar-python with Apache License 2.0 | 5 votes |
def p_old_lambdef_1(p): '''old_lambdef : LAMBDA COLON old_test''' # 1 2 3 p[0] = ast.Lambda(ast.arguments([], None, None, [], rule=inspect.currentframe().f_code.co_name, **p[2][1]), p[3], rule=inspect.currentframe().f_code.co_name, **p[1][1])
Example #20
Source File: test_ast.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 5 votes |
def test_AST_objects(self): x = ast.AST() self.assertEqual(x._fields, ()) x.foobar = 42 self.assertEqual(x.foobar, 42) self.assertEqual(x.__dict__["foobar"], 42) with self.assertRaises(AttributeError): x.vararg with self.assertRaises(TypeError): # "_ast.AST constructor takes 0 positional arguments" ast.AST(2)
Example #21
Source File: ast3.py From gast with BSD 3-Clause "New" or "Revised" License | 5 votes |
def visit_arguments(self, node): new_node = gast.arguments( self._visit(node.args), [], # posonlyargs self._visit(node.vararg), self._visit(node.kwonlyargs), self._visit(node.kw_defaults), self._visit(node.kwarg), self._visit(node.defaults), ) gast.copy_location(new_node, node) return new_node
Example #22
Source File: ast2.py From gast with BSD 3-Clause "New" or "Revised" License | 5 votes |
def visit_arguments(self, node): vararg = node.vararg and node.vararg.id kwarg = node.kwarg and node.kwarg.id new_node = ast.arguments( self._visit(node.args), self._visit(vararg), self._visit(kwarg), self._visit(node.defaults), ) return new_node
Example #23
Source File: ast2.py From gast with BSD 3-Clause "New" or "Revised" License | 5 votes |
def visit_arguments(self, node): # missing locations for vararg and kwarg set at function level if node.vararg: vararg = ast.Name(node.vararg, ast.Param()) else: vararg = None if node.kwarg: kwarg = ast.Name(node.kwarg, ast.Param()) else: kwarg = None if node.vararg: vararg = ast.Name(node.vararg, ast.Param()) else: vararg = None new_node = gast.arguments( self._visit(node.args), [], # posonlyargs self._visit(vararg), [], # kwonlyargs [], # kw_defaults self._visit(kwarg), self._visit(node.defaults), ) return new_node
Example #24
Source File: specific_code_generator.py From nni with MIT License | 5 votes |
def make_lambda(call): """Wrap an AST Call node to lambda expression node. call: ast.Call node """ empty_args = ast.arguments(args=[], vararg=None, kwarg=None, defaults=[]) return ast.Lambda(args=empty_args, body=call)
Example #25
Source File: code_generator.py From nni with MIT License | 5 votes |
def make_lambda(call): """Wrap an AST Call node to lambda expression node. call: ast.Call node """ empty_args = ast.arguments(args=[], vararg=None, kwarg=None, defaults=[]) return ast.Lambda(args=empty_args, body=call)
Example #26
Source File: test_ast.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 5 votes |
def test_arguments(self): x = ast.arguments() self.assertEqual(x._fields, ('args', 'vararg', 'kwonlyargs', 'kw_defaults', 'kwarg', 'defaults')) with self.assertRaises(AttributeError): x.vararg x = ast.arguments(*range(1, 7)) self.assertEqual(x.vararg, 2)
Example #27
Source File: _recompute.py From icontract with MIT License | 5 votes |
def _execute_comprehension(self, node: Union[ast.ListComp, ast.SetComp, ast.GeneratorExp, ast.DictComp]) -> Any: """Compile the generator or comprehension from the node and execute the compiled code.""" args = [ast.arg(arg=name) for name in sorted(self._name_to_value.keys())] if platform.python_version_tuple() < ('3', ): raise NotImplementedError("Python versions below not supported, got: {}".format(platform.python_version())) if platform.python_version_tuple() < ('3', '8'): func_def_node = ast.FunctionDef( name="generator_expr", args=ast.arguments(args=args, kwonlyargs=[], kw_defaults=[], defaults=[]), decorator_list=[], body=[ast.Return(node)]) module_node = ast.Module(body=[func_def_node]) else: func_def_node = ast.FunctionDef( name="generator_expr", args=ast.arguments(args=args, posonlyargs=[], kwonlyargs=[], kw_defaults=[], defaults=[]), decorator_list=[], body=[ast.Return(node)]) module_node = ast.Module(body=[func_def_node], type_ignores=[]) ast.fix_missing_locations(module_node) code = compile(source=module_node, filename='<ast>', mode='exec') module_locals = {} # type: Dict[str, Any] module_globals = {} # type: Dict[str, Any] exec(code, module_globals, module_locals) # pylint: disable=exec-used generator_expr_func = module_locals["generator_expr"] return generator_expr_func(**self._name_to_value)
Example #28
Source File: test_ast.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 5 votes |
def _check_arguments(self, fac, check): def arguments(args=None, vararg=None, kwonlyargs=None, kwarg=None, defaults=None, kw_defaults=None): if args is None: args = [] if kwonlyargs is None: kwonlyargs = [] if defaults is None: defaults = [] if kw_defaults is None: kw_defaults = [] args = ast.arguments(args, vararg, kwonlyargs, kw_defaults, kwarg, defaults) return fac(args) args = [ast.arg("x", ast.Name("x", ast.Store()))] check(arguments(args=args), "must have Load context") check(arguments(kwonlyargs=args), "must have Load context") check(arguments(defaults=[ast.Num(3)]), "more positional defaults than args") check(arguments(kw_defaults=[ast.Num(4)]), "length of kwonlyargs is not the same as kw_defaults") args = [ast.arg("x", ast.Name("x", ast.Load()))] check(arguments(args=args, defaults=[ast.Name("x", ast.Store())]), "must have Load context") args = [ast.arg("a", ast.Name("x", ast.Load())), ast.arg("b", ast.Name("y", ast.Load()))] check(arguments(kwonlyargs=args, kw_defaults=[None, ast.Name("x", ast.Store())]), "must have Load context")
Example #29
Source File: test_ast.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 5 votes |
def test_funcdef(self): a = ast.arguments([], None, [], [], None, []) f = ast.FunctionDef("x", a, [], [], None) self.stmt(f, "empty body on FunctionDef") f = ast.FunctionDef("x", a, [ast.Pass()], [ast.Name("x", ast.Store())], None) self.stmt(f, "must have Load context") f = ast.FunctionDef("x", a, [ast.Pass()], [], ast.Name("x", ast.Store())) self.stmt(f, "must have Load context") def fac(args): return ast.FunctionDef("x", args, [ast.Pass()], [], None) self._check_arguments(fac, self.stmt)
Example #30
Source File: hgawk_grammar.py From histogrammar-python with Apache License 2.0 | 5 votes |
def p_parameters_1(p): '''parameters : LPAR RPAR''' # 1 2 p[0] = ast.arguments([], None, None, [], rule=inspect.currentframe().f_code.co_name, **p[1][1])