Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: handle ast node pretty printing #14

Open
kergoth opened this issue Jan 19, 2015 · 4 comments
Open

Feature request: handle ast node pretty printing #14

kergoth opened this issue Jan 19, 2015 · 4 comments

Comments

@kergoth
Copy link

kergoth commented Jan 19, 2015

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 :)

@wolever
Copy link
Owner

wolever commented Jan 19, 2015

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

@kergoth
Copy link
Author

kergoth commented Jul 29, 2015

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.

@wolever
Copy link
Owner

wolever commented Jul 29, 2015

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")

@kergoth
Copy link
Author

kergoth commented Jul 29, 2015

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants