typed-astunparse is to typed-ast as astunparse is to ast
Project description
The typed-astunparse is to typed-ast as astunparse is to ast. In short: unparsing of Python 3 abstract syntax trees (AST) with type comments.
Why this module was created
The built-in ast module can parse Python source code into AST but it can’t generate source code from the AST. The astunparse module (using a refactored version of an obscure script found in official Python repository) provides code generation capability for native Python AST.
However, both ast and astunparse modules 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 why module typed-astunparse (i.e. this one) was created: to provide unparser for AST defined in typed-ast.
Usage
Example of roundtrip from code through AST to code:
import typed_ast.ast3
import typed_astunparse
code = 'my_string = None # type: str'
roundtrip = typed_astunparse.unparse(typed_ast.ast3.parse(code))
print(roundtrip)
This will print:
my_string = None # type: str
for more examples see examples.ipynb notebook.
Installation
For simplest installation use pip
:
pip3 install typed-astunparse
You can also build your own version:
git clone https://github.com/mbdevpl/typed-astunparse
cd typed-astunparse
pip3 install -U test_requirements.txt
python3 -m unittest discover # make sure the tests pass
python3 setup.py bdist_wheel
pip3 install dist/*.whl
Requirements
Python version 3.4 or later.
Python libraries as specified in requirements.txt.
Building and running tests additionally requires packages listed in test_requirements.txt.
Tested on Linux, OS X and Windows.
Links
Extensions of this module
If you’re extending typed-astunparse and you’d like to share why, feel free to submit a pull request introducing your project.
horast: human-oriented ast
Built upon both typed-ast and typed-astunparse providing parsing and unparsing of arbitrary comments in addition to type comments.
Who’s using this module and why
If you’re using typed-astunparse in your work and you’d like to share why, feel free to submit a pull request introducing your project.
static-typing: using typed-astunparse directly to provide AST unparsing function
References
ast:
astunparse:
https://pypi.org/project/astunparse
PEP 483 - The Theory of Type Hints:
PEP 484 - Type Hints:
PEP 3107 - Function Annotations:
PEP 526 - Syntax for Variable Annotations:
typed-ast:
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for typed_astunparse-2.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7466f417cb4d5bf3b0c577513354654b90bccdd3a7737b7026571a8ed346e2df |
|
MD5 | 3597af2ca502c17e63b032cb004d461a |
|
BLAKE2b-256 | e2eb8bb65d42c6a39b8db87c48f045919172a387aec49dece40ea123b6d9c14a |