You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
Adam Johnson 5900a4ecd6
Add package links to PyPI (#688)
1 week ago
.cargo Implement a Python PEG parser in Rust (#566) 5 months ago
.github Drop support for running libcst using a python 3.6 interpreter (#663) 2 months ago
docs/source Fix license headers (#560) 5 months ago
libcst Restore the 0.4.1 behavior for libcst.helpers.get_absolute_module (#684) 2 weeks ago
native make sure ParserError's raw_line is zero-indexed (#681) 3 weeks ago
stubs fix copyright headers and add a script to check (#635) 4 months ago
.editorconfig Implement a Python PEG parser in Rust (#566) 5 months ago
.fixit.config.yaml [CI] add Fixit to tox -e lint (#386) 2 years ago
.flake8 Implement a Python PEG parser in Rust (#566) 5 months ago
.gitattributes Add the logo to the docs/source/_static/ directory 3 years ago
.gitignore Use pyre site-package feature (#589) 5 months ago
.pyre_configuration Use pyre site-package feature (#589) 5 months ago
.readthedocs.yml Update readthedocs config (#637) 4 months ago
.watchmanconfig Fix some typos and add watchman config for pyre incremental check 3 years ago
CHANGELOG.md bump version to 0.4.3 2 weeks ago
CODE_OF_CONDUCT.md Adopt Contributor Covenant CoC 4 months ago
CONTRIBUTING.md Remove tox references (#588) 5 months ago
LICENSE Fix license headers (#560) 5 months ago
MANIFEST.in Implement a Python PEG parser in Rust (#566) 5 months ago
README.rst Drop support for running libcst using a python 3.6 interpreter (#663) 2 months ago
check_copyright.sh fix copyright headers and add a script to check (#635) 4 months ago
codecov.yml [CI] move test coverage job as a separate job (#48) 3 years ago
pyproject.toml Drop support for running libcst using a python 3.6 interpreter (#663) 2 months ago
requirements-dev.txt Bump black to latest to address failures in Python 3.9/3.10 (#672) 2 months ago
requirements.txt Upgrade dev tools (Black/Flake8/isort) and read install requirements from requirements.txt (#380) 2 years ago
setup.py Add package links to PyPI (#688) 1 week ago

README.rst

LibCST

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

Support Ukraine - Help Provide Humanitarian Aid to Ukraine. Documentation Github Actions CodeCov PYPI PYPI Download Notebook

LibCST parses Python 3.0 -> 3.11 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.7+ 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.

Then, 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:

ufmt format && python -m fixit.cli.apply_fix

We use slotscheck to check the correctness of class __slots__. To check that slots are defined properly, run:

python -m slotscheck libcst

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

python -m unittest

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.

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 build the libcst.native module and install libcst, run this from the root:

pip uninstall -y libcst
pip install -e .

Type Checking

We use Pyre for type-checking.

To verify types for the library, do the following in the root:

pyre check

Note: You may need to run the pip install -e . command prior to type checking, see the section above on building.

Generating Documents

To generate documents, do the following in the root:

sphinx-build docs/source/ docs/build/

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.