Skip to main content

A concrete syntax tree with AST-like properties for Python 3.5, 3.6, 3.7 and 3.8 programs.

Project description

LibCST

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

Documentation Github Actions CodeCov PYPI PYPI Download Notebook

LibCST parses Python 3.0, 3.1, 3.3, 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.

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

Development

Start by setting up and activating a virtualenv:

git clone git@github.com:Instagram/LibCST.git 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:
deactivate

We use ufmt 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 unittest libcst.tests.test_batched_visitor

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

We use Pyre for type-checking.

To set up pyre check environment:

  1. Copy the example Pyre config: cp .pyre_configuration.example .pyre_configuration.

  2. In the config file, add your venv site-packages dir to “search_path”. (e.g. add “/workspace/libcst-env/lib/python3.7/site-packages”) Note: venv dir must not be inside the libcst dir

  3. Remove installed LibCST and install from the source code:

pip uninstall -y libcst
pip install -e .

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

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

libcst-0.4.0a1.tar.gz (699.5 kB view hashes)

Uploaded Source

Built Distributions

libcst-0.4.0a1-cp310-cp310-win_amd64.whl (1.7 MB view hashes)

Uploaded CPython 3.10 Windows x86-64

libcst-0.4.0a1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.7 MB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

libcst-0.4.0a1-cp310-cp310-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (2.8 MB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

libcst-0.4.0a1-cp310-cp310-macosx_11_0_arm64.whl (1.8 MB view hashes)

Uploaded CPython 3.10 macOS 11.0+ ARM64

libcst-0.4.0a1-cp310-cp310-macosx_10_9_x86_64.whl (1.8 MB view hashes)

Uploaded CPython 3.10 macOS 10.9+ x86-64

libcst-0.4.0a1-cp39-cp39-win_amd64.whl (1.7 MB view hashes)

Uploaded CPython 3.9 Windows x86-64

libcst-0.4.0a1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.7 MB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

libcst-0.4.0a1-cp39-cp39-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (2.8 MB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

libcst-0.4.0a1-cp39-cp39-macosx_11_0_arm64.whl (1.8 MB view hashes)

Uploaded CPython 3.9 macOS 11.0+ ARM64

libcst-0.4.0a1-cp39-cp39-macosx_10_9_x86_64.whl (1.8 MB view hashes)

Uploaded CPython 3.9 macOS 10.9+ x86-64

libcst-0.4.0a1-cp38-cp38-win_amd64.whl (1.7 MB view hashes)

Uploaded CPython 3.8 Windows x86-64

libcst-0.4.0a1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.7 MB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

libcst-0.4.0a1-cp38-cp38-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (2.8 MB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

libcst-0.4.0a1-cp38-cp38-macosx_11_0_arm64.whl (1.8 MB view hashes)

Uploaded CPython 3.8 macOS 11.0+ ARM64

libcst-0.4.0a1-cp38-cp38-macosx_10_9_x86_64.whl (1.8 MB view hashes)

Uploaded CPython 3.8 macOS 10.9+ x86-64

libcst-0.4.0a1-cp37-cp37m-win_amd64.whl (1.7 MB view hashes)

Uploaded CPython 3.7m Windows x86-64

libcst-0.4.0a1-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.7 MB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.17+ x86-64

libcst-0.4.0a1-cp37-cp37m-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (2.8 MB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

libcst-0.4.0a1-cp37-cp37m-macosx_10_9_x86_64.whl (1.8 MB view hashes)

Uploaded CPython 3.7m macOS 10.9+ x86-64

libcst-0.4.0a1-cp36-cp36m-win_amd64.whl (1.7 MB view hashes)

Uploaded CPython 3.6m Windows x86-64

libcst-0.4.0a1-cp36-cp36m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.7 MB view hashes)

Uploaded CPython 3.6m manylinux: glibc 2.17+ x86-64

libcst-0.4.0a1-cp36-cp36m-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (2.8 MB view hashes)

Uploaded CPython 3.6m manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

libcst-0.4.0a1-cp36-cp36m-macosx_10_9_x86_64.whl (1.8 MB view hashes)

Uploaded CPython 3.6m macOS 10.9+ x86-64

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page