astpretty
Pretty print the output of python stdlib ast.parse.
astpretty is intended to be a replacement for ast.dump.
Installation
pip install astpretty
Usage
astpretty provides two api functions:
astpretty.pprint(node, indent=FOUR_SPACE_INDENT, show_offsets=True)
Print a representation of the ast node.
>>> astpretty.pprint(ast.parse('if x == y: y += 4').body[0])
If(
lineno=1,
col_offset=0,
test=Compare(
lineno=1,
col_offset=3,
left=Name(lineno=1, col_offset=3, id='x', ctx=Load()),
ops=[Eq()],
comparators=[Name(lineno=1, col_offset=8, id='y', ctx=Load())],
),
body=[
AugAssign(
lineno=1,
col_offset=11,
target=Name(lineno=1, col_offset=11, id='y', ctx=Store()),
op=Add(),
value=Num(lineno=1, col_offset=16, n=4),
),
],
orelse=[],
)
indent allows control over the indentation string:
>>> astpretty.pprint(ast.parse('if x == y: y += 4').body[0], indent=' ')
If(
lineno=1,
col_offset=0,
test=Compare(
lineno=1,
col_offset=3,
left=Name(lineno=1, col_offset=3, id='x', ctx=Load()),
ops=[Eq()],
comparators=[Name(lineno=1, col_offset=8, id='y', ctx=Load())],
),
body=[
AugAssign(
lineno=1,
col_offset=11,
target=Name(lineno=1, col_offset=11, id='y', ctx=Store()),
op=Add(),
value=Num(lineno=1, col_offset=16, n=4),
),
],
orelse=[],
)
show_offsets controls whether the output includes line / column information:
>>> astpretty.pprint(ast.parse('x += 5').body[0], show_offsets=False)
AugAssign(
target=Name(id='x', ctx=Store()),
op=Add(),
value=Num(n=5),
)
astpretty.pformat(node, indent=FOUR_SPACE_INDENT, show_offsets=True)
Return a string representation of the ast node.
Arguments are identical to astpretty.pprint.
>>> astpretty.pformat(ast.parse('if x == y: y += 4').body[0])
"If(\n lineno=1,\n col_offset=0,\n test=Compare(\n lineno=1,\n col_offset=3,\n left=Name(lineno=1, col_offset=3, id='x', ctx=Load()),\n ops=[Eq()],\n comparators=[Name(lineno=1, col_offset=8, id='y', ctx=Load())],\n ),\n body=[\n AugAssign(\n lineno=1,\n col_offset=11,\n target=Name(lineno=1, col_offset=11, id='y', ctx=Store()),\n op=Add(),\n value=Num(lineno=1, col_offset=16, n=4),\n ),\n ],\n orelse=[],\n)"
Comparison with stdlib ast.dump
>>> print(ast.dump(ast.parse('if x == y: y += 4').body[0]))
If(test=Compare(left=Name(id='x', ctx=Load()), ops=[Eq()], comparators=[Name(id='y', ctx=Load())]), body=[AugAssign(target=Name(id='y', ctx=Store()), op=Add(), value=Num(n=4))], orelse=[])
typed-ast support
astpretty works with typed-ast!
For usage with typed-ast make sure you have typed-ast installed, a
convenient way to do this is with the typed extra to astpretty:
pip install astpretty[typed]
The apis above work equally well with the return values from the ast modules
provided by typed_ast:
>>> import astpretty
>>> from typed_ast import ast3
>>> astpretty.pprint(ast3.parse('x = 4 # type: int'))
Module(
body=[
Assign(
lineno=1,
col_offset=0,
targets=[Name(lineno=1, col_offset=0, id='x', ctx=Store())],
value=Num(lineno=1, col_offset=4, n=4),
type_comment='int',
),
],
type_ignores=[],
)
With typed-ast installed, the commandline interface adds --typed-27 and
--typed-3 options for using the alternative ast parsers:
$ astpretty --typed-3 t.py
Module(
body=[
Assign(
lineno=1,
col_offset=0,
targets=[Name(lineno=1, col_offset=0, id='x', ctx=Store())],
value=Num(lineno=1, col_offset=4, n=4),
type_comment='int',
),
],
type_ignores=[],
)
Metadata
Release files for astpretty 3.0.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 |
|---|---|---|---|---|
| astpretty-3.0.0.tar.gz | 4.7 kB | Without provenance | Details for astpretty-3.0.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| astpretty-3.0.0-py2.py3-none-any.whl | Python 2, Python 3 | none | any | Details for astpretty-3.0.0-py2.py3-none-any.whl |
Total release size: 9.6 kB
Release files / astpretty-3.0.0.tar.gz
| Download URL | astpretty-3.0.0.tar.gz |
|---|---|
| Size | 4.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
b08c95f32e5994454ea99882ff3c4a0afc8254c38998a0ed4b479dba448dc581
|
|
BLAKE2b-256 checksum How to use checksums |
2db6ffec7edce2a8315ef1acd4ae2e334ba428db2b2e8d7577a9aaf1e434034d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.63.0 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.8.10
|
Release files / astpretty-3.0.0-py2.py3-none-any.whl
| Download URL | astpretty-3.0.0-py2.py3-none-any.whl |
|---|---|
| Size | 4.9 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
15bfd47593667169485a1fa7938b8de9445b11057d6f2b6e214b2f70667f94b6
|
|
BLAKE2b-256 checksum How to use checksums |
9f0a79fff71a08bc0cc427f0dfbd4cca62b60f7f277aae81b89b79e9b04d526d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.63.0 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.8.10
|
Security for astpretty 3.0.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.