google-pasta

0.2.0 · maintenance · verified Sun Mar 29

google-pasta is an AST-based Python refactoring library (version 0.2.0). It aims to enable robust Python source code refactoring through Abstract Syntax Tree (AST) modifications, useful for tasks like renaming modules, rewriting import statements, enforcing code style, or safely migrating code between APIs. The library is currently marked as 'Pre-Alpha' on PyPI and has a slow release cadence, with the last update in March 2020.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates the core functionality of `google-pasta`: parsing Python source code into an AST and then dumping it back into a string. A key design goal is 'symmetry', meaning `pasta.dump(pasta.parse(src))` should equal the original source code.

import pasta

# Original source code string
original_code = """
def greet(name):
    print(f"Hello, {name}!")

greet("World")
"""

# Parse the source code into an AST
ast_tree = pasta.parse(original_code)

# (Optional) Modify the AST here, for example, renaming a function
# For simplicity, we're just parsing and dumping to demonstrate symmetry

# Dump the AST back into source code
reconstructed_code = pasta.dump(ast_tree)

# Verify symmetry (as per pasta's design goals)
assert original_code == reconstructed_code

print("Original Code:\n" + original_code)
print("Reconstructed Code:\n" + reconstructed_code)
print(f"Code matches after parse and dump: {original_code == reconstructed_code}")

view raw JSON →