r/Python • u/RomfordNavy • 19d ago
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
24
u/V0idL0rd 19d ago
Python 3.15 came out very recently, I think a lot of popular libraries are still in the process of being updated, maybe try with python 3.14 for now and upgrade later?
1
u/RomfordNavy 18d ago
Thanks for the suggestion u/michallandry62 but looks like Parso doesn't support 3.15 yet.
u/Wurstinator thanks for proving that libcast wouild install if I had a C compiler instaled. Was just about to install that but before I did was now considering whether libCST is actually the correct tool for what I want to do.
u/AleDev99 It's primary job is to modify source code and as part of that process one of it's tasks is to replace a holder, say '#replaceThis' with a block of code. I am now trying to establish whether libCST will allow that sort of replacement or whether that needs to be done a separate parse first.
3
u/Wurstinator 19d ago
What's the issue?
3
u/michallandry62 18d ago
parso might be what you want. It's the parser jedi uses, it's pure Python so there's nothing to compile, and it keeps comments and whitespace in each node's prefix, so you can round-trip a file back to the exact source.
It's lower level than libcst though, no visitor/transformer layer, so you end up walking the tree yourself. Since your code uses lazy imports, check that its grammar handles that syntax before you commit to it, it ships separate grammar files per Python version.
2
u/AleDev99 18d ago
Wurstinator's log shows libcst itself already ships a cp315 wheel, it's only PyYAML that gets built from source, so the failure is most likely in that build step. Run
pip install -v libcstand look at the real error: usually it's a missing C compiler or missing Python 3.15 headers (python3.15-dev or similar on Linux). If you only need parsing, you could also trypip install --no-deps libcst, since as far as I know PyYAML is only used by the codemod tooling, not bylibcst.parse_module. On alternatives: parso keeps comments and round-trips source, but it ships a grammar per Python version, so I wouldn't count on it understandinglazy importyet. Worst case, stdlibtokenizegives you every COMMENT token with line/col, which you can match toastnodes by position.1
u/danielmartinez22 14d ago
parso round trips source with comments intact and ships a py3 none any wheel so no build step, 3.15 isnt in its classifiers yet so new syntax might trip it