In [1]:
import pathlib

import typed_ast.ast3
import typed_astunparse

In [2]:
code = 'my_string = None # type: str'
roundtrip = typed_astunparse.unparse(typed_ast.ast3.parse(code))
print(roundtrip)


my_string = None  # type: str


In [3]:
code = "def negation(arg): # type: (bool) -> bool\n    return (not arg)"
roundtrip = typed_astunparse.unparse(typed_ast.ast3.parse(code))
print(roundtrip)



def negation(arg):
    # type: (bool) -> bool
    return (not arg)


In [4]:
code = "with open('setup.py') as f: # type: typing.io.TextIO\n    print(f.read())"
roundtrip = typed_astunparse.unparse(typed_ast.ast3.parse(code))
print(roundtrip)


with open('setup.py') as f:  # type: typing.io.TextIO
    print(f.read())


In [5]:
with pathlib.Path('typed_astunparse', '__init__.py').open() as f:
    tree = typed_ast.ast3.parse(f.read())
#print(typed_astunparse.dump(tree))  # long output
print(typed_astunparse.unparse(tree))


'This is "__init__.py" file for "typed_astunparse" package.\n\nfunctions: unparse, dump\n'
import ast
import typing as t
import typed_ast.ast3
from six.moves import cStringIO
from .unparser import Unparser
from .printer import Printer
from ._version import VERSION
__version__ = VERSION

def unparse(tree: t.Union[(ast.AST, typed_ast.ast3.AST)]) -> str:
    'Unparse the abstract syntax tree into a str.\n\n    Behave just like astunparse.unparse(tree), but handle trees which are typed, untyped, or mixed.\n    In other words, a mixture of ast.AST-based and typed_ast.ast3-based nodes will be unparsed.\n    '
    stream = cStringIO()
    Unparser(tree, file=stream)
    return stream.getvalue()

def dump(tree: t.Union[(ast.AST, typed_ast.ast3.AST)], annotate_fields: bool=True, include_attributes: bool=False) -> str:
    'Behave just like astunparse.dump(tree), but handle typed_ast.ast3-based trees.'
    stream = cStringIO()
    Printer(file=stream, annotate_fields=annotate_fields, include_attributes=include_attributes).visit(tree)
    return stream.getvalue()
__all__ = ['unparse', 'dump']


In [6]:
with pathlib.Path('setup.py').open() as f:
    tree = typed_ast.ast3.parse(f.read())
#print(typed_astunparse.dump(tree))  # long output
print(typed_astunparse.unparse(tree))


'This is setup.py file for typed-astunparse.'
import setup_boilerplate

class Package(setup_boilerplate.Package):
    'Package metadata.'
    name = 'typed-astunparse'
    description = 'typed-astunparse is to typed-ast as astunparse is to ast'
    download_url = 'https://github.com/mbdevpl/typed-astunparse'
    classifiers = ['Development Status :: 4 - Beta', 'Environment :: Console', 'Intended Audience :: Developers', 'Intended Audience :: Science/Research', 'License :: OSI Approved :: Apache Software License', 'Natural Language :: English', 'Operating System :: MacOS :: MacOS X', 'Operating System :: Microsoft :: Windows', 'Operating System :: POSIX :: Linux', 'Programming Language :: Python :: 3.4', 'Programming Language :: Python :: 3.5', 'Programming Language :: Python :: 3.6', 'Programming Language :: Python :: 3.7', 'Programming Language :: Python :: 3 :: Only', 'Programming Language :: Python :: Implementation :: CPython', 'Topic :: Education', 'Topic :: Scientific/Engineering', 'Topic :: Software Development :: Code Generators', 'Topic :: Software Development :: Compilers', 'Topic :: Software Development :: Pre-processors', 'Topic :: Utilities']
    keywords = ['ast', 'unparsing', 'pretty printing']
if (__name__ == '__main__'):
    Package.setup()


In [7]:
code = "a = 0"
tree = typed_ast.ast3.parse(code)
print(typed_astunparse.dump(tree))


Module(
  body=[Assign(
    targets=[Name(
      id='a',
      ctx=Store())],
    value=Num(n=0),
    type_comment=None)],
  type_ignores=[])

In [8]:
code = "a = 0 # type:"
tree = typed_ast.ast3.parse(code)
print(typed_astunparse.dump(tree))


Module(
  body=[Assign(
    targets=[Name(
      id='a',
      ctx=Store())],
    value=Num(n=0),
    type_comment='')],
  type_ignores=[])

In [9]:
code = "a = 0 # type: int"
tree = typed_ast.ast3.parse(code)
print(typed_astunparse.dump(tree))


Module(
  body=[Assign(
    targets=[Name(
      id='a',
      ctx=Store())],
    value=Num(n=0),
    type_comment='int')],
  type_ignores=[])