Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Instagram/LibCST: A concrete syntax tree parser and serializer library for Python that preserves many aspects of Python's abstract syntax tree #929

Open
1 task
ShellLM opened this issue Nov 4, 2024 · 1 comment
Labels
Git-Repo Source code repository like gitlab or gh github gh tools like cli, Actions, Issues, Pages python Python code, tools, info

Comments

@ShellLM
Copy link
Collaborator

ShellLM commented Nov 4, 2024

Instagram/LibCST: A concrete syntax tree parser and serializer library for Python that preserves many aspects of Python's abstract syntax tree

Snippet

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

LibCST parses Python 3.0 -> 3.12 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=[],
)
"

README

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

We recommend using hatch for running tests, linters, etc.

Then, start by setting up and building the project:

git clone git@github.com:Instagram/LibCST.git libcst
cd libcst
hatch env create

To run the project's test suite, you can:

hatch run test

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

hatch run python -m unittest libcst.tests.test_batched_visitor

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

We have multiple linters, including copyright checks and slotscheck to check the correctness of class __slots__. To run all of the linters:

hatch run lint

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

hatch run format

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 rebuild the libcst.native module, from the repo root:

hatch env prune && hatch env create

Type Checking

We use Pyre for type-checking.

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

hatch run typecheck

Generating Documents

To generate documents, do the following in the root:

hatch run docs

Future

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

Suggested labels

None

@ShellLM ShellLM added Git-Repo Source code repository like gitlab or gh github gh tools like cli, Actions, Issues, Pages python Python code, tools, info labels Nov 4, 2024
@ShellLM
Copy link
Collaborator Author

ShellLM commented Nov 4, 2024

Related content

#762 similarity score: 0.9
#877 similarity score: 0.89
#730 similarity score: 0.88
#871 similarity score: 0.88
#749 similarity score: 0.87
#515 similarity score: 0.87

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Git-Repo Source code repository like gitlab or gh github gh tools like cli, Actions, Issues, Pages python Python code, tools, info
Projects
None yet
Development

No branches or pull requests

1 participant