def _get_assign_names(targets, load_names, store_names):
for target in targets:
orig_target = target
target = _get_ast_name_node(target)
if (isinstance(target, ast.Name)
and isinstance(target.ctx, ast.Store)):
# 'x = value': store name 'x'
store_names.add(target.id)
elif (isinstance(target, ast.Name)
and isinstance(target.ctx, ast.Load)):
# 'obj.attr = value': load name 'obj'
load_names.add(target.id)
elif isinstance(target, ast.Tuple):
# x, y = ...
_get_assign_names(target.elts, load_names, store_names)
elif isinstance(target, ast.Constant):
# '(1).__class__ = MyInt': it raises a TypeError
raise ComplexAssignment(orig_target)
elif isinstance(target, (ast.Dict, ast.List)):
# '{...}[key] = ...', '[...][index] = ...'
pass
elif isinstance(target, ast.Call):
# 'globals()[key] = value'
# 'type(mock)._mock_check_sig = checksig'
raise ComplexAssignment(orig_target)
else:
raise Exception("unsupported assign target: %s"
% ast.dump(target))
评论列表
文章目录