ast-comments
A Python extension to the built-in ast module that preserves comments in the Abstract Syntax Tree.
This library finds comments in source code and includes them as nodes in the parsed AST.
Installation
pip install ast-comments
Usage
Usage is identical to the standard ast module:
>>> from ast_comments import *
>>> tree = parse("hello = 'hello' # comment to hello")
The parsed tree is an instance of the original ast.Module object.
The only difference is that there is a new type of tree node: Comment.
>>> tree
<_ast.Module object at 0x7ffba52322e0>
>>> tree.body
[<ast.Assign object at 0x10a01d5b0>, <ast_comments.Comment object at 0x10a09e0a0>]
>>> tree.body[1].value
'# comment to hello'
>>> dump(tree)
"Module(body=[Assign(targets=[Name(id='hello', ctx=Store())], value=Constant(value='hello')), Comment(value='# comment to hello', inline=True)], type_ignores=[])"
If you have Python 3.9 or above, you can also unparse the tree object with its comments preserved:
>>> print(unparse(tree))
hello = 'hello' # comment to hello
Note: Python's compile() function cannot be used directly on the parsed tree output. The included pre_compile_fixer() function can be used to prepare the tree for compilation by stripping comment nodes when needed.
Additional examples can be found in the test files: test_parse.py and test_unparse.py.
Contributing
Contributions are welcome! Please feel free to open an issue or create a pull request.
Metadata
Release files for ast-comments 1.3.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 | Provenance | Actions |
|---|---|---|---|---|
| ast_comments-1.3.0.tar.gz | 5.5 kB | Without provenance | Details for ast_comments-1.3.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| ast_comments-1.3.0-py3-none-any.whl | Python 3 | none | any | Details for ast_comments-1.3.0-py3-none-any.whl |
Total release size: 11.4 kB
Release files / ast_comments-1.3.0.tar.gz
| Download URL | ast_comments-1.3.0.tar.gz |
|---|---|
| Size | 5.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
45f0113ecff4156a98255c1b87cc6e15b9cee185c3f037c714cf27d3225ed7c3
|
|
BLAKE2b-256 checksum How to use checksums |
eae89bb599fd6162644d31fe0f87e0e2903d92cd434e4327d9cc2eaa904f6777
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.8.3 CPython/3.10.19 Darwin/25.2.0
|
Release files / ast_comments-1.3.0-py3-none-any.whl
| Download URL | ast_comments-1.3.0-py3-none-any.whl |
|---|---|
| Size | 5.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
1b53fbf7fa89af1ac6222fea0a752328f2d6c35de565e045ad74a16c9441209d
|
|
BLAKE2b-256 checksum How to use checksums |
8a38e30499bf1b346e372b780f9161131bb230ce1a63b36662b0acabed3b3219
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.8.3 CPython/3.10.19 Darwin/25.2.0
|
Security for ast-comments 1.3.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.