Skip to content

Commit

Permalink
Merge pull request ipython#2123 from rkern/fix-pretty
Browse files Browse the repository at this point in the history
BUG: Look up the `_repr_pretty_` method on the class using the MRO
  • Loading branch information
bfroehle committed Jul 14, 2012
2 parents 55124a8 + fece557 commit b5feb48
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
4 changes: 2 additions & 2 deletions IPython/lib/pretty.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,8 +353,8 @@ def pretty(self, obj):
# Some objects automatically create any requested
# attribute. Try to ignore most of them by checking for
# callability.
if '_repr_pretty_' in obj_class.__dict__:
meth = obj_class._repr_pretty_
if '_repr_pretty_' in cls.__dict__:
meth = cls._repr_pretty_
if callable(meth):
return meth(obj, self, cycle)
return _default_pprint(obj, self, cycle)
Expand Down
19 changes: 19 additions & 0 deletions IPython/lib/tests/test_pretty.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ def _repr_pretty_(self, p, cycle):
p.text("MyDict(...)")


class Dummy1(object):
def _repr_pretty_(self, p, cycle):
p.text("Dummy1(...)")

class Dummy2(Dummy1):
_repr_pretty_ = None


def test_indentation():
"""Test correct indentation in groups"""
count = 40
Expand All @@ -63,3 +71,14 @@ def test_dispatch():
expectedoutput = "MyDict(...)"

nt.assert_equals(gotoutput, expectedoutput)


def test_callability_checking():
"""
Test that the _repr_pretty_ method is tested for callability and skipped if
not.
"""
gotoutput = pretty.pretty(Dummy2())
expectedoutput = "Dummy1(...)"

nt.assert_equals(gotoutput, expectedoutput)

0 comments on commit b5feb48

Please sign in to comment.