Skip to content
A concrete syntax tree parser and serializer library for Python that preserves many aspects of Python's abstract syntax tree
Branch: master
Clone or download
Type Name Latest commit message Commit time
Failed to load latest commit information.
.circleci run pyre codegen in CI Jan 2, 2020
.github add initial Jul 23, 2019
docs/source [doc] add get_full_name_for_node_or_raise Feb 12, 2020
libcst Use helpers in various codemod transforms. Feb 11, 2020
stubs Upgrade Pyre to 0.32. Oct 4, 2019
.editorconfig Add config files to make tools easier to use Jul 23, 2019
.flake8 Remove Instagram-specific bits from flake8 config Jul 23, 2019
.gitattributes Add the logo to the docs/source/_static/ directory Aug 5, 2019
.gitignore Fuzz with Hypothesmith Aug 22, 2019
.pyre_configuration add pyre CI job Jul 23, 2019
.readthedocs.yml [readthedoc] install libcst Aug 7, 2019
.watchmanconfig Fix some typos and add watchman config for pyre incremental check Sep 16, 2019 Bump LibCST to new release 0.3.1. Feb 6, 2020 add Code Of Conduct file Jul 23, 2019 add test command and rephrase coding style. Jul 23, 2019
LICENSE Clarify LibCST license. Sep 27, 2019 support mypy using py.typed Sep 16, 2019
README.rst Implement parsing of posonly param indicator ('/'). This brings us to… Jan 17, 2020
codecov.yml [CI] move test coverage job as a separate job (#48) Aug 27, 2019
pyproject.toml Add config files to make tools easier to use Jul 23, 2019
requirements-dev.txt Upgrade Pyre to 0.0.41 to hopefully resolve issue with installation. Jan 28, 2020
requirements.txt Add a config file format to allow customization of LibCST codemod run… Dec 12, 2019 Add a --version flag to `libcst.tool`, single-source version. Feb 7, 2020
tox.ini Hook up 3.8 to various tox environments. Oct 24, 2019



A Concrete Syntax Tree (CST) parser and serializer library for Python

Documentation CircleCI CodeCov PYPI Notebook

LibCST parses Python 3.5, 3.6, 3.7 or 3.8 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.

1 + 2
            value=' ',
            value=' ',

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 <>

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)")))

For a more detailed usage example, see our documentation.


LibCST requires Python 3.6+ 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

Further Reading


Start by setting up and activating a virtualenv:

git clone libcst
cd libcst
python3 -m venv ../libcst-env/  # just an example, put this wherever you want
source ../libcst-env/bin/activate
pip install --upgrade pip  # optional, if you have an old system version of pip
pip install -r requirements.txt -r requirements-dev.txt
# If you're done with the virtualenv, you can leave it by running:

We use isort and black to format code. To format changes to be conformant, run the following in the root:

tox -e autofix

To run all tests, you'll need to install tox and do the following in the root:

tox -e py37

You can also run individual tests by using unittest and specifying a module like this:

python -m unitttest libcst.tests.test_batched_visitor

See the unittest documentation for more examples of how to run tests.

We use Pyre for type-checking. To verify types for the library, do the following in the root:

pyre check

To generate documents, do the following in the root:

tox -e docs


  • Advanced full repository facts providers like fully qualified name and call graph.


LibCST is MIT licensed, as found in the LICENSE file.


  • 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.
You can’t perform that action at this time.