3 ms·
If someone wants to do something similar: it’s better to use the 2to3 machinery than ast as it can retain comments.
by the_mitsuhiko 5y ago
If someone wants to do something similar: it’s better to use the 2to3 machinery than ast as it can retain comments.
- ianlevesque 5y agoThey 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.
- avinassh 5y ago> 2to3 machinery Are you referring to this one - https://docs.python.org/3.9/library/2to3.html#to3-reference https://docs.python.org/3.9/library/2to3.html#to3-reference ?
- the_mitsuhiko 5y agoYes. The black formatter maintains a fork called blib2to3 which works better.