# 打印树节点 for node in tree.body: if isinstance(node, ast.Assign): print(f"Assignment: {node.targets}") elif isinstance(node, ast.AnnAssign): print(f"Annotation: {node.targets}") elif isinstance(node, ast.Import): print(f"Import: {node.module}")
# 打印注释 comments = [co.comments[0] for co in ast.walk(tree)] for comment in comments: print(f"{comment.body.strip()}")
# 打印错误信息 errors = [] for usage in ast.walk(tree): if isinstance(usage, ast.Import): errors.append(f"{usage.module} is imported but not defined") elif isinstance(usage, ast.ImportFrom): errors.append(f"{usage.module} is imported from but not defined")
if errors: print("\nErrors:") for error in errors: print(f"{error}")