Skip to main content

Transform code using the power of the Python AST

Project description


Apply structured migrations to existing python source code easily.

This is intended to be used to migrate from older code patterns to modern supported ones.

For library authors this can assist in providing migration tools so that users can migrate to newer versions of your library more easily.

ast-refactor will modify your source code if you run it, so be sure to run it on a repository that doesn't have code that is not checked in.


When dealing with large codebases it is common to encounter code that is using libraries in ways that are deprecated (or removed) in newer versions of those libraries.

For example you have the following code that uses an older version of pandas (<0.17)

df = pd.DataFrame({"A": ["foo", "foo", "foo", "foo", "foo",
                          "bar", "bar", "bar", "bar"],
                    "B": ["one", "one", "one", "two", "two",
                          "one", "one", "two", "two"],
                    "C": ["small", "large", "large", "small",
                          "small", "large", "small", "small",
                    "D": [1, 2, 2, 3, 3, 4, 5, 6, 7],
                    "E": [2, 4, 5, 5, 6, 6, 8, 9, 9]})

table = (pd
        rows=['A', 'B'], 

The second statement uses two pandas functions with deprecated keyword arguments. You can obviously fix this by hand, but if you have lots of code this is tedious and error prone.

ast-refactor provides you the tools to automatically convert that second statement into something that will work for modern versions of pandas.

table = (pd
        index=['A', 'B'], 

This is intended as a tool to help library author and owners of large codebases migrate source code easily.


For detailed usage documentation see usage docs and writing a migrator and

as a python cli

The easiest way to install this tool is using pipx.

$ pipx install \

$ ast-refactor run some/path/or/file

as a docker container

Alternatively you can run it from a docker container

$ docker run \
    -e UID=$(id -u) \
    -e GID=$(id -u) \
    -v /some/path/or/file:/work \
    flatironhealth/ast-refactor \
    run \



$ pip install .
$ ast-refactor some/path/or/file


docker build -t ast-refactor .

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ast-refactor-0.1.0.tar.gz (7.5 kB view hashes)

Uploaded source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page