Skip to main content

typed-astunparse is to typed-ast as astunparse is to ast

Project description

package version from PyPI build status from Travis CI test coverage from Coveralls code health from Landscape code GPA from Code Climate issue count from Code Climate license

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 and variable annotations.

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 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.

Moreover, neither module accepts variable annotations introduced in PEP 526. They cause syntax errors. The typed-ast module provides an updated AST including type comments defined in PEP 484 and variable annotations introduced in PEP 526. It also provides a parser for Python code using those features.

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 intended for Python 3.5 and later. 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.ast35.parse(code))
print(roundtrip)

code = 'my_string: str = None'
roundtrip = typed_astunparse.unparse(typed_ast.ast35.parse(code))
print(roundtrip)

for more examples see examples.ipynb notebook.

Project details


Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

typed_astunparse-0.2.0-py3-none-any.whl (12.0 kB view details)

Uploaded Python 3

File details

Details for the file typed_astunparse-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for typed_astunparse-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 efc8f6ad540a40c6023e31af3ad67f66d4ec1cdbd12d9ec5e37b6639a8e83cd6
MD5 bff52beb7cb8423bee137278d89d7a0d
BLAKE2b-256 886e658f5060efd5fcfbbcbd53c12fb34ed005f1807f024aaa3fefca40300070

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page