-
Notifications
You must be signed in to change notification settings - Fork 188
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Dump CST to .dot (graphviz) files #1147
Dump CST to .dot (graphviz) files #1147
Conversation
…so that other libraries may provide their own custom representation of LibCST graphs.
… docstrings & tests), in libcst.helpers
# Conflicts: # libcst/helpers/node_fields.py # libcst/helpers/tests/test_node_fields.py # libcst/tool.py
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #1147 +/- ##
==========================================
+ Coverage 91.15% 91.16% +0.01%
==========================================
Files 258 262 +4
Lines 26689 26784 +95
==========================================
+ Hits 24328 24418 +90
- Misses 2361 2366 +5 ☔ View full report in Codecov by Sentry. |
I like the idea of visualizing the CST, I'd definitely like that to be part of LibCST itself. I think static representations (like a text dump or even an image produced by graphviz) have the same general limitation: they quickly get too large on any real world Python code. As for this specific PR, I'm happy to merge it if you spend the energy cleaning it up! I think it'd make sense for the dot output to be a flag to the existing |
About the PR
Done Also, did I break CI Coverage in a way ? About the rest
Entirely true.
This is awesome ! If I may add a little something, it would be great if nodes could be collapsed, to be able to focus on essential parts (though I don't know how hard it is to implement) |
PR looks great, thank you!
No, the codecov integration in this repo is barely limping along. I haven't had time to investigate yet
Thanks <3
It's using a very old version of libcst (0.3.x) and I don't really want to mislead people. I've built libcst binary wheels for pyodide now, so when pyscript upgrades its pyodide version we'll be able to use a newer version of libcst, and then we can include it in the docs. Likely a couple of months out.
Agreed. File an issue at https://github.com/aleivag/libcst-sandbox ? |
Summary
Currently, the way to "show" a LibCST's CST is to print it out, using
libcst.tool.dump
.This is great when working with LibCST on a day to day basis.
However, there are currently no ways to "show" a CST to people not involved with LibCST.
For instance, in LibCST conferences, or when working on educational material, or when pitching LibCST, there is nothing to "display".
This PR adds a way to dump a CST in a .dot file (or stdout), which can be used in graphviz to "show" the CST.
Examples
When dumping the CST from
fn(1, 2)
- just like LibCST doc, withdump_graphviz
:When dumping
fn(1, 2)
- without any filters:Note that this just produces the .dot files, Graphviz still needs to be called after to produce the visual graphs. I did not want to add a direct dependency between LibCST and Graphviz.
Changes
This PR adds the
libcst.display
module, with:libcst.display.text
which contains the oldlibcst.tool.dump
implementationlibcst.display.graphviz
whichlibcst.tool.dump
has been updated to importlibcst.display.text.dump
(legacy reasons), and can also call "print-graphviz" if used from command line.Still TODO
This is still a prototype, and I am not sure if you would consider it to be part LibCST, or a side library.
If this PR is OK to be part of LibCST, I will go on with code cleanup, tests and documentation.