pasta: Python AST Augmentation

This is still a work-in-progress; there is much more to do. Existing functionality may not be perfect.

Mission

Enable python source code refactoring through AST modifications.

Sample use cases:

Design Goals

Python Version Support

Supports python 2.7 and up to 3.8.

Dependencies

pasta depends on six.

Basic Usage

import pasta
tree = pasta.parse(source_code)

# ... Augment contents of tree ...

source_code = pasta.dump(tree)

Built-in Augmentations

Pasta includes some common augmentations out-of-the-box. These can be used as building blocks for more complex refactoring actions.

There will be more of these basic augmentations added over time. Stay tuned!

Rename an imported name

Rewrites references to an imported name, module or package. For some more examples, see pasta/augment/rename_test.py.

# Rewrite references from one module to another
rename.rename_external(tree, 'pkg.subpkg.module', 'pkg.other_module')

# Rewrite references from one package to another
rename.rename_external(tree, 'pkg.subpkg', 'pkg.other_pkg')

# Rewrite references to an imported name in another module
rename.rename_external(tree, 'pkg.module.Query', 'pkg.module.ExecuteQuery')

Known issues and limitations

Developing

This project uses setuptools to facilitate testing and packaging.

# Run all tests
python setup.py test

# Run a single test suite
python setup.py test -s pasta.base.annotate_test.suite

Disclaimer

This is not an official Google product.