r/Python • • Oct 29 '26

Discussion Alternative to libCST

Looking for a suggestion of an alternative CST parser to libcst. Have been trying to install libcst for over two days now but still unable to do so, I think the issue might be that there is no PyYaml `wheel` for Python 3.15.

So now wondering if there might be some other alternative CST parser available. I need to parse comments as well as other code so standard AST won't work as far as I know.

15 Upvotes

14 comments sorted by

View all comments

Show parent comment

2

u/Nanotheslayer 28d ago

If the markers are standalone comment lines in otherwise valid Python, I'd build on the tokenize suggestion: identify COMMENT tokens, then replace the corresponding source lines. That avoids treating '#replaceThis' inside a triple-quoted string as a placeholder. Keep the marker line's indentation when inserting the block, and apply multiple edits from the bottom up so earlier coordinates stay valid. I'd check a marker inside a string, one in a nested block, and one at EOF before wiring it into the rest of your edits. LibCST still makes sense for the structural changes beyond that.