A concrete syntax tree parser and serializer library for Python that preserves many aspects of Python's abstract syntax tree https://libcst.readthedocs.io/
Find a file
Jennifer Taylor 64ff2b1996 Better repr for partial configuration.
This adds a __repr__ to various types in the parser config, so that the
generated documentation for functions using these types renders easier
to read.
2019-08-01 16:14:12 -07:00
.circleci use cache to speedup pyre CI job 2019-07-24 19:25:31 -07:00
.github add initial PULL_REQUEST_TEMPLATE.md 2019-07-22 19:59:57 -07:00
docs/source Better repr for partial configuration. 2019-08-01 16:14:12 -07:00
libcst Better repr for partial configuration. 2019-08-01 16:14:12 -07:00
stubs Add runtime type validation support 2019-07-22 19:53:49 -07:00
.editorconfig Add config files to make tools easier to use 2019-07-22 19:53:49 -07:00
.flake8 Remove Instagram-specific bits from flake8 config 2019-07-22 20:05:07 -07:00
.gitignore add sphinx autodoc templates and docs CI job 2019-07-22 20:05:27 -07:00
.pyre_configuration add pyre CI job 2019-07-22 20:04:29 -07:00
CHANGELOG.md Add a changelog file. 2019-07-24 12:11:50 -07:00
CODE_OF_CONDUCT.md add Code Of Conduct file 2019-07-22 19:59:50 -07:00
CONTRIBUTING.md add test command and rephrase coding style. 2019-07-22 19:59:57 -07:00
LICENSE first commit 2019-05-29 11:32:49 -07:00
pyproject.toml Add config files to make tools easier to use 2019-07-22 19:53:49 -07:00
README.rst Update base README.rst to at least advertise the current python version. 2019-08-01 14:04:38 -07:00
requirements-dev.txt [document] Use ReadTheDot Theme 2019-08-01 12:09:32 -07:00
requirements.txt Add runtime type validation support 2019-07-22 19:53:49 -07:00
setup.py [document] Use ReadTheDot Theme 2019-08-01 12:09:32 -07:00
tox.ini add sphinx autodoc templates and docs CI job 2019-07-22 20:05:27 -07:00

======
LibCST
======

|circleci_badge|

.. |circleci_badge| image:: https://circleci.com/gh/Instagram/LibCST/tree/master.svg?style=svg&circle-token=f89ff46c689cf53116308db295a492d687bf5732
   :target: https://circleci.com/gh/Instagram/LibCST/tree/master
   :alt: CircleCI

.. intro-start

LibCST is a Concrete Syntax Tree (CST) parser and serializer library for Python Code. It parses Python 3.7 source code as a CST tree and keeps all formatting detail (comments, whitespaces, parentheses, etc). It's useful for building Code Modifier (codemod) applications, code formatters, etc.

.. intro-end

.. why-libcst-intro-start
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. 

.. why-libcst-intro-end


.. why-libcst-example-start

.. code-block:: python

    fn(1, 2)  # calls fn

.. code-block:: python

    Module(
        body=[
            SimpleStatementLine(
                body=[
                    Expr(
                        value=Call(
                            func=Name(
                                value='fn',
                                lpar=[],
                                rpar=[],
                            ),
                            args=[
                                Arg(
                                    value=Number(
                                        number=Integer(
                                            value='1',
                                            lpar=[],
                                            rpar=[],
                                        ),
                                        operator=None,
                                        lpar=[],
                                        rpar=[],
                                    ),
                                    keyword=None,
                                    equal=None,
                                    comma=Comma(
                                        whitespace_before=SimpleWhitespace(
                                            value='',
                                        ),
                                        whitespace_after=SimpleWhitespace(
                                            value=' ',
                                        ),
                                    ),
                                    star='',
                                    whitespace_after_star=SimpleWhitespace(
                                        value='',
                                    ),
                                    whitespace_after_arg=SimpleWhitespace(
                                        value='',
                                    ),
                                ),
                                Arg(
                                    value=Number(
                                        number=Integer(
                                            value='2',
                                            lpar=[],
                                            rpar=[],
                                        ),
                                        operator=None,
                                        lpar=[],
                                        rpar=[],
                                    ),
                                    keyword=None,
                                    equal=None,
                                    comma=None,
                                    star='',
                                    whitespace_after_star=SimpleWhitespace(
                                        value='',
                                    ),
                                    whitespace_after_arg=SimpleWhitespace(
                                        value='',
                                    ),
                                ),
                            ],
                            lpar=[],
                            rpar=[],
                            whitespace_after_func=SimpleWhitespace(
                                value='',
                            ),
                            whitespace_before_args=SimpleWhitespace(
                                value='',
                            ),
                        ),
                        semicolon=None,
                    ),
                ],
                leading_lines=[],
                trailing_whitespace=TrailingWhitespace(
                    whitespace=SimpleWhitespace(
                        value='  ',
                    ),
                    comment=Comment(
                        value='# calls fn',
                    ),
                    newline=Newline(
                        value=None,
                    ),
                ),
            ),
        ],
        header=[],
        footer=[],
        encoding='utf-8',
        default_indent='    ',
        default_newline='\n',
        has_trailing_newline=True,
    )

.. why-libcst-example-end

Getting Started
===============

Examining a sample tree
-----------------------

To examine the tree that is parsed from a particular file, do the following:

.. code-block:: shell

    python -m libcst.tool print <some_py_file.py>

Development
-----------

Start by setting up and activating a virtualenv:

.. code-block:: shell

    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:

.. code-block:: shell

    isort -q -y && black libcst/

To run all tests, do the following in the root:

.. code-block:: shell

    tox -e py37

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

.. code-block:: shell

    pyre check

To generate documents, do the following in the root:

.. code-block:: shell

    tox -e docs

License
=======

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