From 9fbb32b9dc7fc7075aae6b7be7c1b0642abbb8f0 2017-12-21 13:24:44 From: Thomas Kluyver Date: 2017-12-21 13:24:44 Subject: [PATCH] Ensure that __repr__() methods override pretty printers for parent classes Closes gh-10950 --- diff --git a/IPython/lib/pretty.py b/IPython/lib/pretty.py index cbbb726..af7feb9 100644 --- a/IPython/lib/pretty.py +++ b/IPython/lib/pretty.py @@ -392,6 +392,10 @@ class RepresentationPrinter(PrettyPrinter): meth = cls._repr_pretty_ if callable(meth): return meth(obj, self, cycle) + if cls is not object \ + and callable(cls.__dict__.get('__repr__')): + return _repr_pprint(obj, self, cycle) + return _default_pprint(obj, self, cycle) finally: self.end_group() diff --git a/IPython/lib/tests/test_pretty.py b/IPython/lib/tests/test_pretty.py index 6d65743..0229dbe 100644 --- a/IPython/lib/tests/test_pretty.py +++ b/IPython/lib/tests/test_pretty.py @@ -420,4 +420,18 @@ def test_function_pretty(): return "Don't panic" nt.assert_in('meaning_of_life(question=None)', pretty.pretty(meaning_of_life)) - + + +class OrderedCounter(Counter, OrderedDict): + 'Counter that remembers the order elements are first encountered' + + def __repr__(self): + return '%s(%r)' % (self.__class__.__name__, OrderedDict(self)) + + def __reduce__(self): + return self.__class__, (OrderedDict(self),) + +def test_custom_repr(): + """A custom repr should override a pretty printer for a parent type""" + oc = OrderedCounter("abracadabra") + nt.assert_in("OrderedCounter(OrderedDict", pretty.pretty(oc))