Python ast.keyword() Examples
The following are 30
code examples of ast.keyword().
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: code_generator.py From nni with MIT License | 6 votes |
def parse_annotation_function(code, func_name): """Parse an annotation function. Return the value of `name` keyword argument and the AST Call node. func_name: expected function name """ expr = parse_annotation(code) call = expr.value assert type(call) is ast.Call, 'Annotation is not a function call' assert type(call.func) is ast.Attribute, 'Unexpected annotation function' assert type(call.func.value) is ast.Name, 'Invalid annotation function name' assert call.func.value.id == 'nni', 'Annotation is not a NNI function' assert call.func.attr == func_name, 'internal error #2' assert len(call.keywords) == 1, 'Annotation function contains more than one keyword argument' assert call.keywords[0].arg == 'name', 'Annotation keyword argument is not "name"' name = call.keywords[0].value return name, call
Example #2
Source File: vars_visitor.py From pyt with GNU General Public License v2.0 | 6 votes |
def visit_curried_call_inside_call_args(self, inner_call): # Curried functions aren't supported really, but we now at least have a defined behaviour. # In f(g(a)(b)(c)), inner_call is the Call node with argument c # Try to get the name of curried function g curried_func = inner_call.func.func while isinstance(curried_func, ast.Call): curried_func = curried_func.func if isinstance(curried_func, ast.Name): self.result.append('ret_' + curried_func.id) elif isinstance(curried_func, ast.Attribute): self.result.append('ret_' + curried_func.attr) # Visit all arguments except a (ignore the curried function g) not_curried = inner_call while not_curried.func is not curried_func: for arg in itertools.chain(not_curried.args, not_curried.keywords): self.visit(arg.value if isinstance(arg, ast.keyword) else arg) not_curried = not_curried.func
Example #3
Source File: vars_visitor.py From pyt with GNU General Public License v2.0 | 6 votes |
def visit_Call(self, node): # This will not visit Flask in Flask(__name__) but it will visit request in `request.args.get() if not isinstance(node.func, ast.Name): self.visit(node.func) for arg_node in itertools.chain(node.args, node.keywords): arg = arg_node.value if isinstance(arg_node, ast.keyword) else arg_node if isinstance(arg, ast.Call): if isinstance(arg.func, ast.Name): # We can't just visit because we need to add 'ret_' self.result.append('ret_' + arg.func.id) elif isinstance(arg.func, ast.Attribute): # e.g. html.replace('{{ param }}', param) # func.attr is replace # func.value.id is html # We want replace self.result.append('ret_' + arg.func.attr) elif isinstance(arg.func, ast.Call): self.visit_curried_call_inside_call_args(arg) else: raise Exception('Cannot visit vars of ' + ast.dump(arg)) else: self.visit(arg)
Example #4
Source File: setup_info.py From pipenv with MIT License | 6 votes |
def unparse_Call(self, item): unparsed = {} if isinstance(item.func, (ast.Name, ast.Attribute)): func_name = self.unparse(item.func) else: try: func_name = self.unparse(item.func) except Exception: func_name = None if not func_name: return {} if isinstance(func_name, dict): unparsed.update(func_name) func_name = next(iter(func_name.keys())) else: unparsed[func_name] = {} for key in ("kwargs", "keywords"): val = getattr(item, key, []) if val is None: continue for keyword in self.unparse(val): unparsed[func_name].update(self.unparse(keyword)) return unparsed
Example #5
Source File: test_ast.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 6 votes |
def test_classdef(self): def cls(bases=None, keywords=None, body=None, decorator_list=None): if bases is None: bases = [] if keywords is None: keywords = [] if body is None: body = [ast.Pass()] if decorator_list is None: decorator_list = [] return ast.ClassDef("myclass", bases, keywords, body, decorator_list) self.stmt(cls(bases=[ast.Name("x", ast.Store())]), "must have Load context") self.stmt(cls(keywords=[ast.keyword("x", ast.Name("x", ast.Store()))]), "must have Load context") self.stmt(cls(body=[]), "empty body on ClassDef") self.stmt(cls(body=[None]), "None disallowed") self.stmt(cls(decorator_list=[ast.Name("x", ast.Store())]), "must have Load context")
Example #6
Source File: setup_info.py From requirementslib with MIT License | 6 votes |
def unparse_Call(self, item): unparsed = {} if isinstance(item.func, (ast.Name, ast.Attribute)): func_name = self.unparse(item.func) else: try: func_name = self.unparse(item.func) except Exception: func_name = None if not func_name: return {} if isinstance(func_name, dict): unparsed.update(func_name) func_name = next(iter(func_name.keys())) else: unparsed[func_name] = {} for key in ("kwargs", "keywords"): val = getattr(item, key, []) if val is None: continue for keyword in self.unparse(val): unparsed[func_name].update(self.unparse(keyword)) return unparsed
Example #7
Source File: translation.py From mochi with MIT License | 6 votes |
def translate_self_evaluating(self, exp): if isinstance(exp, (int, float, complex)): if hasattr(exp, 'value'): return EMPTY, ast.Num(exp.value, lineno=exp.lineno, col_offset=0) else: return EMPTY, ast.Num(exp, lineno=0, col_offset=0) expType = type(exp) if expType is str: return EMPTY, ast.Str(exp, lineno=0, col_offset=0) if expType is Keyword: #raise SyntaxError('keyword!', self.filename) modast = ast.parse( 'Keyword("{name}", {lineno})'.format(name=exp.name, lineno=exp.lineno)) return EMPTY, modast.body[0].value
Example #8
Source File: _343.py From codetransformer with GNU General Public License v2.0 | 6 votes |
def make_call_keywords(stack_builders, count): """ Make the keywords entry for an ast.Call node. """ out = [] for _ in range(count): value = make_expr(stack_builders) load_kwname = stack_builders.pop() if not isinstance(load_kwname, instrs.LOAD_CONST): raise DecompilationError( "Expected a LOAD_CONST, but got %r" % load_kwname ) if not isinstance(load_kwname.arg, str): raise DecompilationError( "Expected LOAD_CONST of a str, but got %r." % load_kwname, ) out.append(ast.keyword(arg=load_kwname.arg, value=value)) out.reverse() return out
Example #9
Source File: test_ast.py From Fluid-Designer with GNU General Public License v3.0 | 6 votes |
def test_classdef(self): def cls(bases=None, keywords=None, body=None, decorator_list=None): if bases is None: bases = [] if keywords is None: keywords = [] if body is None: body = [ast.Pass()] if decorator_list is None: decorator_list = [] return ast.ClassDef("myclass", bases, keywords, body, decorator_list) self.stmt(cls(bases=[ast.Name("x", ast.Store())]), "must have Load context") self.stmt(cls(keywords=[ast.keyword("x", ast.Name("x", ast.Store()))]), "must have Load context") self.stmt(cls(body=[]), "empty body on ClassDef") self.stmt(cls(body=[None]), "None disallowed") self.stmt(cls(decorator_list=[ast.Name("x", ast.Store())]), "must have Load context")
Example #10
Source File: flatten.py From kappa with BSD 2-Clause "Simplified" License | 6 votes |
def visit_Call(self, call: ast.Call) -> VisitExprReturnT: result_actions = [] func, func_actions = self.visit_expr(call.func) result_actions.extend(func_actions) args = [] for arg in call.args: arg_flattened, arg_actions = self.visit_expr(arg) result_actions.extend(arg_actions) args.append(arg_flattened) keywords = [] for kw in call.keywords: kw_value_flattened, kw_value_actions = self.visit_expr(kw.value) result_actions.extend(kw_value_actions) keywords.append(ast.keyword(arg=kw.arg, value=kw_value_flattened)) result_id = self.next_symbol_id() result_actions.append(assign(result_id, ast.Call(func, args, keywords))) return load(result_id), result_actions
Example #11
Source File: flatten.py From kappa with BSD 2-Clause "Simplified" License | 6 votes |
def visit_ClassDef(self, class_def: ast.ClassDef) -> ActionsT: bases, bases_actions = self.visit_expr_list(class_def.bases) keywords = [] keywords_actions = [] for kw in class_def.keywords: kw_value_flattened, kw_value_actions = self.visit_expr(kw.value) keywords_actions.extend(kw_value_actions) keywords.append(ast.keyword(arg=kw.arg, value=kw_value_flattened)) body = self.visit_stmt_list(class_def.body) if class_def.decorator_list: raise NodeNotSupportedError(class_def, "ClassDef decorators not supported") result_node = ast.ClassDef(name=class_def.name, bases=bases, keywords=keywords, body=body, decorator_list=class_def.decorator_list) return bases_actions + keywords_actions + [result_node]
Example #12
Source File: annotate.py From pasta with Apache License 2.0 | 6 votes |
def visit_Call_arguments(self, node): def arg_location(tup): arg = tup[1] if isinstance(arg, ast.keyword): arg = arg.value return (getattr(arg, "lineno", 0), getattr(arg, "col_offset", 0)) if node.starargs: sorted_keywords = sorted( [(None, kw) for kw in node.keywords] + [('*', node.starargs)], key=arg_location) else: sorted_keywords = [(None, kw) for kw in node.keywords] all_args = [(None, n) for n in node.args] + sorted_keywords if node.kwargs: all_args.append(('**', node.kwargs)) for i, (prefix, arg) in enumerate(all_args): if prefix is not None: self.attr(node, '%s_prefix' % prefix, [self.ws, prefix], default=prefix) self.visit(arg) if arg is not all_args[-1][1]: self.attr(node, 'comma_%d' % i, [self.ws, ',', self.ws], default=', ') return bool(all_args)
Example #13
Source File: test_ast.py From ironpython3 with Apache License 2.0 | 6 votes |
def test_classdef(self): def cls(bases=None, keywords=None, starargs=None, kwargs=None, body=None, decorator_list=None): if bases is None: bases = [] if keywords is None: keywords = [] if body is None: body = [ast.Pass()] if decorator_list is None: decorator_list = [] return ast.ClassDef("myclass", bases, keywords, starargs, kwargs, body, decorator_list) self.stmt(cls(bases=[ast.Name("x", ast.Store())]), "must have Load context") self.stmt(cls(keywords=[ast.keyword("x", ast.Name("x", ast.Store()))]), "must have Load context") self.stmt(cls(starargs=ast.Name("x", ast.Store())), "must have Load context") self.stmt(cls(kwargs=ast.Name("x", ast.Store())), "must have Load context") self.stmt(cls(body=[]), "empty body on ClassDef") self.stmt(cls(body=[None]), "None disallowed") self.stmt(cls(decorator_list=[ast.Name("x", ast.Store())]), "must have Load context")
Example #14
Source File: test_ast.py From ironpython3 with Apache License 2.0 | 6 votes |
def test_call(self): func = ast.Name("x", ast.Load()) args = [ast.Name("y", ast.Load())] keywords = [ast.keyword("w", ast.Name("z", ast.Load()))] stararg = ast.Name("p", ast.Load()) kwarg = ast.Name("q", ast.Load()) call = ast.Call(ast.Name("x", ast.Store()), args, keywords, stararg, kwarg) self.expr(call, "must have Load context") call = ast.Call(func, [None], keywords, stararg, kwarg) self.expr(call, "None disallowed") bad_keywords = [ast.keyword("w", ast.Name("z", ast.Store()))] call = ast.Call(func, args, bad_keywords, stararg, kwarg) self.expr(call, "must have Load context") call = ast.Call(func, args, keywords, ast.Name("z", ast.Store()), kwarg) self.expr(call, "must have Load context") call = ast.Call(func, args, keywords, stararg, ast.Name("w", ast.Store())) self.expr(call, "must have Load context")
Example #15
Source File: transformers.py From async2rewrite with MIT License | 6 votes |
def stateful_wait_for(self, call): if isinstance(call.func, ast.Attribute): if call.func.attr in ['wait_for_message', 'wait_for_reaction']: event = call.func.attr.split('_')[2] event = 'message' if event == 'message' else 'reaction_add' call.func.attr = 'wait_for' if call.args: timeout = call.args[0] call.args = [] call.keywords.append(ast.keyword(arg='timeout', value=timeout)) call.args.insert(0, ast.Str(s=event)) for kw in list(call.keywords): if kw.arg != 'check' and kw.arg != 'timeout': call.keywords.remove(kw) warnings.warn('wait_for keyword breaking change detected. Rewrite removes the {} keyword' ' from wait_for.'.format(kw.arg)) elif kw.arg == 'timeout': warnings.warn('wait_for timeout breaking change detected. Timeouts now raise ' 'asyncio.TimeoutError instead of returning None.') stats_counter['call_changes'] += 1 return call
Example #16
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 #17
Source File: specific_code_generator.py From nni with MIT License | 6 votes |
def parse_annotation_function(code, func_name): """Parse an annotation function. Return the value of `name` keyword argument and the AST Call node. func_name: expected function name """ expr = parse_annotation(code) call = expr.value assert type(call) is ast.Call, 'Annotation is not a function call' assert type(call.func) is ast.Attribute, 'Unexpected annotation function' assert type(call.func.value) is ast.Name, 'Invalid annotation function name' assert call.func.value.id == 'nni', 'Annotation is not a NNI function' assert call.func.attr == func_name, 'internal error #2' assert len(call.keywords) == 1, 'Annotation function contains more than one keyword argument' assert call.keywords[0].arg == 'name', 'Annotation keyword argument is not "name"' name = call.keywords[0].value return name, call
Example #18
Source File: function_args.py From wemake-python-styleguide with MIT License | 6 votes |
def _has_same_kwarg( node: types.AnyFunctionDefAndLambda, call: ast.Call, ) -> bool: """Tells whether ``call`` has the same kwargs as ``node``.""" kwarg_name: Optional[str] = None null_arg_keywords = filter(lambda key: key.arg is None, call.keywords) for keyword in null_arg_keywords: # `a=1` vs `**kwargs`: # {'arg': 'a', 'value': <_ast.Num object at 0x1027882b0>} # {'arg': None, 'value': <_ast.Name object at 0x102788320>} if isinstance(keyword.value, ast.Name): kwarg_name = keyword.value.id else: # We can judge on things like `**{}` return False if node.args.kwarg and kwarg_name: return node.args.kwarg.arg == kwarg_name return node.args.kwarg == kwarg_name # type: ignore
Example #19
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 #20
Source File: hgawk_grammar.py From histogrammar-python with Apache License 2.0 | 5 votes |
def iskeyword(x): return isinstance(x, ast.keyword)
Example #21
Source File: annotate.py From pasta with Apache License 2.0 | 5 votes |
def visit_Call_arguments35(self, node): def arg_compare(a1, a2): """Old-style comparator for sorting args.""" def is_arg(a): return not isinstance(a, (ast.keyword, ast.Starred)) # No kwarg can come before a regular arg (but Starred can be wherever) if is_arg(a1) and isinstance(a2, ast.keyword): return -1 elif is_arg(a2) and isinstance(a1, ast.keyword): return 1 # If no lineno or col_offset on one of the args, they compare as equal # (since sorting is stable, this should leave them mostly where they # were in the initial list). def get_pos(a): if isinstance(a, ast.keyword): a = a.value return (getattr(a, 'lineno', None), getattr(a, 'col_offset', None)) pos1 = get_pos(a1) pos2 = get_pos(a2) if None in pos1 or None in pos2: return 0 # If both have lineno/col_offset set, use that to sort them return -1 if pos1 < pos2 else 0 if pos1 == pos2 else 1 # Note that this always sorts keywords identically to just sorting by # lineno/col_offset, except in cases where that ordering would have been # a syntax error (named arg before unnamed arg). all_args = sorted(node.args + node.keywords, key=functools.cmp_to_key(arg_compare)) for i, arg in enumerate(all_args): self.visit(arg) if arg is not all_args[-1]: self.attr(node, 'comma_%d' % i, [self.ws, ',', self.ws], default=', ') return bool(all_args)
Example #22
Source File: statements.py From wemake-python-styleguide with MIT License | 5 votes |
def _check_double_starred_dict( self, keywords: Sequence[ast.keyword], ) -> None: for keyword in keywords: if keyword.arg is not None: continue if self._has_wrong_keys(keyword): self.add_violation(WrongNamedKeywordViolation(keyword.value))
Example #23
Source File: statements.py From wemake-python-styleguide with MIT License | 5 votes |
def _has_non_string_keys(self, node: ast.keyword) -> bool: if not isinstance(node.value, ast.Dict): return True for key_node in node.value.keys: if not isinstance(key_node, ast.Str): return True return False
Example #24
Source File: statements.py From wemake-python-styleguide with MIT License | 5 votes |
def _check_double_starred_dict( self, keywords: Sequence[ast.keyword], ) -> None: for keyword in keywords: if keyword.arg is not None: continue complex_keys = self._has_non_string_keys(keyword) pointless_args = self._is_pointless_star(keyword.value) if not complex_keys and pointless_args: self.add_violation(PointlessStarredViolation(keyword.value))
Example #25
Source File: test_ast.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 5 votes |
def test_call(self): func = ast.Name("x", ast.Load()) args = [ast.Name("y", ast.Load())] keywords = [ast.keyword("w", ast.Name("z", ast.Load()))] call = ast.Call(ast.Name("x", ast.Store()), args, keywords) self.expr(call, "must have Load context") call = ast.Call(func, [None], keywords) self.expr(call, "None disallowed") bad_keywords = [ast.keyword("w", ast.Name("z", ast.Store()))] call = ast.Call(func, args, bad_keywords) self.expr(call, "must have Load context")
Example #26
Source File: hgawk_grammar.py From histogrammar-python with Apache License 2.0 | 5 votes |
def notkeyword(x): return not isinstance(x, ast.keyword)
Example #27
Source File: _343.py From codetransformer with GNU General Public License v2.0 | 5 votes |
def _make_expr_call_function_var(toplevel, stack_builders): starargs = make_expr(stack_builders) keywords = make_call_keywords(stack_builders, toplevel.keyword) positionals = make_call_positionals(stack_builders, toplevel.positional) return ast.Call( func=make_expr(stack_builders), args=positionals, keywords=keywords, starargs=starargs, kwargs=None, )
Example #28
Source File: _343.py From codetransformer with GNU General Public License v2.0 | 5 votes |
def _make_expr_call_function(toplevel, stack_builders): keywords = make_call_keywords(stack_builders, toplevel.keyword) positionals = make_call_positionals(stack_builders, toplevel.positional) return ast.Call( func=make_expr(stack_builders), args=positionals, keywords=keywords, starargs=None, kwargs=None, )
Example #29
Source File: _343.py From codetransformer with GNU General Public License v2.0 | 5 votes |
def _make_expr_call_function_var_kw(toplevel, stack_builders): kwargs = make_expr(stack_builders) starargs = make_expr(stack_builders) keywords = make_call_keywords(stack_builders, toplevel.keyword) positionals = make_call_positionals(stack_builders, toplevel.positional) return ast.Call( func=make_expr(stack_builders), args=positionals, keywords=keywords, starargs=starargs, kwargs=kwargs, )
Example #30
Source File: _343.py From codetransformer with GNU General Public License v2.0 | 5 votes |
def _make_expr_call_function_kw(toplevel, stack_builders): kwargs = make_expr(stack_builders) keywords = make_call_keywords(stack_builders, toplevel.keyword) positionals = make_call_positionals(stack_builders, toplevel.positional) return ast.Call( func=make_expr(stack_builders), args=positionals, keywords=keywords, starargs=None, kwargs=kwargs, )