Package: typed-astunparse
Unparser for Python 3 abstract syntax trees (ASTs) with type comments.Thetyped-astunparseis totyped-astasastunparseis toast. In short: unparsing of Python
3 abstract syntax trees (ASTs) with type comments.ContentsWhy this module was createdUsageInstallationRequirementsLinksExtensions of this moduleWho’s using this module and whyReferencesWhy this module was createdThe built-inastmodule can parse Python source code into AST but it can’t generate source
code from the AST. Theastunparsemodule (using a refactored version of an obscure
script found in official Python repository) provides code generation capability for native
Python AST.However, bothastandastunparsemodules completely ignore type comments introduced in
PEP 484. They treat them like all other comments, so when you parse the code usingcompile(), your type comments will be lost. There is no place for them in the AST, so
obviously they also cannot be unparsed.Thetyped-astmodule provides an updated AST including type comments defined in PEP 484 and
a parser for Python code that contains such comments.Unfortunately,typed-astdoesn’t provide any means to go from AST back to source code with type
comments. This is why moduletyped-astunparse(i.e. this one) was created: to provide unparser
for AST defined intyped-ast.UsageExample of roundtrip from code through AST to code:importtyped_ast.ast3importtyped_astunparsecode='my_string = None  # type: str'roundtrip=typed_astunparse.unparse(typed_ast.ast3.parse(code))print(roundtrip)This will print:my_string=None# type: strfor more examples seeexamples.ipynbnotebook.InstallationFor simplest installation usepip:pip3installtyped-astunparseYou can also build your own version:gitclonehttps://github.com/mbdevpl/typed-astunparsecdtyped-astunparsepip3install-Utest_requirements.txtpython3-munittest# make sure the tests passpython3setup.pybdist_wheelpip3installdist/*.whlRequirementsPython version 3.5 or later.Python libraries as specified inrequirements.txt.Building and running tests additionally requires packages listed intest_requirements.txt.Tested on Linux, OS X and Windows.LinksExtensions of this moduleIf you’re extending typed-astunparse and you’d like to share why,
feel free to submit apull requestintroducing your project.horast: human-oriented astBuilt upon bothtyped-astandtyped-astunparseproviding parsing and unparsing
of arbitrary comments in addition to type comments.https://pypi.org/project/horasthttps://github.com/mbdevpl/horastWho’s using this module and whyIf you’re using typed-astunparse in your work and you’d like to share why,
feel free to submit apull requestintroducing your project.static-typing: usingtyped-astunparsedirectly to provide AST unparsing functionhttps://pypi.org/project/static-typinghttps://github.com/mbdevpl/static-typingReferencesast:https://docs.python.org/3/library/ast.htmlhttps://greentreesnakes.readthedocs.io/astunparse:https://pypi.org/project/astunparsehttps://github.com/simonpercivall/astunparsehttps://astunparse.readthedocs.io/en/latest/PEP 483 - The Theory of Type Hints:https://www.python.org/dev/peps/pep-0483/PEP 484 - Type Hints:https://www.python.org/dev/peps/pep-0484/PEP 3107 - Function Annotations:https://www.python.org/dev/peps/pep-3107/PEP 526 - Syntax for Variable Annotations:https://www.python.org/dev/peps/pep-0526/typed-ast:https://pypi.org/project/typed-asthttps://github.com/python/typed_ast