Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from Mar 25, 2012
Merged

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

merged 2 commits into from Mar 25, 2012

Conversation

doerwalter
Copy link
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().

_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().
@takluyver
Copy link
Member

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

@doerwalter
Copy link
Contributor Author

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
Copy link
Contributor Author

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
Copy link
Contributor

rkern commented Mar 23, 2012

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

@doerwalter
Copy link
Contributor Author

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

@takluyver
Copy link
Member

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

takluyver added a commit that referenced this pull request Mar 25, 2012
Fix indentation bug in IPython/lib/pretty.py
@takluyver takluyver merged commit aff7498 into ipython:master Mar 25, 2012
mattvonrocketstein pushed a commit to mattvonrocketstein/ipython that referenced this pull request Nov 3, 2014
Fix indentation bug in IPython/lib/pretty.py
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants