{"library":"astunparse","title":"AST Unparser for Python","description":"astunparse is a Python library that provides an Abstract Syntax Tree (AST) unparser, allowing conversion of ASTs back into Python source code. It is a standalone, factored-out version of the `unparse` utility originally found within the Python source distribution itself. In addition to unparsing, it offers a `dump` function for pretty-printing AST structures. The current version is 1.6.3, released in December 2019, and it has a low-frequency release cadence, primarily updating for compatibility with newer Python AST changes.","status":"active","version":"1.6.3","language":"en","source_language":"en","source_url":"https://github.com/simonpercivall/astunparse","tags":["AST","code generation","unparser","static analysis","developer tools","python2","python3"],"install":[{"cmd":"pip install astunparse","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"Compatibility layer for Python 2 and 3, used internally for dual-version support.","package":"six"}],"imports":[{"symbol":"unparse","correct":"import astunparse\n# ...\nastunparse.unparse(my_ast_node)"},{"note":"Used for pretty-printing an AST, similar to `ast.dump` but with custom formatting.","symbol":"dump","correct":"import astunparse\n# ...\nastunparse.dump(my_ast_node)"}],"quickstart":{"code":"import ast\nimport inspect\nimport astunparse\n\n# Get the AST of a simple function\ndef example_func(x, y):\n    return x + y\n\nsource_code = inspect.getsource(example_func)\nparsed_ast = ast.parse(source_code)\n\n# Unparse the AST back to source code\nunparsed_code = astunparse.unparse(parsed_ast)\nprint('--- Unparsed Code ---')\nprint(unparsed_code)\n\n# Pretty-print the AST\ndumped_ast = astunparse.dump(parsed_ast)\nprint('\\n--- Dumped AST ---')\nprint(dumped_ast)","lang":"python","description":"This quickstart demonstrates how to parse Python source code into an AST using the built-in `ast` module, then use `astunparse.unparse` to convert the AST back into source code, and `astunparse.dump` to get a pretty-printed representation of the AST."},"warnings":[{"fix":"For Python 3.9 and newer, prefer `ast.unparse(node)` directly. For older versions or specific `astunparse` features, continue using the library.","message":"Python 3.9+ includes `ast.unparse` natively. The standalone `astunparse` library is primarily useful for projects targeting Python versions prior to 3.9 or when specific behaviors of this library are preferred over the built-in one. For newer Python versions, `ast.unparse` is generally the recommended approach.","severity":"gotcha","affected_versions":"<3.9 for standalone usage relevance"},{"fix":"Test your unparsing logic thoroughly against target Python versions. When targeting newer Python versions, consider if the built-in `ast.unparse` might be more up-to-date with recent AST changes than the standalone library.","message":"While `astunparse` aims for broad compatibility (Python 2.6 through 3.8 as per its last update), the underlying Python AST structure can change significantly between major Python versions. For example, Python 3.12 introduced changes (PEP 695) to AST node attributes, which could affect code relying on specific AST node structures or the exact output for constructs like f-strings. This means strict round-trip compatibility across different Python versions cannot always be guaranteed by any AST unparser.","severity":"breaking","affected_versions":"All versions, especially when moving between major Python releases (e.g., Python 3.8 to 3.9+, or 3.11 to 3.12+)."},{"fix":"For use cases requiring the preservation of type comments, consider alternative libraries like `typed-astunparse`, which are built to work with `typed-ast` (an AST variant that includes type comment nodes).","message":"`astunparse` (and the standard `ast` module it derives from) does not preserve or unparse PEP 484 type comments. If your project's code includes type annotations and you need them preserved after unparsing, `astunparse` will discard them.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-05T00:00:00.000Z","next_check":"2026-07-04T00:00:00.000Z"}