Repository navigation
Description
See for instance this StackOverflow question. There are two problems: subclassing a type that is normally pretty-printed in some cases loses the pretty-printing, and adding a custom __repr__ to a class that does not define its own pretty-printing has no effect.
Here is a simple illustration of the behavior:
In [1]: from collections import OrderedDict
...:
...: class Ugly(OrderedDict):
...: pass
...:
...: class Pretty(OrderedDict):
...: def __repr__(self):
...: return "I feel pretty!"
...:
In [2]: OrderedDict(a=1, b=2, c=3, d=4, e=5, f=6, g=8)
Out[2]:
OrderedDict([('a', 1),
('c', 3),
('b', 2),
('e', 5),
('d', 4),
('g', 8),
('f', 6)])
In [3]: Ugly(a=1, b=2, c=3, d=4, e=5, f=6, g=8)
Out[3]: Ugly([('a', 1), ('c', 3), ('b', 2), ('e', 5), ('d', 4), ('g', 8), ('f', 6)])
In [4]: Pretty(a=1, b=2, c=3, d=4, e=5, f=6, g=8)
Out[4]: Pretty([('a', 1), ('c', 3), ('b', 2), ('e', 5), ('d', 4), ('g', 8), ('f', 6)])
The output of OrderedDict is prettified. Ugly inherits from OrderedDict with no modifications, but is not prettified. Pretty inherits from OrderedDict and tries to make its own prettier __repr__, but it has no effect. To add insult to injury, the output for Pretty is not only not the output Pretty defined for itself, but is even uglier than the output for the plain OrderedDict.
The thing is that using a special pretty-printer is only better when you know what __repr__ it's replacing. Otherwise you may be replacing a pretty __repr__ with an uglier one. The pretty printer is better than the default __repr__ for something like OrderedDict, but if someone writes their own __repr__, they're saying they want that to be used instead of the default, and it's irritating to have IPython override that. It is also especially annoying that the pretty-printing behavior prettifies the output of, say, OrderedDict, but does not prettify the output of a user-defined subclass of OrderedDict, and yet still blocks the user from doing so by overriding __repr__.
I'm not 100% sure, but I'm tempted to say that the right solution is to only use the pretty printer if the class defines a _repr_pretty, or if the __repr__ is defined by a hand-curated list of "known ugly printers" (e.g., OrderedDict). If a user-defined class defines its own __repr__ (and doesn't define _repr_pretty), then that should always be used.
The trick is that, to do this right, we would need to not just check the object's class, but actually determine which class is defining the __repr__. That is, if a subclass of OrderedDict doesn't override __repr__, the pretty printer should be used; if the subclass does override __repr__, the custom __repr__ should be used instead of the pretty-printer. So the order of priority is:
_repr_pretty__repr__defined in a class that we don't know is ugly.- Default pretty print
__repr__defined in a class that we know is ugly
Activity
Carreau commentedon May 28, 2017
Hum, I see where you are coming from, and I see where this would be useful,
but at the same time, that means that if the pretty_repr is doing the right thing, then you don't get it for free anymore – which is sad. If you are bothering with implementing a
__repr__then you are likely also responsible to re-implement other rich display methods.Using
__repr__instead of__repr_pretty__if__repr__is on a subclass also seem like the wrong thing to do as_repr_pretty_is almost always doing more than repr (like trying to recursively see inside containers and pretty_print) which__repr__cannot do.That would also break expected API for project that do rely on the behavior, so if change there is it will likely be for the next major version (which is not soon).
Regardless of what we choose, it might be a good idea to document the behavior, and potentially trigger a warning if we detect that something may be wrong.
BrenBarn commentedon May 29, 2017
Hmmm, what cases do you have in mind where the user overrides repr, but pretty_repr is doing the right thing? To me it seems basic that things like pretty_repr should only be in addition to the basic behavior provided by Python. If someone writes a class that works with plain Python, it should work the same way with IPython unless they choose to explicitly support whatever extra bells and whistles IPython makes available.
Right now this isn't the case: writing a class that defines a
__repr__causes the class to break in IPython. You can of course globally disable pretty printing, but that doesn't seem like the right solution. "Pretty printing enabled" should mean "pretty printing is enabled for classes that explicitly support it", not "pretty printing overrides__repr__even for classes that weren't written with IPython in mind". In other words, the current behavior is already breaking the expected API --- that API being the API of Python itself, which defines the behavior of__repr__.So I think I disagree that writing a
__repr__makes you responsible for "other rich display methods", because__repr__isn't a rich display method; it's a built-in feature of Python. It's great for IPython to add rich display methods, but we can't assume that any class that has a__repr__is even aware of IPython, let alone wants to hook into its rich display mechanism.takluyver commentedon May 30, 2017
Yeah, I think I agree -
__repr__defined in a subclass should take precedence over pretty-printers defined for the parent class. Even if the subclass'__repr__is less pretty, it's probably more correct.takluyver commentedon May 30, 2017
i.e. the search algorithm for finding how to represent a class should look like this:
_repr_pretty_defined on that type (not a parent class)__repr__defined on that type (not a parent class)