google-pasta

raw JSON →
0.2.0 verified Tue May 12 auth: no python install: verified quickstart: draft maintenance

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.

pip install google-pasta
error pasta.base.annotate.AnnotationError: Expected 'return' but found 'async'
cause The `google-pasta` library, particularly version 0.2.0, has limitations in parsing modern Python syntax, including `async/await` keywords, leading to an `AnnotationError` when encountering such constructs.
fix
Avoid using google-pasta for code containing async/await syntax, or manually refactor such code. The library's development is slow, and full support for newer Python features may not be available in older versions.
error ModuleNotFoundError: No module named 'google.pasta'
cause This error occurs when the `google-pasta` library is not installed or not accessible in the current Python environment.
fix
Install the library using pip: pip install google-pasta.
error Changing the indentation level of a block of code is not supported.
cause This is a known limitation of the `google-pasta` library, meaning it cannot automatically adjust or refactor code blocks by changing their indentation.
fix
Manually adjust indentation levels in your code if refactoring requires such changes, as google-pasta is not designed to handle this operation.
error Failure to parse f-strings when field has = at the end.
cause The `google-pasta` library may encounter parsing issues with f-strings that contain an equals sign (`=`) at the end of a field, indicating a limitation with specific f-string syntax.
fix
Refactor f-strings that exhibit this specific pattern to use alternative formatting methods or simpler f-string expressions that avoid the problematic syntax.
gotcha The library operates under the assumption that the Python version for which the code is written is the same as the Python version running `pasta`. This is due to its reliance on the built-in `ast.parse` module, which can have version-specific behaviors.
fix Ensure your environment's Python version matches the target code's Python version. Be cautious with newer Python syntax features (e.g., f-strings) if `pasta`'s development predates their full support.
gotcha Changing indentation levels of code blocks is not directly supported, which can limit certain refactoring operations like extracting methods.
fix Be aware of this limitation when designing refactoring tools. For transformations requiring indentation changes, post-processing with a linter/formatter might be necessary.
gotcha Some Python features, such as the `global` keyword, are not fully supported, which can lead to incorrect transformations or parsing errors.
fix Test `pasta` thoroughly with codebases containing these unsupported features before relying on it for critical refactoring tasks.
gotcha The project's PyPI status is '2 - Pre-Alpha', indicating it is still experimental and not considered stable or production-ready by its maintainers.
fix Use with caution and thorough testing. Do not rely on it for mission-critical applications without extensive validation of its output.
gotcha Development is slow or stalled. The last PyPI release (0.2.0) was in March 2020, and the last reported code push activity was around late 2020. Open issues exist for modern Python syntax (e.g., f-strings parsing failures, Python 3.10 support), suggesting potential compatibility problems with newer Python versions.
fix Verify functionality with your specific Python version and syntax before use. Consider contributing to the project if you encounter issues or require newer feature support.
python os / libc status wheel install import disk
3.10 alpine (musl) wheel - 0.02s 18.3M
3.10 alpine (musl) - - 0.02s 18.3M
3.10 slim (glibc) wheel 1.6s 0.01s 19M
3.10 slim (glibc) - - 0.01s 19M
3.11 alpine (musl) wheel - 0.03s 20.3M
3.11 alpine (musl) - - 0.04s 20.3M
3.11 slim (glibc) wheel 1.6s 0.03s 21M
3.11 slim (glibc) - - 0.03s 21M
3.12 alpine (musl) wheel - 0.02s 12.2M
3.12 alpine (musl) - - 0.03s 12.2M
3.12 slim (glibc) wheel 1.7s 0.03s 13M
3.12 slim (glibc) - - 0.03s 13M
3.13 alpine (musl) wheel - 0.03s 11.9M
3.13 alpine (musl) - - 0.03s 11.8M
3.13 slim (glibc) wheel 1.6s 0.03s 12M
3.13 slim (glibc) - - 0.03s 12M
3.9 alpine (musl) wheel - 0.02s 17.8M
3.9 alpine (musl) - - 0.02s 17.8M
3.9 slim (glibc) wheel 1.8s 0.02s 18M
3.9 slim (glibc) - - 0.02s 18M

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}")