Typed Python AST (abstract syntax tree) unparser
The typed-astunparse Python package is to typed-ast as astunparse is to ast. In 10 words: it enables unparsing of Python 3 AST with type comments.
The built-in ast module can parse Python source code into AST. It can’t, however, generate source code from the AST. That’s where astunparse comes in. Using a refactored version of an obscure script found in official Python repository, it provides code generation capability for native Python AST.
The ast and astunparse modules, however, completely ignore type comments introduced in
PEP 484. They treat them like all other comments, so when you parse the code using
compile()
, your type comments will be lost. There is no place for them in the AST, so
obviously they also cannot be unparsed.
The typed-ast module provides an updated AST including type comments defined in PEP 484 and a parser for Python code that contains such comments.
Unfortunately, typed-ast doesn’t provide any means to go from AST back to source code with type comments. This is where this module, typed-astunparse, comes in. It provides unparser for AST defined in typed-ast.
links
-
ast:
-
astunparse:
https://pypi.python.org/pypi/astunparse
-
PEP 483 - The Theory of Type Hints:
-
PEP 484 - Type Hints:
-
PEP 3107 - Function Annotations:
-
PEP 526 - Syntax for Variable Annotations:
-
typed-ast:
Status | Alpha |
Language | Python |
---|---|
License | Apache License 2.0 |