Skip to main content
This is a pre-production deployment of Warehouse. Changes made here affect the production instance of PyPI (pypi.python.org).
Help us improve Python packaging - Donate today!

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.

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.

requirements

This package is intended for Python 3.3 and later. It is tested on Linux (Travis CI) and Windows (AppVeyor), but it might work on other systems too.

typed-astunparse mainly depends on:

  • astunparse
  • typed-ast

installation

For simplest installation use pip:

pip install typed-astunparse

You can also build your own version:

git clone https://github.com/mbdevpl/typed-astunparse
cd typed-astunparse
python -m unittest discover # make sure the tests pass
python setup.py bdist_wheel
ls -1tr dist/*.whl | tail -n 1 | xargs pip install

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)

for more examples see examples.ipynb notebook.

Release History

Release History

This version
History Node

2.0.0

History Node

1.0.2

History Node

1.0.1

History Node

1.0.0

History Node

0.2.1

History Node

0.2.0

History Node

0.1.0

Download Files

Download Files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

File Name & Checksum SHA256 Checksum Help Version File Type Upload Date
typed_astunparse-2.0.0-py3-none-any.whl (13.5 kB) Copy SHA256 Checksum SHA256 py3 Wheel Mar 17, 2017
typed-astunparse-2.0.0.tar.gz (16.3 kB) Copy SHA256 Checksum SHA256 Source Mar 17, 2017

Supported By

WebFaction WebFaction Technical Writing Elastic Elastic Search Pingdom Pingdom Monitoring Dyn Dyn DNS Sentry Sentry Error Logging CloudAMQP CloudAMQP RabbitMQ Heroku Heroku PaaS Kabu Creative Kabu Creative UX & Design Fastly Fastly CDN DigiCert DigiCert EV Certificate Rackspace Rackspace Cloud Servers DreamHost DreamHost Log Hosting