🍞 lib2toast
This library converts a lib2to3 concrete syntax tree (CST) to a standard Python AST.
Potential use cases include:
- Parsing Python code with less dependence on the Python version
- Extending or modifying the Python grammar in order to experiment with new features or to create a Python-like dialect
Usage
This library is still at an early stage and the API may change.
lib2toast.api.compile(code, *, grammar=..., compiler=...): Compile a string of code to an AST. This AST can then be compiled to a Python code object or executed with the built-incompile()andexec()functions. By default, this uses a grammar that covers all syntax that is accepted by the latest version of Python, plus some additional syntax. Pass a custom grammar to use different syntax. You can uselib2toast.api.load_grammarto load a grammar object from a file. If you do this, you'll usually also want to pass a customcompilerobject by subclassinglib2toast.compile.Compiler.lib2toast.api.run(code, *, filename=..., grammar=..., compiler=...): Compiles code and then immediately executes it.lib2toast.api.load_grammar(path, *, async_keywords=True): Load a grammar file from a path. Ifasync_keywordsis True, treatsasyncas a keyword as in Python 3.7+.
There is also a command-line interface: python -m lib2toast -c code runs
code after parsing it using lib2toast.
Showcase
The command-line interface shows that lib2toast supports parsing some new syntax in older Python versions:
$ python3.9 -m lib2toast -c 'print(f"{"x"}")'
x
This is new syntax introduced in Python 3.12 by PEP 701.
It also supports some (not all) Python 2 syntax that was removed in Python 3:
$ python3.9 -m lib2toast -c 'print(1 <> 2)'
True
The test suite shows some examples of syntactic variants of Python parsed with lib2toast. For example:
dataclass(frozen=True) C:
x: int
y: int = 0
Implementation
lib2toast is implemented on top of blib2to3, the fork of lib2to3 maintained
by the Black project in order to parse and format
Python code. It originates from lib2to3, a tool shipped with earlier Python 3 versions
to support converting between Python 2 and 3 code.
The core part of the implementation is a tool that converts Python code to an
AST. This makes it easy to test for correctness: just run Python's built-in
ast.parse and assert that it produces the same tree, including line and
column numbers. So far I have tested the compiler on lib2toast's own code
as well as some of Black's code (the Black test cases were especially helpful),
but there are probably more bugs.
Python version support
This library supports Python versions 3.9 and up.
Python 3.8 is unsupported because it is about to reach the end of its support period, the AST structure is quite different between 3.8 and 3.9, and I don't have a use case for 3.8.
In the future I plan to support all supported upstream versions of Python.
Contributing
Contributions to this project are welcome, including ideas for new ways to use the core functionality of the library.
Check the "Issues" tab for potential areas to contribute.
Release notes
Version 0.1.0 (August 6, 2024)
- Fix miscompilation of Unicode identifiers; they are now NFKC-normalized
- Fix crash on augmented assignment with an unparenthesized tuple on the right-hand side
- Fix crash on nested async comprehensions
- Fix incorrect line ranges in suites containing statements ending in a semicolon
- Fix crash on statement ending in a semicolon
- Fix crash on certain triple-quoted f-strings
- Fix miscompilation of calls with arguments on the left-hand side of an assignment
- Make it easier to create a dialect with more kinds of trailers
- Adjust some type annotations to make subclassing easier
Version 0.0.1 (July 1, 2024)
Initial release.
Release files for lib2toast 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| lib2toast-0.1.0.tar.gz | 31.2 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| lib2toast-0.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 51.7 kB
Release files / lib2toast-0.1.0.tar.gz
| Download URL | lib2toast-0.1.0.tar.gz |
|---|---|
| Size | 31.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
b55cf438d7be8c5ce189fe85e5c49698162815c59f2d8f5ec5afa54c6813f64e
|
|
BLAKE2b-256 checksum How to use checksums |
32da383891b06ae4a364511b2d3a0e15eb01a2ff769edb8531aa9893d7e9688d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/5.1.0 CPython/3.12.4
|
Release files / lib2toast-0.1.0-py3-none-any.whl
| Download URL | lib2toast-0.1.0-py3-none-any.whl |
|---|---|
| Size | 20.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
7ebd3a5ccdcde3f46033511e0ab271da84a2073b48bcfea65011a130ad097494
|
|
BLAKE2b-256 checksum How to use checksums |
3a13d6820c459e6988fb35079ff5d07f660bfef1b956a5cc29d78519a5bbbced
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/5.1.0 CPython/3.12.4
|