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.5 abstract syntax trees (AST) with type comments.
The built-in ast module has capabilites to parse Python source code into AST. It does not, however, have the capability to 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 built-in AST.
Built-in ast module, however, completely ignores type comments introduced in PEP 484. It treats
them like all other comments, so when you parse the code using compile()
, your type
comments will be lost. The typed-ast module intoduces AST and parser that includes type comments
defined in PEP 484.
Unfortunately, typed-ast also doesn’t provide any means to go from AST to source code. This is where this module, typed-astunparse, comes in. It provides unparser for AST defined in typed-ast.
requirements
This package is intendended for Python 3.5. It was tested on 64 bit Ubuntu, but it might work on other systems too.
typed-astunparse depends on:
astunparse
typed-ast
installation
For simplest installation use pip
:
pip3.5 install typed-astunparse
You can also build your own version:
git clone https://github.com/mbdevpl/typed-astunparse
cd typed-astunparse
python3.5 -m unittest discover # make sure the tests pass
python3.5 setup.py bdist_wheel
ls -1tr dist/*.whl | tail -n 1 | xargs pip3.5 install
usage
Example of roundtrip from code through AST to code:
import typed_ast
import typed_astunparse
code = 'my_string = None # type: str'
roundtrip = typed_astunparse.unparse(typed_ast.parse(code))
print(roundtrip)
for more examples see examples.ipynb
notebook.
links
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 Distributions
Built Distribution
Hashes for typed_astunparse-0.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | edf37e598bc7a2f0c3b5e034dd1a3454a0f9d826aaf51181d5e0a204ac74327e |
|
MD5 | ea372076ea123edcd10adbd894403746 |
|
BLAKE2b-256 | 9707ea13b5475a9b75b831888c4263b4b1b3ef86f06a3eaaec0e1f3503047ec9 |