astunparse

repository·main·Indexed 42 days ago

https://github.com/simonpercivall/astunparse

A library for converting Python Abstract Syntax Trees (AST) back into source code or generating pretty-printed dumps of the AST. It provides a unified implementation compatible with Python 2.6 and Python 3.5, featuring the unparse() and dump() functions, as well as a command-line tool for roundtripping Python files.

Tokens
1.9K
Snippets
12
Records
16
Agent score
51%

What's inside astunparse

  1. Quickstart: Unparse and dump source code

    main

    The following example demonstrates how to use inspect to get the source of a module, parse it into an AST, and then use astunparse to both reconstruct the source and print a pretty-printed version of the tree.

    import inspect
    import ast
    import astunparse
    
    # get back the source code
    astunparse.unparse(ast.parse(inspect.getsource(ast)))
    
    # get a pretty-printed dump of the AST
    astunparse.dump(ast.parse(inspect.getsource(ast)))
  2. Use AST Unparser in your project

    main

    To use astunparse in your Python project, import the module and use the unparse function to convert an Abstract Syntax Tree (AST) back into source code.

    import astunparse
    
    # Use the unparse function to convert an AST tree back to source code
    # source = astunparse.unparse(ast_tree)
  3. How Unparser handles __future__ imports

    main
    The Unparser class tracks from __future__ import ... statements. This is necessary because certain __future__ imports (like unicode_literals) change how subsequent nodes in the AST (such as Str nodes) should be unparsed to remain syntactically correct for the target Python version.
  4. How the Unparser handles large floats and imaginary literals

    main

    When Python's ast module encounters extremely large float or imaginary literals, it often converts them into inf (infinity) within the AST. To prevent losing the original literal representation during unparsing, astunparse uses a special constant INFSTR to represent these values.

    This ensures that the unparser can attempt to reconstruct a valid (though potentially approximate) representation of the original overflowing values.

  5. Get a pretty-printed dump of an AST with dump()

    main

    Use astunparse.dump() to generate a human-readable, pretty-printed string representation of a Python AST. This is useful for debugging and inspecting the structure of the tree.

    import ast
    import astunparse
    
    # Assuming 'tree' is an AST object
    print(astunparse.dump(tree))
  6. Convert a Python AST back into source code with unparse()

    main

    Use astunparse.unparse() to convert an Abstract Syntax Tree (AST) back into its original Python source code representation. This is a factored-out version of the unparse utility found in the Python source distribution.

    import ast
    import astunparse
    
    # Assuming 'tree' is an AST object
    source_code = astunparse.unparse(tree)
  7. Configure Printer indentation and output destination

    main

    When instantiating the Printer class, you can control where the output is written and how the tree is indented:

    • file: A file-like object where the output will be written. Defaults to sys.stdout.
    • indent: The string used for each level of indentation. Defaults to `
  8. Run the unparser from the command line

    main

    The unparse.py script can be executed directly from the terminal to process files.

    Basic Usage: Provide one or more paths to Python source files to unparse them to stdout.

    python unparse.py <path_to_source_file>

    Testing Mode: Use the --testdir flag to run roundtrip tests on all .py files within specified directories.

    python unparse.py --testdir <directory_path>
  9. Get a pretty-printed AST dump with dump()

    main

    Use dump(tree) to generate a formatted, pretty-printed string representation of a Python AST. This function uses the Printer class to traverse the tree and returns the output as a string.

    import ast
    import astunparse
    
    tree = ast.parse('x = 1 + 2')
    dump_str = astunparse.dump(tree)
    print(dump_str)
  10. Convert an AST to source code with unparse()

    main

    Use unparse(tree) to convert a Python Abstract Syntax Tree (AST) back into its original source code string. This function wraps the Unparser class and returns the resulting string.

    import ast
    import astunparse
    
    tree = ast.parse('print("hello world")')
    source = astunparse.unparse(tree)
    print(source)