Registry / serialization / google-pasta

google-pasta

JSON →
library0.2.0pypypi✓ verified 52d ago

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.

serialization
pip install google-pasta
Install & Compatibility
Where this runs
tested against v0.2.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.925 runs
installs and imports cleanly · install 0.0s · import 0.025s · 18.3MB
glibc
py 3.103.925 runs
installs and imports cleanly · install 1.6s · import 0.023s · 19MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

parse
import pasta src = 'def foo(): pass' ast = pasta.parse(src)
dump
import pasta src = 'def foo(): pass' ast = pasta.parse(src) reconstructed_src = pasta.dump(ast)

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}")
Debug
Known issues
gotchaThe 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.
affects: All versions
gotchaChanging 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.
affects: All versions
gotchaSome 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.
affects: All versions
gotchaThe 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.
affects: All versions
gotchaDevelopment 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.
affects: Python 3.8+ (potentially)
Errors
Common errors & fixes
pasta.base.annotate.AnnotationError: Expected 'return' but found 'async'
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.
ModuleNotFoundError: No module named 'google.pasta'
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`.
Changing the indentation level of a block of code is not supported.
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.
Failure to parse f-strings when field has = at the end.
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.
Upgrade
Version history
0.2.0latest on PyPI
Audit
Dependencies

No dependency data recorded yet.

Agent activity
15 hits · last 30 days
node
6
seranking-bot
4
Amazon
2
ahrefsbot
2
Resources