classification
Title: Expose ast.unparse in the ast module
Type: enhancement Stage: patch review
Components: Library (Lib) Versions: Python 3.9
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: pablogsal Nosy List: BTaskaya, Batuhan Taskaya, brandtbucher, brett.cannon, gvanrossum, levkivskyi, miss-islington, pablogsal
Priority: normal Keywords: patch

Created on 2019-11-20 22:34 by pablogsal, last changed 2019-11-25 14:25 by BTaskaya.

Pull Requests
URL Status Linked Edit
PR 17302 merged pablogsal, 2019-11-20 22:44
PR 17376 merged pablogsal, 2019-11-25 11:29
PR 17377 open BTaskaya, 2019-11-25 14:25
Messages (7)
msg357107 - (view) Author: Pablo Galindo Salgado (pablogsal) * (Python committer) Date: 2019-11-20 22:34
As discussed in https://mail.python.org/archives/list/python-dev@python.org/thread/JAQDBMC23HW2PQ27HQNJ7G244T423IDD/ I propose to expose the unparse.py tool as part of the standard library in the ast module.

The exposed function will have the interface:

ast.unparse(ast_obj, *, option1, option2,...)

and will return a string with the unparsed version of ast_obj.

The unparse tool will need some cosmetic improvements that will be tracked separately in this issue.
msg357111 - (view) Author: Pablo Galindo Salgado (pablogsal) * (Python committer) Date: 2019-11-20 23:01
After PR17302 is merged we need to fix the following cosmetic issues indicated by Victor:

(*) unparse adds many useless parentheses. The algorithm seems naive.
For example, it adds "()" to "class _AddedDllDirectory():". It also
adds parenthesis around yield, like "(yield (top, dirs, nondirs))",
whereas the AST node was at "top level": it isn't a sub-expression.
Maybe this algortihm should be made smarter.

(*) newlines in docstring are rendered as two characters: "\" + "n"
(escaped newline: \n), rather than a newline character. I would expect
a newline, it's more common that \n... But it may "break" inline
doctests rendering... Maybe it should be an option (render newlines as
newline character or escape them?), or maybe even let the user choose
how to render them using a callback (that's related to the "pluggable
formatter" question).

(*) Indentation is hardcoded to 4 spaces. What if I want 2 spaces or
something different? Should it become an option?

(*) Float infinity is replaces with 1e309. Again, maybe someone wants
to render this differently? It sounds like an arbitrary choice (which
"works" as expected).
msg357138 - (view) Author: Batuhan Taskaya (Batuhan Taskaya) Date: 2019-11-21 07:48
After PR 17302 is accepted, I'll work on refactorings including a precedence algorithm to find when to parentheses.
msg357176 - (view) Author: Batuhan (BTaskaya) * Date: 2019-11-21 15:08
@gvanrossum are you OK with adding type comments support? Current version loses type comment information so if typed_ast parses this, they wont be the same in AST representation.
msg357258 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2019-11-22 09:21
> @gvanrossum are you OK with adding type comments support? Current version loses type comment information so if typed_ast parses this, they wont be the same in AST representation.

Good catch, definitely do that!
msg357417 - (view) Author: Pablo Galindo Salgado (pablogsal) * (Python committer) Date: 2019-11-24 23:02
New changeset 27fc3b6f3fc49a36d3f962caac5c5495696d12ed by Pablo Galindo in branch 'master':
bpo-38870: Expose a function to unparse an ast object in the ast module (GH-17302)
https://github.com/python/cpython/commit/27fc3b6f3fc49a36d3f962caac5c5495696d12ed
msg357438 - (view) Author: miss-islington (miss-islington) Date: 2019-11-25 11:49
New changeset ded8888fbc33011dd39b7b1c86a5adfacc4943f3 by Miss Islington (bot) (Pablo Galindo) in branch 'master':
bpo-38870: Remove dependency on contextlib to avoid performance regression on import (GH-17376)
https://github.com/python/cpython/commit/ded8888fbc33011dd39b7b1c86a5adfacc4943f3
History
Date User Action Args
2019-11-25 14:25:53BTaskayasetpull_requests: + pull_request16859
2019-11-25 11:49:21miss-islingtonsetnosy: + miss-islington
messages: + msg357438
2019-11-25 11:29:23pablogsalsetpull_requests: + pull_request16858
2019-11-24 23:02:46pablogsalsetmessages: + msg357417
2019-11-22 09:21:49gvanrossumsetmessages: + msg357258
2019-11-21 21:00:42brett.cannonsetnosy: + brett.cannon
2019-11-21 15:08:05BTaskayasetnosy: + gvanrossum
messages: + msg357176
2019-11-21 07:48:21Batuhan Taskayasetnosy: + Batuhan Taskaya
messages: + msg357138
2019-11-20 23:01:49pablogsalsetmessages: + msg357111
2019-11-20 22:55:42levkivskyisetnosy: + levkivskyi
2019-11-20 22:55:23brandtbuchersetnosy: + brandtbucher
2019-11-20 22:44:42pablogsalsetkeywords: + patch
stage: patch review
pull_requests: + pull_request16794
2019-11-20 22:34:29pablogsalcreate