Skip to main content

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

Project description

LibCST

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

Documentation CircleCI CodeCov PYPI Notebook

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

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

Future

  • Extension to Matchers API which incorporates metadata on nodes.

  • Addition to Matchers API which allows for operations such as extract/find/replaceall, similar to the RE module.

  • Additional layer providing command-line frontend for executing refactors.

  • More metadata providers from deep static analysis including variable fully qualified name, variable type annotation, etc.

License

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

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.

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.2.0.tar.gz (275.3 kB view details)

Uploaded Source

Built Distribution

libcst-0.2.0-py3-none-any.whl (341.0 kB view details)

Uploaded Python 3

File details

Details for the file libcst-0.2.0.tar.gz.

File metadata

  • Download URL: libcst-0.2.0.tar.gz
  • Upload date:
  • Size: 275.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/39.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.3

File hashes

Hashes for libcst-0.2.0.tar.gz
Algorithm Hash digest
SHA256 0a3c189bcbb71dbf77c55556f5302f4c615d6211de92bf60ffb5512a89fd39f9
MD5 1d5639bd0b82ec9ab43b7602979f00a6
BLAKE2b-256 7c27f6ccfcde60d3c8603254f639683ee727fdf4009412f9e24d4834979f6fd6

See more details on using hashes here.

Provenance

File details

Details for the file libcst-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: libcst-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 341.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/39.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.3

File hashes

Hashes for libcst-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1c61c522c0d47998eaf209e2cab454a0a68c7e3f102a8b88de15f1c81fc87976
MD5 cc7ab0ca52a5787e4b779499acb790e1
BLAKE2b-256 8136da5ae7d09dc1161a4325e125f92c0b8a32f89901e15603a7deac19afce85

See more details on using hashes here.

Provenance

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