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

Merged
merged 2 commits into from Mar 25, 2012

Projects

None yet

3 participants

@doerwalter
Contributor

_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
Owner

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

Contributor

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)
Contributor

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.

Contributor
rkern commented Mar 23, 2012

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

Contributor

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

Owner

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 Mar 25, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment