Python ast.Starred() Examples
The following are 30
code examples of ast.Starred().
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: overuses.py From wemake-python-styleguide with MIT License | 6 votes |
def is_primitive(node: ast.AST) -> bool: """ Detects if node is a form of a primitive value. We use this predicate to allow values like ``[]`` or ``call()`` to be overused. Because you cannot simply them. We do not check for strings, numbers, etc because they are globally ignored. """ if isinstance(node, (ast.Tuple, ast.List)): return not node.elts # we do allow `[]` and `()` elif isinstance(node, ast.Set): return ( # we do allow `{*set_items}` len(node.elts) == 1 and isinstance(node.elts[0], ast.Starred) ) elif isinstance(node, ast.Dict): # we do allow `{}` and `{**values}` return not list(filter(None, node.keys)) elif isinstance(node, ast.Call): return not call_args.get_all_args(node) # we do allow `call()` return False
Example #2
Source File: flake8_builtins.py From flake8-builtins with GNU General Public License v2.0 | 6 votes |
def check_for_loop(self, statement): stack = [statement.target] while stack: item = stack.pop() if isinstance(item, (ast.Tuple, ast.List)): stack.extend(list(item.elts)) elif isinstance(item, ast.Name) and \ item.id in BUILTINS: yield self.error(statement, variable=item.id) elif PY3 and isinstance(item, ast.Starred): if hasattr(item.value, 'id') and item.value.id in BUILTINS: yield self.error( statement, variable=item.value.id, ) elif hasattr(item.value, 'elts'): stack.extend(list(item.value.elts))
Example #3
Source File: FstringifyTransformer.py From flynt with MIT License | 6 votes |
def visit_Call(self, node: ast.Call): """ Convert `ast.Call` to `ast.JoinedStr` f-string """ match = matching_call(node) if match: state.call_candidates += 1 # bail in these edge cases... if any(isinstance(arg, ast.Starred) for arg in node.args): return node result_node = joined_string(node) self.visit(result_node) self.counter += 1 state.call_transforms += 1 return result_node return node
Example #4
Source File: call_visitor.py From pyt with GNU General Public License v2.0 | 6 votes |
def visit_Call(self, call_node): func_name = get_call_names_as_string(call_node.func) trigger_re = r"(^|\.){}$".format(re.escape(self._trigger_str)) if re.search(trigger_re, func_name): seen_starred = False for index, arg in enumerate(call_node.args): if isinstance(arg, ast.Starred): seen_starred = True if seen_starred: self.unknown_arg_visitor.visit(arg) else: self.argument_visitors[index].visit(arg) for keyword in call_node.keywords: if keyword.arg is None: self.unknown_kwarg_visitor.visit(keyword.value) else: self.argument_visitors[keyword.arg].visit(keyword.value) self.generic_visit(call_node)
Example #5
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 #6
Source File: name_nodes.py From wemake-python-styleguide with MIT License | 6 votes |
def extract_name(node: ast.AST) -> Optional[str]: """ Utility to extract names for several types of nodes. Is used to get name from node in case it is ``ast.Name``. Should not be used direclty with assigns, use safer :py:`~get_assign_names` function. Example: >>> import ast >>> tree = ast.parse('a') >>> node = tree.body[0].value >>> extract_name(node) 'a' """ if isinstance(node, ast.Starred): node = node.value if isinstance(node, ast.Name): return node.id return None
Example #7
Source File: checker.py From pyflakes with MIT License | 6 votes |
def TUPLE(self, node): if not PY2 and isinstance(node.ctx, ast.Store): # Python 3 advanced tuple unpacking: a, *b, c = d. # Only one starred expression is allowed, and no more than 1<<8 # assignments are allowed before a stared expression. There is # also a limit of 1<<24 expressions after the starred expression, # which is impossible to test due to memory restrictions, but we # add it here anyway has_starred = False star_loc = -1 for i, n in enumerate(node.elts): if isinstance(n, ast.Starred): if has_starred: self.report(messages.TwoStarredExpressions, node) # The SyntaxError doesn't distinguish two from more # than two. break has_starred = True star_loc = i if star_loc >= 1 << 8 or len(node.elts) - star_loc - 1 >= 1 << 24: self.report(messages.TooManyExpressionsInStarredAssignment, node) self.handleChildren(node)
Example #8
Source File: checker.py From linter-pylama with MIT License | 6 votes |
def TUPLE(self, node): if not PY2 and isinstance(node.ctx, ast.Store): # Python 3 advanced tuple unpacking: a, *b, c = d. # Only one starred expression is allowed, and no more than 1<<8 # assignments are allowed before a stared expression. There is # also a limit of 1<<24 expressions after the starred expression, # which is impossible to test due to memory restrictions, but we # add it here anyway has_starred = False star_loc = -1 for i, n in enumerate(node.elts): if isinstance(n, ast.Starred): if has_starred: self.report(messages.TwoStarredExpressions, node) # The SyntaxError doesn't distinguish two from more # than two. break has_starred = True star_loc = i if star_loc >= 1 << 8 or len(node.elts) - star_loc - 1 >= 1 << 24: self.report(messages.TooManyExpressionsInStarredAssignment, node) self.handleChildren(node)
Example #9
Source File: checker.py From blackmamba with MIT License | 6 votes |
def TUPLE(self, node): if not PY2 and isinstance(node.ctx, ast.Store): # Python 3 advanced tuple unpacking: a, *b, c = d. # Only one starred expression is allowed, and no more than 1<<8 # assignments are allowed before a stared expression. There is # also a limit of 1<<24 expressions after the starred expression, # which is impossible to test due to memory restrictions, but we # add it here anyway has_starred = False star_loc = -1 for i, n in enumerate(node.elts): if isinstance(n, ast.Starred): if has_starred: self.report(messages.TwoStarredExpressions, node) # The SyntaxError doesn't distinguish two from more # than two. break has_starred = True star_loc = i if star_loc >= 1 << 8 or len(node.elts) - star_loc - 1 >= 1 << 24: self.report(messages.TooManyExpressionsInStarredAssignment, node) self.handleChildren(node)
Example #10
Source File: parsing.py From pythonwhat with GNU Affero General Public License v3.0 | 5 votes |
def get_pos_arg_part(arg, indx_pos): is_star = isinstance(arg, ast.Starred) return { "node": arg if not is_star else arg.value, "highlight": arg, "type": "argument", "is_starred": is_star, "name": indx_pos, }
Example #11
Source File: reprconf.py From Tautulli with GNU General Public License v3.0 | 5 votes |
def _build_call35(self, o): """ Workaround for python 3.5 _ast.Call signature, docs found here https://greentreesnakes.readthedocs.org/en/latest/nodes.html """ import ast callee = self.build(o.func) args = [] if o.args is not None: for a in o.args: if isinstance(a, ast.Starred): args.append(self.build(a.value)) else: args.append(self.build(a)) kwargs = {} for kw in o.keywords: if kw.arg is None: # double asterix `**` rst = self.build(kw.value) if not isinstance(rst, dict): raise TypeError('Invalid argument for call.' 'Must be a mapping object.') # give preference to the keys set directly from arg=value for k, v in rst.items(): if k not in kwargs: kwargs[k] = v else: # defined on the call as: arg=value kwargs[kw.arg] = self.build(kw.value) return callee(*args, **kwargs)
Example #12
Source File: translation.py From mochi with MIT License | 5 votes |
def create_assign_targets(self, seq): elts = [] for index, item in enumerate(seq): if isinstance(item, Symbol): # TODO errorcheck index + 1 != lastIndex if item == VARG: elts.append( ast.Starred( value=self.create_assign_target( seq[index + 1]), ctx=ast.Store(), lineno=0, # TODO col_offset=0)) break elts.append(self.create_assign_target(item)) elif issequence_except_str(item): if len(item) == 0: raise MochiSyntaxError("can't bind to ()", self.filename) #if item[0] == 'getattr': # attr_exp = self.translate_getattr(item)[1] # attr_exp.ctx = ast.Store() # elts.append(attr_exp) #else: elts.append(self.create_assign_targets(item)) else: raise MochiSyntaxError(item, self.filename) return ast.Tuple(elts=elts, ctx=ast.Store(), lineno=0, # TODO col_offset=0) #@syntax('set')
Example #13
Source File: reprconf.py From bazarr with GNU General Public License v3.0 | 5 votes |
def _build_call35(self, o): """ Workaround for python 3.5 _ast.Call signature, docs found here https://greentreesnakes.readthedocs.org/en/latest/nodes.html """ import ast callee = self.build(o.func) args = [] if o.args is not None: for a in o.args: if isinstance(a, ast.Starred): args.append(self.build(a.value)) else: args.append(self.build(a)) kwargs = {} for kw in o.keywords: if kw.arg is None: # double asterix `**` rst = self.build(kw.value) if not isinstance(rst, dict): raise TypeError('Invalid argument for call.' 'Must be a mapping object.') # give preference to the keys set directly from arg=value for k, v in rst.items(): if k not in kwargs: kwargs[k] = v else: # defined on the call as: arg=value kwargs[kw.arg] = self.build(kw.value) return callee(*args, **kwargs)
Example #14
Source File: parsing.py From pythonwhat with GNU Affero General Public License v3.0 | 5 votes |
def get_target_vars(target): get_id = lambda n: n.id if not isinstance(n, ast.Starred) else n.value.id if isinstance(target, (ast.Name, ast.Starred)): tv = [get_id(target)] elif isinstance(target, ast.Tuple): tv = [get_id(node) for node in target.elts] else: tv = [] return TargetVars(tv)
Example #15
Source File: flatten.py From kappa with BSD 2-Clause "Simplified" License | 5 votes |
def visit_Starred(self, starred: ast.Starred) -> VisitExprReturnT: value, value_actions = self.visit_expr(starred.value) return ast.Starred(value=value, ctx=starred.ctx), value_actions
Example #16
Source File: annotate.py From pasta with Apache License 2.0 | 5 votes |
def visit_Call(self, node): self.visit(node.func) with self.scope(node, 'arguments', default_parens=True): # python <3.5: starargs and kwargs are in separate fields # python 3.5+: starargs args included as a Starred nodes in the arguments # and kwargs are included as keywords with no argument name. if sys.version_info[:2] >= (3, 5): any_args = self.visit_Call_arguments35(node) else: any_args = self.visit_Call_arguments(node) if any_args: self.optional_token(node, 'trailing_comma', ',')
Example #17
Source File: test_ast.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 5 votes |
def test_starred(self): left = ast.List([ast.Starred(ast.Name("x", ast.Load()), ast.Store())], ast.Store()) assign = ast.Assign([left], ast.Num(4)) self.stmt(assign, "must have Store context")
Example #18
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 #19
Source File: checker.py From blackmamba with MIT License | 5 votes |
def getParent(self, node): # Lookup the first parent which is not Tuple, List or Starred while True: node = node.parent if not hasattr(node, 'elts') and not hasattr(node, 'ctx'): return node
Example #20
Source File: statements.py From wemake-python-styleguide with MIT License | 5 votes |
def _check_starred_args( self, args: Sequence[ast.AST], ) -> None: for node in args: if isinstance(node, ast.Starred): if self._is_pointless_star(node.value): self.add_violation(PointlessStarredViolation(node))
Example #21
Source File: iterables.py From wemake-python-styleguide with MIT License | 5 votes |
def visit_Starred(self, node: ast.Starred) -> None: """ Checks iterable's unpacking. Raises: IterableUnpackingViolation """ self._check_unnecessary_iterable_unpacking(node) self.generic_visit(node)
Example #22
Source File: iterables.py From wemake-python-styleguide with MIT License | 5 votes |
def _check_unnecessary_iterable_unpacking(self, node: ast.Starred) -> None: parent = get_parent(node) if isinstance(parent, self._upackable_iterable_parent_types): if len(getattr(parent, 'elts', [])) == 1: self.add_violation(IterableUnpackingViolation(node))
Example #23
Source File: classes.py From wemake-python-styleguide with MIT License | 5 votes |
def _slot_item_name(self, node: ast.AST) -> Optional[str]: if isinstance(node, ast.Str): return node.s if isinstance(node, ast.Starred): return source.node_to_string(node) return None
Example #24
Source File: operators.py From wemake-python-styleguide with MIT License | 5 votes |
def unwrap_starred_node(node: ast.AST) -> ast.AST: """Unwraps the unary ``*`` starred node.""" if isinstance(node, ast.Starred): return node.value return node
Example #25
Source File: call_args.py From wemake-python-styleguide with MIT License | 5 votes |
def get_starred_args(call: ast.Call) -> Iterator[ast.Starred]: """Gets ``ast.Starred`` arguments from ``ast.Call``.""" for argument in call.args: if isinstance(argument, ast.Starred): yield argument
Example #26
Source File: function_args.py From wemake-python-styleguide with MIT License | 5 votes |
def _has_same_vararg( node: types.AnyFunctionDefAndLambda, call: ast.Call, ) -> bool: """Tells whether ``call`` has the same vararg ``*args`` as ``node``.""" vararg_name: Optional[str] = None for starred_arg in get_starred_args(call): # 'args': [<_ast.Starred object at 0x10d77a3c8>] if isinstance(starred_arg.value, ast.Name): vararg_name = starred_arg.value.id else: # We can judge on things like `*[]` return False if vararg_name and node.args.vararg: return node.args.vararg.arg == vararg_name return node.args.vararg == vararg_name # type: ignore
Example #27
Source File: function_args.py From wemake-python-styleguide with MIT License | 5 votes |
def _has_same_args( # noqa: WPS231 node: types.AnyFunctionDefAndLambda, call: ast.Call, ) -> bool: """ Tells whether ``call`` has the same positional args as ``node``. On ``python3.8+`` also works with ``posonlyargs`` arguments or ``/`` arguments as they also known. """ node_args = _get_args_without_special_argument(node) paired_arguments = zip_longest(call.args, node_args) for call_arg, func_arg in paired_arguments: if isinstance(call_arg, ast.Starred): # nevertheless `*args` is vararg ensure there is no # plain arg defined on corresponding position if isinstance(func_arg, ast.arg): return False elif isinstance(call_arg, ast.Name): # for each found call arg there should be not null # same func arg defined on the same position if not func_arg or call_arg.id != func_arg.arg: return False else: return False return True
Example #28
Source File: astwalker.py From pycodesuggest with MIT License | 5 votes |
def check_and_replace(self, candidate, typenames=None, scope=None, class_scope=False): if typenames is None: typenames = self.scope_variables if scope is None: scope = self.scope if isinstance(candidate, ast.Name) or isinstance(candidate, ast.Attribute): self.queue.append((scope, typenames, candidate, class_scope)) elif hasattr(candidate, "elts"): for elt in candidate.elts: self.check_and_replace(elt, typenames=typenames, scope=scope) elif isinstance(candidate, ast.Starred): self.check_and_replace(candidate.value, typenames=typenames, scope=scope)
Example #29
Source File: test_ast.py From Fluid-Designer with GNU General Public License v3.0 | 5 votes |
def test_starred(self): left = ast.List([ast.Starred(ast.Name("x", ast.Load()), ast.Store())], ast.Store()) assign = ast.Assign([left], ast.Num(4)) self.stmt(assign, "must have Store context")
Example #30
Source File: _analyze.py From myhdl with GNU Lesser General Public License v2.1 | 5 votes |
def visit_Call(self, node): # ast.Call signature changed in python 3.5 # http://greentreesnakes.readthedocs.org/en/latest/nodes.html#Call if sys.version_info >= (3, 5): starargs = any(isinstance(arg, ast.Starred) for arg in node.args) kwargs = any(kw.arg is None for kw in node.keywords) else: starargs = node.starargs is not None kwargs = node.kwargs is not None if starargs: self.raiseError(node, _error.NotSupported, "extra positional arguments") if kwargs: self.raiseError(node, _error.NotSupported, "extra named arguments") self.generic_visit(node)