3 ms·
They address that with a bit of a hack. > One of the problems of patching code using ast package of Python is that it loses all the formatting and comments of
by ianlevesque 5y ago
They address that with a bit of a hack.
> One of the problems of patching code using ast package of Python is that it loses all the formatting and comments of the original source code. This can be solved by making the patch script a little smarter. Instead of having it unparse the entire patched AST and write that to disk, we can make it unparse only the nodes it modified and insert the modified code at the corresponding line number in the file. The ast nodes have lineno attribute that can be used to retrieve the line number of the file to be injected the patched code with.