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.

Author serhiy.storchaka
Recipients Anthony Sottile, benjamin.peterson, brett.cannon, rhettinger, serhiy.storchaka, terry.reedy, xtreak, yselivanov
Date 2019-09-07.08:57:25
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1567846646.03.0.23546452246.issue37995@roundup.psfhosted.org>
In-reply-to
Content
> It would be great is the tool wasn't tightly bound to our particular AST and could be used for any hand-rolled AST.

I do not think it is possible. Every tree has its specifics: what types of nodes are supported (AST, list and primitive immutable values), what children the node can have (node._fields and node._attributes), how to get a child (getattr(node, name), but can be absent), what is the node name (node.__class__.__name__), how to represent nodes (constructor, list display and literals for primitives), what options are supported (annotate_fields, include_attributes and indent), what nodes are simple and what are complex. All these details are different in every AST implementation. Recursive walking the tree is trivial in general, handling specifics makes every foramating function unique.

What was interesting in the first Raymond's implementation, is that some simple nodes are written in one line. Its code can't be used with ast.AST because of different tree structure and different meaning of simple nodes (and also nodes with a single child are very rare), but PR 15631 has been updated to produce more compact output by using other heuristics:

>>> 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-09-07 08:57:26serhiy.storchakasetrecipients: + serhiy.storchaka, brett.cannon, rhettinger, terry.reedy, benjamin.peterson, yselivanov, Anthony Sottile, xtreak
2019-09-07 08:57:26serhiy.storchakasetmessageid: <1567846646.03.0.23546452246.issue37995@roundup.psfhosted.org>
2019-09-07 08:57:26serhiy.storchakalinkissue37995 messages
2019-09-07 08:57:25serhiy.storchakacreate