Skip to content
This repository
Browse code

Merge pull request #1495 from rkern/fix-hyperobject-pprint

BUG: Fix pretty-printing for overzealous objects

Some classes use __getattr__ to automatically create requested attributes. The recent trend of LINQ-like query objects is what I ran into. This confuses the pretty-printing machinery that tests for _repr_pretty_. This pull request fixes many of these problems by simply testing if the _repr_pretty_ attribute is callable. This may still be confused by certain mocking frameworks, but it's a good start.
  • Loading branch information...
commit 873e364771bf39acbf1b8332ed0fdb14389e1a3b 2 parents 3912ea7 + 1f5362d
Min RK minrk authored
17 IPython/core/tests/test_formatters.py
@@ -10,6 +10,7 @@
10 10 import nose.tools as nt
11 11
12 12 from IPython.core.formatters import FormatterABC, PlainTextFormatter
  13 +from IPython.lib import pretty
13 14
14 15 class A(object):
15 16 def __repr__(self):
@@ -19,6 +20,16 @@ class B(A):
19 20 def __repr__(self):
20 21 return 'B()'
21 22
  23 +class BadPretty(object):
  24 + _repr_pretty_ = None
  25 +
  26 +class GoodPretty(object):
  27 + def _repr_pretty_(self, pp, cycle):
  28 + pp.text('foo')
  29 +
  30 + def __repr__(self):
  31 + return 'GoodPretty()'
  32 +
22 33 def foo_printer(obj, pp, cycle):
23 34 pp.text('foo')
24 35
@@ -27,9 +38,15 @@ def test_pretty():
27 38 f.for_type(A, foo_printer)
28 39 nt.assert_equals(f(A()), 'foo')
29 40 nt.assert_equals(f(B()), 'foo')
  41 + nt.assert_equals(f(GoodPretty()), 'foo')
  42 + # Just don't raise an exception for the following:
  43 + f(BadPretty())
  44 +
30 45 f.pprint = False
31 46 nt.assert_equals(f(A()), 'A()')
32 47 nt.assert_equals(f(B()), 'B()')
  48 + nt.assert_equals(f(GoodPretty()), 'GoodPretty()')
  49 +
33 50
34 51 def test_deferred():
35 52 f = PlainTextFormatter()
6 IPython/lib/pretty.py
@@ -347,7 +347,11 @@ def pretty(self, obj):
347 347 return printer(obj, self, cycle)
348 348 # Finally look for special method names.
349 349 if hasattr(obj_class, '_repr_pretty_'):
350   - return obj_class._repr_pretty_(obj, self, cycle)
  350 + # Some objects automatically create any requested
  351 + # attribute. Try to ignore most of them by checking for
  352 + # callability.
  353 + if callable(obj_class._repr_pretty_):
  354 + return obj_class._repr_pretty_(obj, self, cycle)
351 355 return _default_pprint(obj, self, cycle)
352 356 finally:
353 357 self.end_group()

0 comments on commit 873e364

Please sign in to comment.
Something went wrong with that request. Please try again.