AST-Compat
Compatible AST construction and ast.unparse for Python 3.5-3.13.
What is AST-Compat for?
-
backward compatibility for constructing ASTs
For instance,
ast.argumentsis changed since Python 3.8, that another fieldposonlyargis introduced, hence you should construct anast.argumentswith an additional argumentposonlyarg=...since Python 3.8 and it wouldn't work if your code is written before Python 3.8.Through
ast_compat, you don't have to worry about above question if a new argument is optional or a list.Note that you should use
ast_compat.XXXinstead ofast.XXXto construct ASTs. -
Support ast.Constant before Python 3.6.
ast.Constantis convenient, and things likeast.Numare redundant according to this observation and improvement: https://bugs.python.org/issue32892However,
ast.Constantis not available in 3.5 or earlier versions, thus we backportast.Constantin this library.To access the content of
ast.Constantin a compatible way, useast_compat.get_constantinstead of.value. -
Support dumping AST to string with ast_compat.unparse, which synchronizes the tooling code provided by CPython official repo.
Usage
import ast_compat as astc
from ast_compat import get_constant
assert get_constant(astc.Constant((1, 2))) == (1, 2)
empty_args = astc.arguments() # work for all of Python 3.5-3.13
Implementation, and rebuild
The compatibility is following the specification of Python ASTs, i.e, the ADSL file you can find at
https://github.com/python/cpython/blob/<branch/tag>/Parser/Python.asdl.
We parse the file, and generate verifications and default argument factories,
check ast_compat/compat3k*_{ast|unparse}.py. To explain, ast_compat/compat3k5_*.py is for
Python 3.5, and ast_compat/compat3k9_*.py is for Python 3.9.
The file of generator is generate_ast_compat.py, and the use of generator API is in this way:
from generate_ast_compat import compat
compat((3, 5)) # generate ast_compat/compat3k5_ast.py and ast_compat/compat3k5_unparse.py
compat((3, 6))
compat((3, 7))
compat((3, 8))
prerelease_url = "https://raw.githubusercontent.com/python/cpython/v3.9.0a3/parser/python.asdl"
compat((3, 9), prerelease_url)
The code generation needs Python 3.7+, though this library works for Python 3.5 and 3.5+.
Metadata
Release files for ast-compat 0.12.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| ast_compat-0.12.0-py3-none-any.whl | Python 3 | none | any | Details for ast_compat-0.12.0-py3-none-any.whl |
Release files / ast_compat-0.12.0-py3-none-any.whl
| Download URL | ast_compat-0.12.0-py3-none-any.whl |
|---|---|
| Size | 53.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
911d81236a18201d4930c93ccd6a2d3dd14442cbccfbd2787b883ac8cb38b208
|
|
BLAKE2b-256 checksum How to use checksums |
d5d006e348161072f45b610d80427196ee249a030783179f5d65473983c15b76
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.11.10
|
Security for ast-compat 0.12.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.