Looking at the ast.dump  output, we can see that the head  object which is of type Module  has an attribute body  whose value is a list of 2 nodes 

Abstract Syntax Tree for Patching Code and Assessing Code Quality | Soroco Engineering

submited by
Style Pass
2021-06-07 05:30:06

Looking at the ast.dump output, we can see that the head object which is of type Module has an attribute body whose value is a list of 2 nodes – one representing var = 1 and the other representing print(var) . The first node representing var = 1 has a target attribute representing the LHS var and a value attribute representing the RHS 1 . Let’s see if we can print the RHS.

Now that we understand ASTs and how to generate them, inspect them, modify them and re-create code from them, let’s go back to the problem of writing patch scripts to modify the code of a system to use pandas 1.0.0 instead of pandas 0.25.x. We call these AST based patch scripts as “IntelliPatch”.

One can extend the patch script to take care of all backward incompatibilities in pandas 1.0.0. And then write an outer function that goes through every Python file of a system, reads its code, patches it and writes it back to disk.

It is important to note that a developer should review the changes done by the IntelliPatch before committing it. For example, if code is hosted on git , then a git diff should be performed and reviewed by the developer.

Leave a Comment