Closed ngie-eign closed 4 months ago
This code snippet was based in part on the Best Practices doc.
Ok, I figured it out.
The core problem was that I used the libcst.EmptyLine
class instead of creating an instance of libcst.EmptyLine
and mutating that. Once I did that and addressed the other issue with directly setting the .comment
attribute, things just worked.
I can definitely tell that this heat wave/climate change is mucking with my cognitive abilities... sighs
import libcst
input_str = open(__file__).read()
py_module = libcst.parse_module(input_str)
copyright_line = libcst.EmptyLine().with_changes(
comment=libcst.Comment(value="# A new comment"),
)
new_py_module = py_module.with_changes(
body=(copyright_line, *py_module.body, )
)
output = new_py_module.code
You can also just libcst.EmptyLine(comment=libcst.Comment(value="# A new comment"))
Note: All potentially sensitive information has been removed.
Repro
More details
libcst_crashes.py.txt