Skip to main content

Pretty print the output of python stdlib `ast.parse`.

Project description

Build Status Azure DevOps coverage

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=[],
)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

astpretty-1.7.0.tar.gz (4.9 kB view details)

Uploaded Source

Built Distribution

astpretty-1.7.0-py2.py3-none-any.whl (5.1 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file astpretty-1.7.0.tar.gz.

File metadata

  • Download URL: astpretty-1.7.0.tar.gz
  • Upload date:
  • Size: 4.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.6.8

File hashes

Hashes for astpretty-1.7.0.tar.gz
Algorithm Hash digest
SHA256 a352e4fb5ca87a3333b9b4c6d00b1938f0719e2c555b206e628012bf02905a1e
MD5 3abca5f6dca6284db1c2aab00e9e3f13
BLAKE2b-256 482ef92b32b2934fb16397cd54447c78062c33d0898e23862d78fea2c2e6fe0f

See more details on using hashes here.

Provenance

File details

Details for the file astpretty-1.7.0-py2.py3-none-any.whl.

File metadata

  • Download URL: astpretty-1.7.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 5.1 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.6.8

File hashes

Hashes for astpretty-1.7.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 c827ac3cef040b06283166b6c098e1f639621b065023c6378f22e31a272e9b56
MD5 311999d0cd6ebaca05af38e1953553fe
BLAKE2b-256 3fc21791d5945767ead406c6bb0fda1f482106dcc0ba370819bcc79512ba1260

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page