pylint-bot / test

0 stars 0 forks source link

Provide a way to print the complete state of astroid objects #178

Closed pylint-bot closed 8 years ago

pylint-bot commented 9 years ago

Originally reported by: BitBucket: ceridwenv, GitHub: @ceridwen?


While working on my patch, I've lots of times had to resort to vars() and similar built-ins to get information about the state of node classes, inference contexts, and the like. The __repr__ and __str__ methods for the node classes as currently written aren't adequate because they don't print out all the attributes. Inference contexts don't have either and vars() also doesn't work because of slots. dump() gives information about the nodes and some of their attributes, but doesn't play well with pdb (line breaks end up as '\n' literals, which makes the output hard to read) and is also not complete. I think at a minimum, every object should have a method that will print out its complete (non-recursive) state in a format that pdb understands. dump() should be changed to format correctly in pdb and possibly to include more information than it currently does. Maybe the best way to do this is to add an optional parameter that gives a verbosity level? For other recursive data structures, a pprint-equivalent would be good.


pylint-bot commented 9 years ago

Original comment by BitBucket: ceridwenv, GitHub: @ceridwen?:


Are there other objects that need some form of legible representation other than nodes and contexts? Should I use __str__, __repr__, both, or another method for complete-state representations?

pylint-bot commented 9 years ago

Original comment by Claudiu Popa (BitBucket: PCManticore, GitHub: @PCManticore?):


I think you could use __str__ or maybe another method for complete-state representations. Usually when I try things with astroid, I do it in an interpreter session and it would be cumbersome to have the complete representation each time I do something like the following.

#!python

>>> node = astroid.parse('...')
>>> node
# Oups, prints a lot of stuff, instead of printing the tree node.
pylint-bot commented 9 years ago

Original comment by BitBucket: ceridwenv, GitHub: @ceridwen?:


This is taking longer than I'd hoped to get right, mainly because the formatting is hard to get optimally legible. How do you feel about my using the 3.4 functools.singledispatch library and the 2.7 backport on PyPi to handle the type dispatching in as_string.dump? I've done some more refactoring of the fields, moving what I'd term as AST-dependent fields like _style and locals into _other_other_fields for lack of a better name. I'm currently using repr_tree as the method, I may add another method if I can figure out a reasonable representation of the AST fields.

pylint-bot commented 9 years ago

Original comment by Claudiu Popa (BitBucket: PCManticore, GitHub: @PCManticore?):


Sounds good! I'm fine with using singledispatch.

pylint-bot commented 9 years ago

Original comment by Claudiu Popa (BitBucket: PCManticore, GitHub: @PCManticore?):


Does this needs to be open or can we consider it fixed?

pylint-bot commented 9 years ago

Original comment by BitBucket: ceridwenv, GitHub: @ceridwen?:


Are there any other important cases of objects with state in astroid? I did the ones I know about, but I'm not sure if there are any others. If there aren't, I'd say this is finished.

pylint-bot commented 9 years ago

Original comment by Claudiu Popa (BitBucket: PCManticore, GitHub: @PCManticore?):


I think it's done. There's still the CallContext object, but we have #194 for it.