Author serhiy.storchaka
Recipients benjamin.peterson, brett.cannon, rhettinger, serhiy.storchaka, yselivanov
Date 2019-08-31.09:29:27
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1567243768.69.0.362429469264.issue37995@roundup.psfhosted.org>
In-reply-to
Content
ast.dump() is mainly useful for debugging purposes. Unfortunately the output is too long and complex even for simple examples. It contains too much nested calls and lists. 

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

It is worse if include more information:

>>> print(ast.dump(node, include_attributes=True))
Module(body=[Expr(value=Call(func=Name(id='spam', ctx=Load(), lineno=1, col_offset=0, end_lineno=1, end_col_offset=4), args=[Name(id='eggs', ctx=Load(), lineno=1, col_offset=5, end_lineno=1, end_col_offset=9), Constant(value='and cheese', kind=None, lineno=1, col_offset=11, end_lineno=1, end_col_offset=23)], keywords=[], lineno=1, col_offset=0, end_lineno=1, end_col_offset=24), lineno=1, col_offset=0, end_lineno=1, end_col_offset=24)], type_ignores=[])

And for larger examples it is almost unusable.

I propose to make ast.dump() producing a multiline indented output. Add the optional "indent" parameter. If it is a non-negative number or a string, the output if formatted with the specified indentation. If it is None (by default), the output is a single string.

>>> 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',
                  kind=None)],
            keywords=[]))],
   type_ignores=[])

Looks better, no?

I am not sure about closing parenthesis. Should they be attached to the last item (as above) or split on a separate line (as below)? Or use some heuristic to make the output more readable and compact?

>>> 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',
                  kind=None
               )
            ],
            keywords=[]
         )
      )
   ],
   type_ignores=[]
)
History
Date User Action Args
2019-08-31 09:29:28serhiy.storchakasetrecipients: + serhiy.storchaka, brett.cannon, rhettinger, benjamin.peterson, yselivanov
2019-08-31 09:29:28serhiy.storchakasetmessageid: <1567243768.69.0.362429469264.issue37995@roundup.psfhosted.org>
2019-08-31 09:29:28serhiy.storchakalinkissue37995 messages
2019-08-31 09:29:27serhiy.storchakacreate