Skip to main content

Unparser for Python 3 abstract syntax trees (ASTs) with type comments.

package version from PyPI build status from Travis CI build status from AppVeyor grade from Codacy test coverage from Codecov license

The typed-astunparse is to typed-ast as astunparse is to ast. In short: unparsing of Python 3 abstract syntax trees (ASTs) 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  # make sure the tests pass
python3 setup.py bdist_wheel
pip3 install dist/*.whl

Requirements

Python version 3.5 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.

Metadata

Release files for typed-astunparse 2.1.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for typed-astunparse 2.1.4
File Size Uploaded
typed-astunparse-2.1.4.tar.gz 23.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for typed-astunparse 2.1.4
File Interpreter ABI Platform
typed_astunparse-2.1.4-py3-none-any.whl Python 3 none any Details

Total release size: 38.2 kB

Release files / typed-astunparse-2.1.4.tar.gz

Download URL typed-astunparse-2.1.4.tar.gz
Size 23.3 kB
Tags Source
SHA-256 checksum
How to use checksums
8926f5280d346243b64090e7bf28911f004c5c80d8f46fd825a6e471454d19b7
BLAKE2b-256 checksum
How to use checksums
25de90815e11dfbe6741ea4efc497dbef8a1dd308b40b407094304082510b650
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.3

Release files / typed_astunparse-2.1.4-py3-none-any.whl

Download URL typed_astunparse-2.1.4-py3-none-any.whl
Size 14.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ee80ad573b4b20ebd541ecdf0bc7592d669f04e50a0961f7888bd9bcbc220080
BLAKE2b-256 checksum
How to use checksums
4fa0925c6b3475e0b146e19add88637a91c78fc41d0931118c8a92ea346c7990
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.3

Release history Release notifications | RSS feed

This release

2.1.4 This release

2 release files

2.1.3

2 release files

2.1.2

2 release files

2.1.1

2 release files

2.1.0

2 release files

2.0.4

2 release files

2.0.3

2 release files

2.0.2

2 release files

2.0.1

2 release files

2.0.0

2 release files

1.0.2

1 release file

1.0.1

1 release file

1.0.0

1 release file

0.2.1

1 release file

0.2.0

1 release file

0.1.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page