classification
Title: Output closing parenthesis in ast.dump() on separate line
Type: enhancement Stage: patch review
Components: Library (Lib) Versions: Python 3.9
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Anthony Sottile, benjamin.peterson, eric.smith, mark.dickinson, pablogsal, rhettinger, serhiy.storchaka, terry.reedy
Priority: normal Keywords: patch

Created on 2020-03-17 08:06 by serhiy.storchaka, last changed 2020-03-22 21:07 by Anthony Sottile.

Pull Requests
URL Status Linked Edit
PR 19039 open serhiy.storchaka, 2020-03-17 08:08
Messages (6)
msg364391 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2020-03-17 08:06
Currently ast.dump() in multiline mode (see issue37995) appends closing parenthesis to the end of the line:

>>> import ast
>>> node = ast.parse('spam(eggs, "and cheese")')
>>> print(ast.dump(node, indent=3))
Module(
   body=[
      Expr(
         value=Call(
            func=Name(id='spam', ctx=Load()),
            args=[
               Name(id='eggs', ctx=Load()),
               Constant(value='and cheese')],
            keywords=[]))],
   type_ignores=[])

It uses vertical space more efficiently (which is especially important on Windows console).

But I got a feedback about output closing parenthesis on separate lines (msg363783):

Module(
   body=[
      Expr(
         value=Call(
            func=Name(id='spam', ctx=Load()),
            args=[
               Name(id='eggs', ctx=Load()),
               Constant(value='and cheese')
            ],
            keywords=[]
         )
      )
   ],
   type_ignores=[]
)

It looks more "balanced", but less vertical space efficient. It adds almost 300 lines to 57 examples in Doc/library/ast.rst. And after omitting optional list arguments like keywords=[] and type_ignores=[] (see issue39981) the stairs of parenthesis will look even longer.

The proposed PR changes the output of ast.dump() by moving closing parenthesis on separate lines. I am still not sure what output is better.
msg364411 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2020-03-17 11:20
This feels like something that's very much down to personal preference. 

I also prefer the closing "]" and ")" characters on their own lines, but I'd be happy with an argument to ast.dump giving me that option.
msg364426 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2020-03-17 13:35
For what it's worth (which might not be much), here is what black produces:

Module(
    body=[
        Expr(
            value=Call(
                func=Name(id="spam", ctx=Load()),
                args=[Name(id="eggs", ctx=Load()), Constant(value="and cheese")],
                keywords=[],
            )
        )
    ],
    type_ignores=[],
)

I agree with Mark: it's all probably personal preference, and I'd be okay either way.
msg364427 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2020-03-17 13:38
Ah yes; looking at the `black` output, there's also an opportunity for an exciting discussion about trailing commas here :-)
msg364457 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2020-03-17 17:19
The current output looks like Python code.  The proposed revision looks more like C, and I find the example above less readable with the prominence given to what is close to noise.  The difference is part of the reason I left C for Python over 2 decades ago.  Please make the alternative an option.  The preferred form depends on the person and possibly the AST.  (The example in Pablo's message is quite different.)
msg364824 - (view) Author: Anthony Sottile (Anthony Sottile) * Date: 2020-03-22 21:07
fwiw, astpretty's output looks like the black output: https://github.com/asottile/astpretty

>>> astpretty.pprint(ast.parse('if x == y: y += 4').body[0])
If(
    lineno=1,
    col_offset=0,
    test=Compare(
        lineno=1,
        col_offset=3,
        left=Name(lineno=1, col_offset=3, id='x', ctx=Load()),
        ops=[Eq()],
        comparators=[Name(lineno=1, col_offset=8, id='y', ctx=Load())],
    ),
    body=[
        AugAssign(
            lineno=1,
            col_offset=11,
            target=Name(lineno=1, col_offset=11, id='y', ctx=Store()),
            op=Add(),
            value=Num(lineno=1, col_offset=16, n=4),
        ),
    ],
    orelse=[],
)
History
Date User Action Args
2020-03-22 21:07:55Anthony Sottilesetnosy: + Anthony Sottile
messages: + msg364824
2020-03-17 17:19:06terry.reedysetmessages: + msg364457
2020-03-17 13:38:53mark.dickinsonsetmessages: + msg364427
2020-03-17 13:35:09eric.smithsetnosy: + eric.smith
messages: + msg364426
2020-03-17 11:20:00mark.dickinsonsetnosy: + mark.dickinson
messages: + msg364411
2020-03-17 08:08:56serhiy.storchakasetkeywords: + patch
stage: patch review
pull_requests: + pull_request18390
2020-03-17 08:06:12serhiy.storchakacreate