This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: ast.unparse: visually better code generation
Type: enhancement Stage: patch review
Components: Library (Lib) Versions: Python 3.11
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: BTaskaya Nosy List: BTaskaya, pablogsal
Priority: normal Keywords: patch

Created on 2021-05-15 13:29 by BTaskaya, last changed 2022-04-11 14:59 by admin.

Pull Requests
URL Status Linked Edit
PR 26156 merged BTaskaya, 2021-05-15 23:26
Messages (1)
msg393714 - (view) Author: Batuhan Taskaya (BTaskaya) * (Python committer) Date: 2021-05-15 13:29
This issue is for tracking possible places where we could generate better code on ast.unparse (better as in more closely to what people are actually writing than our naive implementation). 

Examples;
>>> import ast
>>> ast.unparse(ast.parse('a, b, c = [1,2,3]'))
'(a, b, c) = [1, 2, 3]'

could be
>>> ast.unparse(ast.parse('a, b, c = [1,2,3]'))
'a, b, c = [1, 2, 3]'

OR
>>> print(ast.unparse(ast.parse('if value := d.get("something"): print(value)')))
if (value := d.get('something')):
    print(value)

could be
>>> print(ast.unparse(ast.parse('if value := d.get("something"): print(value)')))
if value := d.get('something'):
    print(value)

We could even go further with the long line unpacking (which would definitely require some sort of clever algorithm for nested structures). 

>>> source = '[\n' + '\tsomething,\n' * 20 + ']'
>>> print(source)
[
        something,
        something,
        something,
        something,
        something,
        something,
        something,
        something,
        something,
        something,
        something,
        something,
        something,
        something,
        something,
        something,
        something,
        something,
        something,
        something,
]
>>> print(ast.unparse(ast.parse(source)))
[something, something, something, something, something, something, something, something, something, something, something, something, something, something, something, something, something, something, something, something]
History
Date User Action Args
2022-04-11 14:59:45adminsetgithub: 88308
2021-05-15 23:26:57BTaskayasetkeywords: + patch
stage: patch review
pull_requests: + pull_request24790
2021-05-15 13:29:46BTaskayacreate