A concrete syntax tree with AST-like properties for Python 3.0 through 3.13 programs.
Project description
A Concrete Syntax Tree (CST) parser and serializer library for Python
LibCST parses Python 3.0 -> 3.12 source code as a CST tree that keeps all formatting details (comments, whitespaces, parentheses, etc). It’s useful for building automated refactoring (codemod) applications and linters.
LibCST creates a compromise between an Abstract Syntax Tree (AST) and a traditional Concrete Syntax Tree (CST). By carefully reorganizing and naming node types and fields, we’ve created a lossless CST that looks and feels like an AST.
You can learn more about the value that LibCST provides and our motivations for the project in our documentation. Try it out with notebook examples.
Example expression:
1 + 2
CST representation:
BinaryOperation(
left=Integer(
value='1',
lpar=[],
rpar=[],
),
operator=Add(
whitespace_before=SimpleWhitespace(
value=' ',
),
whitespace_after=SimpleWhitespace(
value=' ',
),
),
right=Integer(
value='2',
lpar=[],
rpar=[],
),
lpar=[],
rpar=[],
)
Getting Started
Examining a sample tree
To examine the tree that is parsed from a particular file, do the following:
python -m libcst.tool print <some_py_file.py>
Alternatively, you can import LibCST into a Python REPL and use the included parser and pretty printing functions:
>>> import libcst as cst >>> from libcst.tool import dump >>> print(dump(cst.parse_expression("(1 + 2)"))) BinaryOperation( left=Integer( value='1', ), operator=Add(), right=Integer( value='2', ), lpar=[ LeftParen(), ], rpar=[ RightParen(), ], )
For a more detailed usage example, see our documentation.
Installation
LibCST requires Python 3.9+ and can be easily installed using most common Python packaging tools. We recommend installing the latest stable release from PyPI with pip:
pip install libcst
For parsing, LibCST ships with a native extension, so releases are distributed as binary wheels as well as the source code. If a binary wheel is not available for your system (Linux/Windows x86/x64 and Mac x64/arm are covered), you’ll need a recent Rust toolchain for installing.
Further Reading
Development
You’ll need a recent Rust toolchain for developing.
We recommend using hatch <https://hatch.pypa.io/> for running tests, linters, etc.
Then, start by setting up and building the project:
git clone git@github.com:Instagram/LibCST.git libcst
cd libcst
hatch env create
To run the project’s test suite, you can:
hatch run test
You can also run individual tests by using unittest and specifying a module like this:
hatch run python -m unittest libcst.tests.test_batched_visitor
See the unittest documentation for more examples of how to run tests.
We have multiple linters, including copyright checks and slotscheck to check the correctness of class __slots__. To run all of the linters:
hatch run lint
We use ufmt to format code. To format changes to be conformant, run the following in the root:
hatch run format
Building
In order to build LibCST, which includes a native parser module, you will need to have the Rust build tool cargo on your path. You can usually install cargo using your system package manager, but the most popular way to install cargo is using rustup.
To build just the native parser, do the following from the native directory:
cargo build
To rebuild the libcst.native module, from the repo root:
hatch env prune && hatch env create
Type Checking
We use Pyre for type-checking.
To verify types for the library, do the following in the root:
hatch run typecheck
Generating Documents
To generate documents, do the following in the root:
hatch run docs
Future
Advanced full repository facts providers like fully qualified name and call graph.
License
LibCST is MIT licensed, as found in the LICENSE file.
Privacy Policy and Terms of Use
Acknowledgements
Guido van Rossum for creating the parser generator pgen2 (originally used in lib2to3 and forked into parso).
David Halter for parso which provides the parser and tokenizer that LibCST sits on top of.
Zac Hatfield-Dodds for hypothesis integration which continues to help us find bugs.
Zach Hammer improved type annotation for Mypy compatibility.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Hashes for libcst-1.5.0-cp313-cp313-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4d6acb0bdee1e55b44c6215c59755ec4693ac01e74bb1fde04c37358b378835d |
|
MD5 | 433395e6c2be8e24ef06fe57ccc5dcd1 |
|
BLAKE2b-256 | a09f5b5481d716670ed5fbd8d06dfa94b7108272b645da2f2406eb909cb6a450 |
Hashes for libcst-1.5.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d2788b2b5838b78fe15df8e9fa6b6903195ea49b2d2ba43e8f423f6c90e4b69f |
|
MD5 | a158890a8af7b19a0283f926b76aedda |
|
BLAKE2b-256 | c995b878c95af17f3e341ac5dc18e3160d45d86b2c05a0cafd866ceb0b766bbd |
Hashes for libcst-1.5.0-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d4592872aaf5b7fa5c2727a7d73c0985261f1b3fe7eff51f4fd5b8174f30b4e2 |
|
MD5 | aaaa5d0ae6029108d8c4b70ecb9ac589 |
|
BLAKE2b-256 | dc37ce62947fd7305fb501589e4b8f6e82e3cf61fca2d62392e281c17a2112f5 |
Hashes for libcst-1.5.0-cp313-cp313-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b5b5bcd3a9ba92840f27ad34eaa038acbee195ec337da39536c0a2efbbf28efd |
|
MD5 | 450be7cc55c8c397e4389944de4a7edc |
|
BLAKE2b-256 | e126697b54aa839c4dc6ea2787d5e977ed4be0636149f85df1a0cba7a29bd188 |
Hashes for libcst-1.5.0-cp313-cp313-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 208ea92d80b2eeed8cbc879d5f39f241582a5d56b916b1b65ed2be2f878a2425 |
|
MD5 | 44a3ebb904ef43092d68339957ecdc64 |
|
BLAKE2b-256 | 2c54f63bf0bd2d70179e0557c9474a0511e33e646d398945b5a01de36237ce60 |
Hashes for libcst-1.5.0-cp313-cp313-macosx_10_13_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 88520b6dea59eaea0cae80f77c0a632604a82c5b2d23dedb4b5b34035cbf1615 |
|
MD5 | 7828ba23e42ca899031090c9f089a8ae |
|
BLAKE2b-256 | 605edd156f628fed03a273d995008f1669e1964727df6a8818bbedaac51f9ae5 |
Hashes for libcst-1.5.0-cp312-cp312-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b48bf71d52c1e891a0948465a94d9817b5fc1ec1a09603566af90585f3b11948 |
|
MD5 | 0edec0a6efa0afaac58b33f6239bc161 |
|
BLAKE2b-256 | b1925cb62834eec397f4b3218c03acc28b6b8470f87c8dad9e9b0fd738c3948c |
Hashes for libcst-1.5.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7dba93cca0a5c6d771ed444c44d21ce8ea9b277af7036cea3743677aba9fbbb8 |
|
MD5 | 97a1398e4dcc2147bc388038789478d8 |
|
BLAKE2b-256 | 63c4e056f3f34642f294421bd4a4d4b40aeccaf153a456bcb4d7e54f4337143f |
Hashes for libcst-1.5.0-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 48e581af6127c5af4c9f483e5986d94f0c6b2366967ee134f0a8eba0aa4c8c12 |
|
MD5 | cf9029500de183562a97d598dfd967cf |
|
BLAKE2b-256 | dab71976377c19f9477267daac2ea8e2d5a72ce12d5b523ff147d404fb7ae74e |
Hashes for libcst-1.5.0-cp312-cp312-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 80b5c4d87721a7bab265c202575809b810815ab81d5e2e7a5d4417a087975840 |
|
MD5 | d9846c3ec89a154ed7633d6991b15542 |
|
BLAKE2b-256 | e8d6574fc6c8b0ca81586ee05f284ef6987730b841b31ce246ef9d3c45f17ec4 |
Hashes for libcst-1.5.0-cp312-cp312-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5f10124bf99a0b075eae136ef0ce06204e5f6b8da4596a9c4853a0663e80ddf3 |
|
MD5 | 0fc3636c2921a5d3bcb8253d318ee640 |
|
BLAKE2b-256 | 48ec4a1a34c3dbe6d51815700a0c14991f4124f10e82f9959d4fb5a9b0b06c74 |
Hashes for libcst-1.5.0-cp312-cp312-macosx_10_13_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 83bc5fbe34d33597af1d5ea113dcb9b5dd5afe5a5f4316bac4293464d5e3971a |
|
MD5 | f6c573b45d153cda36f14d6f47d718fd |
|
BLAKE2b-256 | a7239cdb3362ad75490108a03abeaae8d7f7fb0d86586d806102ae9d9690d6b8 |
Hashes for libcst-1.5.0-cp311-cp311-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 52b6aadfe54e3ae52c3b815eaaa17ba4da9ff010d5e8adf6a70697872886dd10 |
|
MD5 | 28152262a657633173e572215765bd39 |
|
BLAKE2b-256 | 6586ddf0d593f4ef5994f456e00e99a1eb28b661aab5df960034199f4d8bbeb4 |
Hashes for libcst-1.5.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 02be4aab728261bb76d16e77c9a457884cebb60d09c8edee844de43b0e08aff7 |
|
MD5 | bb4cc3f839ec41dc96584cca0bdd3efe |
|
BLAKE2b-256 | e888016b3feb75a3b16896e27691439c3bd493ae7d896bb4e31d6bd4c2e5c20b |
Hashes for libcst-1.5.0-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | fc80ea16c7d44e38f193e4d4ef7ff1e0ba72d8e60e8b61ac6f4c87f070a118bd |
|
MD5 | 35dfbde1e5107c93d2641eab4dff0939 |
|
BLAKE2b-256 | ebad10cffc6a69da4320cc75f7f031a48292b61ad5ba0ba94fa9f963cb0b5f67 |
Hashes for libcst-1.5.0-cp311-cp311-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a8fcd78be4d9ce3c36d0c5d0bdd384e0c7d5f72970a9e4ebd56070141972b4ad |
|
MD5 | 13d8d5af250c11749a5f710b954323f3 |
|
BLAKE2b-256 | 698e5a60d53493e259743fd574abe442dd7f3b497ebb58dee168473a03f90d3e |
Hashes for libcst-1.5.0-cp311-cp311-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8935dd3393e30c2f97344866a4cb14efe560200e232166a8db1de7865c2ef8b2 |
|
MD5 | fdcbd61d034ec7550a4c05dd929cf9f1 |
|
BLAKE2b-256 | 174dc6ed4323e77717edf3f47af8cabbdd4a7de7983fc5a1cc20130947f65f9d |
Hashes for libcst-1.5.0-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 585b3aa705b3767d717d2100935d8ef557275ecdd3fac81c3e28db0959efb0ea |
|
MD5 | 44b5c6c2c4abfadffd5ff685d3903ec0 |
|
BLAKE2b-256 | b5825b9d1f89bdba4106de6080ab3384157581af4f0b94e04a7150b917b5b945 |
Hashes for libcst-1.5.0-cp310-cp310-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9cccfc0a78e110c0d0a9d2c6fdeb29feb5274c9157508a8baef7edf352420f6d |
|
MD5 | c93366faae7ee63ae6b7b8564d655cdd |
|
BLAKE2b-256 | 24fbdb7c696b7bf8e295aa9bf37091fbd1bad35e491be44926da2b20907c3452 |
Hashes for libcst-1.5.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2d5978fd60c66794bb60d037b2e6427ea52d032636e84afce32b0f04e1cf500a |
|
MD5 | 3bc0bd65a647b70a9c5530c42c8fd35c |
|
BLAKE2b-256 | 8798d47a9a88df48cc33db7e1219cd7c29bfdfd8d695634f3f2e86ff04bbd58d |
Hashes for libcst-1.5.0-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 96adc45e96476350df6b8a5ddbb1e1d6a83a7eb3f13087e52eb7cd2f9b65bcc7 |
|
MD5 | 27f2dcd8d8eee701b7c54f2aa7550120 |
|
BLAKE2b-256 | 7419d2ebded5990f2f5ab4c86412df75338b9d8b386fbb5e430669f287bc8d9c |
Hashes for libcst-1.5.0-cp310-cp310-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d6502aeb11412afc759036160c686be1107eb5a4466db56b207c786b9b4da7c4 |
|
MD5 | cac83b348f417afd984cea6277f8fa8a |
|
BLAKE2b-256 | b8ca7fdcbab8f8e8c46336099af7929d0f0e5873222830010aae0160d16544c1 |
Hashes for libcst-1.5.0-cp310-cp310-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d92c5ae2e2dc9356ad7e3d05077d9b7e5065423e45788fd86729c88729e45c6e |
|
MD5 | 3e9b3a71ed64f3b1ea960eca667787a0 |
|
BLAKE2b-256 | 20d53d5819da92a8f997ecf0b5a77d65865d4d2aa4209b34e32835b555218689 |
Hashes for libcst-1.5.0-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 23d0e07fd3ed11480f8993a1e99d58a45f914a711b14f858b8db08ae861a8a34 |
|
MD5 | b279a3f811d2c924dd6c14617a674c4b |
|
BLAKE2b-256 | 8544c8f1e0d83bbdabc240c05d5bedddfd4e095a0031b8df473d8eb004f12554 |
Hashes for libcst-1.5.0-cp39-cp39-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 99e7c52150a135d66716b03e00c7b1859a44336dc2a2bf8f9acc164494308531 |
|
MD5 | 36b7ef4b53eb04eebb4131c172321eb9 |
|
BLAKE2b-256 | ca96a3b9dd3dd72ef0c80ceccb3beeb57e12eaa883cf6b23f440bd239c1e5392 |
Hashes for libcst-1.5.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b60b09abcc2848ab52d479c3a9b71b606d91a941e3779616efd083bb87dbe8ad |
|
MD5 | 577442c551b5e478be512e1132db4d6f |
|
BLAKE2b-256 | 2b639aaaaff7c9427d5d2656482f77b79b0e3f36f40cd2856011b3d394c22d4d |
Hashes for libcst-1.5.0-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4f71aed85932c2ea92058fd9bbd99a6478bd69eada041c3726b4f4c9af1f564e |
|
MD5 | 870abb1547ce61bd1da51062dd6bd011 |
|
BLAKE2b-256 | 533d2c31bde9716ea732d1c939deaf2367c2202a1c124cc3949e90d9be1fbc54 |
Hashes for libcst-1.5.0-cp39-cp39-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6fb324ed20f3a725d152df5dba8d80f7e126d9c93cced581bf118a5fc18c1065 |
|
MD5 | f725e235017b8e370d56db81614bb9fc |
|
BLAKE2b-256 | e46128a9352d0f3ec48a320b92a8e25064f8ed834fb268f0071c51f7185c0165 |
Hashes for libcst-1.5.0-cp39-cp39-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 40748361f4ea66ab6cdd82f8501c82c29808317ac7a3bd132074efd5fd9bfae2 |
|
MD5 | 3e8bf6271b12d5643bd6c36ff92b001f |
|
BLAKE2b-256 | af2d1c5a8da004ef26681d84f491f1058855a6e430f861422a24c5b95d8b70be |
Hashes for libcst-1.5.0-cp39-cp39-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6453b5a8755a6eee3ad67ee246f13a8eac9827d2cfc8e4a269e8bf0393db74bc |
|
MD5 | 3c813f5448c2029102d6aa457e8152b8 |
|
BLAKE2b-256 | e6514d5579bd792661a5fc88fc916c0ef63f45832cec28f08dd633de64ccfed7 |