Skip to content

Loading…

Fix indentation bug in IPython/lib/pretty.py #1517

Merged
merged 2 commits into from

3 participants

@doerwalter

_PrettyPrinterBase.group() was increasing the indentation twice
(once in the call to begin_group() and once in the call to indent()).
Fixed by removing the call to indent().

@doerwalter doerwalter Fix double indentation in _PrettyPrinterBase.group().
_PrettyPrinterBase.group() was increasing the indentation twice
(once in the call to begin_group() and once in the call to indent()).
Fixed by removing the call to indent().
0a38ad0
@takluyver
IPython: interactive computing in Python member

Can you give an example that shows the bug? Also, if possible, a unit test for this would be good.

@doerwalter

Try the following class:

class MyList(object):
    def __init__(self, content):
        self.content = content
    def _repr_pretty_(self, p, cycle):
        if cycle:
            p.text("MyList(...)")
        else:
            with p.group(3, "MyList(", ")"):
                for (i, child) in enumerate(self.content):
                    if i:
                        p.text(",")
                    p.breakable()
                    p.pretty(child)

Without the patch the output is indented six spaces, although the code specifies 3:

In [1]: from gurk import *
In [2]: MyList(range(30))
Out[2]: 
MyList(
      0,
      1,
      2,
      3,
      4,
      5,
      6,
      7,
      8,
      9,
      10,
      11,
      12,
      13,
      14,
      15,
      16,
      17,
      18,
      19,
      20,
      21,
      22,
      23,
      24,
      25,
      26,
      27,
      28,
      29)

With the patch the output is indented 3 spaces:

In [1]: from gurk import *
In [2]: MyList(range(30))
Out[2]: 
MyList(
   0,
   1,
   2,
   3,
   4,
   5,
   6,
   7,
   8,
   9,
   10,
   11,
   12,
   13,
   14,
   15,
   16,
   17,
   18,
   19,
   20,
   21,
   22,
   23,
   24,
   25,
   26,
   27,
   28,
   29)
@doerwalter

Also here is a test:

from IPython.lib import pretty

class MyList(object):
    def __init__(self, content):
        self.content = content
    def _repr_pretty_(self, p, cycle):
        if cycle:
            p.text("MyList(...)")
        else:
            with p.group(3, "MyList(", ")"):
                for (i, child) in enumerate(self.content):
                    if i:
                        p.text(",")
                        p.breakable()
                    else:
                        p.breakable("")
                    p.pretty(child)

gotoutput = pretty.pretty(MyList(range(30)))
expectedoutput = "MyList(\n" + ",\n".join("   %d" % i for i in range(30)) + ")"

assert gotoutput == expectedoutput

However I don't know where I should put the test.

@rkern

A new test_pretty.py in IPython/lib/tests/ would be a good place to put the test.

@doerwalter

OK, I've added the test to the new test_pretty.py file.

@takluyver
IPython: interactive computing in Python member

OK, this all looks OK, so I'm going to go ahead and merge it. Thanks, @doerwalter.

@takluyver takluyver merged commit aff7498 into ipython:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 23, 2012
  1. @doerwalter

    Fix double indentation in _PrettyPrinterBase.group().

    doerwalter committed
    _PrettyPrinterBase.group() was increasing the indentation twice
    (once in the call to begin_group() and once in the call to indent()).
    Fixed by removing the call to indent().
  2. @doerwalter
This page is out of date. Refresh to see the latest.
Showing with 50 additions and 2 deletions.
  1. +1 −2 IPython/lib/pretty.py
  2. +49 −0 IPython/lib/tests/test_pretty.py
View
3 IPython/lib/pretty.py
@@ -155,8 +155,7 @@ def group(self, indent=0, open='', close=''):
"""like begin_group / end_group but for the with statement."""
self.begin_group(indent, open)
try:
- with self.indent(indent):
- yield
+ yield
finally:
self.end_group(indent, close)
View
49 IPython/lib/tests/test_pretty.py
@@ -0,0 +1,49 @@
+"""Tests for IPython.lib.pretty.
+"""
+#-----------------------------------------------------------------------------
+# Copyright (c) 2011, the IPython Development Team.
+#
+# Distributed under the terms of the Modified BSD License.
+#
+# The full license is in the file COPYING.txt, distributed with this software.
+#-----------------------------------------------------------------------------
+
+#-----------------------------------------------------------------------------
+# Imports
+#-----------------------------------------------------------------------------
+from __future__ import print_function
+
+# Third-party imports
+import nose.tools as nt
+
+# Our own imports
+from IPython.lib import pretty
+
+#-----------------------------------------------------------------------------
+# Classes and functions
+#-----------------------------------------------------------------------------
+
+class MyList(object):
+ def __init__(self, content):
+ self.content = content
+ def _repr_pretty_(self, p, cycle):
+ if cycle:
+ p.text("MyList(...)")
+ else:
+ with p.group(3, "MyList(", ")"):
+ for (i, child) in enumerate(self.content):
+ if i:
+ p.text(",")
+ p.breakable()
+ else:
+ p.breakable("")
+ p.pretty(child)
+
+
+def test_indentation():
+ """Test correct indentation in groups"""
+ count = 40
+ gotoutput = pretty.pretty(MyList(range(count)))
+ expectedoutput = "MyList(\n" + ",\n".join(" %d" % i for i in range(count)) + ")"
+
+ nt.assert_equals(gotoutput, expectedoutput)
Something went wrong with that request. Please try again.