def get_call_arg_values(parent, call_node, arg=None, kwarg=None, child=None):
"""Only returns literals."""
if not isinstance(call_node, ast.Call):
raise ValueError('call_node must be an ast.Call instance')
if arg is None and kwarg is None:
raise RuntimeError('either an arg or kwarg must be specified')
arg_node = None
if arg is not None:
if not isinstance(arg, int):
raise ValueError('arg must be specified as a 0-indexed argument position')
if arg < len(call_node.args):
arg_node = call_node.args[arg]
if arg_node is None and kwarg is not None:
if not isinstance(kwarg, str):
raise ValueError('kwarg must be specified as the string of a keyword argument name')
arg_node = next((kw.value for kw in call_node.keywords if kw.arg == kwarg), None)
if arg_node is None:
return
if not hasattr(arg_node, 'parent'):
arg_node.parent = call_node
for arg_value in iter_expr_literal_values(parent, arg_node, child=child):
yield arg_value
评论列表
文章目录