pprintpp icon indicating copy to clipboard operation
pprintpp copied to clipboard

Feature request: handle ast node pretty printing

Open kergoth opened this issue 10 years ago • 4 comments

Ideally, something more like http://alexleone.blogspot.com/2010/01/python-ast-pretty-printer.html than like ast.dump, but anything would be better than the default useless repr() of ast nodes :)

kergoth avatar Jan 19 '15 19:01 kergoth

Sounds awesome! Would definitely accept a PR which knows how to handle AST nodes.

wolever avatar Jan 19 '15 19:01 wolever

Notes to myself about potentially implementing this from my initial reading of the code:

  • [ ] Add new container type for _mk_open_close_empty_dict for an argument list, which is the same as dict, really, just with = separator between key and value rather than :, and unquoted keys.
  • [ ] Consider handling subclasses of handled types for _mk_open_close_empty_dict, wrapping open/close in a call to __class__.__name__(), so we can add AST rather than each and every node class. This could potentially give us frozenset and Counter for free, so we could remove them, and would also get us e.g. OrderedDict. Of course it assumes that it's constructed in the same way as its base class, but if that's not the case, it's easy enough to explicitly add those to the list. Of course, trivial subclasses are handled already to a certain extent, but it formats them with the base class's repr, which isn't ideal. Showing the name of each ast node is critical.

Alternatively, add the ability to punt and call out to an external formatting function for a particular container class, feeding it the current indent, to let it handle pretty formatting itself rather than knowing how to format everything ourselves, possibly passing along the formatting function to use for the contained objects.

kergoth avatar Jul 29 '15 19:07 kergoth

Woo!

I'm tentatively in favor of your proposed subclass handling, just so long as it won't break subclasses with custom repr methods (ex, if I have class MyOrderedDict(OrderedDict): def __repr__(self): return "weeee")

wolever avatar Jul 29 '15 21:07 wolever

Thanks, I'll get started on it and keep you posted. Appreciate the feedback.

kergoth avatar Jul 29 '15 21:07 kergoth