textwrap.dedent() drops tabs - bug or feature?

Discussion in 'Python' started by Steven Bethard, Nov 18, 2005.

  1. So I've recently been making pretty frequent use of textwrap.dedent() to
    allow me to use triple-quoted strings at indented levels of code without
    getting the extra spaces prefixed to each line. I discovered today that
    not only does textwrap.dedent() strip any leading spaces, but it also
    substitutes any internal tabs with spaces. For example::

    py> def test():
    .... x = ('abcd efgh\n'
    .... 'ijkl mnop\n')
    .... y = textwrap.dedent('''\
    .... abcd efgh
    .... ijkl mnop
    .... ''')
    .... return x, y
    ....
    py> test()
    ('abcd\tefgh\nijkl\tmnop\n', 'abcd efgh\nijkl mnop\n')

    Note that even though the tabs are internal, they are still removed by
    textwrap.dedent(). The documentation[1] says:

    """
    dedent(text)
    Remove any whitespace that can be uniformly removed from the left
    of every line in text.

    This is typically used to make triple-quoted strings line up with
    the left edge of screen/whatever, while still presenting it in the
    source code in indented form.
    """

    So it looks to me like even if this is a "feature" it is undocumented.
    I'm planning on filing a bug report, but I wanted to check here first in
    case I'm just smoking something.

    STeVe

    [1] http://docs.python.org/lib/module-textwrap.html
     
    Steven Bethard, Nov 18, 2005
    #1
    1. Advertising

  2. Steven Bethard

    Peter Hansen Guest

    Steven Bethard wrote:
    > Note that even though the tabs are internal, they are still removed by
    > textwrap.dedent(). The documentation[1] says:

    ....
    > So it looks to me like even if this is a "feature" it is undocumented.
    > I'm planning on filing a bug report, but I wanted to check here first in
    > case I'm just smoking something.


    While I wouldn't say it's obvious, I believe it is (indirectly?)
    documented and deliberate.

    Search for this in the docs:
    """
    expand_tabs
    (default: True) If true, then all tab characters in text will be
    expanded to spaces using the expandtabs() method of text.
    """

    (This is not to say a specific rewording of the docs to lessen any
    confusion in this area wouldn't be welcomed.)

    -Peter
     
    Peter Hansen, Nov 19, 2005
    #2
    1. Advertising

  3. Peter Hansen wrote:
    > Steven Bethard wrote:
    >
    >> Note that even though the tabs are internal, they are still removed by
    >> textwrap.dedent(). The documentation[1] says:

    >
    > ...
    >
    >> So it looks to me like even if this is a "feature" it is undocumented.
    >> I'm planning on filing a bug report, but I wanted to check here first
    >> in case I'm just smoking something.

    >
    > While I wouldn't say it's obvious, I believe it is (indirectly?)
    > documented and deliberate.
    >
    > Search for this in the docs:
    > """
    > expand_tabs
    > (default: True) If true, then all tab characters in text will be
    > expanded to spaces using the expandtabs() method of text.
    > """


    Thanks for double-checking this for me. I looked at expand_tabs, and
    it's part of the definition of the TextWrapper class, which is not
    actually used by textwrap.dedent(). So I think the textwrap.dedent()
    expanding-of-tabs behavior is still basically undocumented.

    I looked at the source code, and the culprit is the first line of the
    function definition:

    lines = text.expandtabs().split('\n')

    I filed a bug_ report, but left the Category unassigned so that someone
    else can decide whether it's a doc bug or a code bug.

    STeVe

    ... _bug: http://python.org/sf/1361643
     
    Steven Bethard, Nov 19, 2005
    #3
  4. Steven Bethard

    Peter Hansen Guest

    Steven Bethard wrote:
    > Thanks for double-checking this for me. I looked at expand_tabs, and
    > it's part of the definition of the TextWrapper class, which is not
    > actually used by textwrap.dedent(). So I think the textwrap.dedent()
    > expanding-of-tabs behavior is still basically undocumented.


    Ah, good point. I saw dedent() in there with the wrap() and fill()
    convenience functions which use a TextWrapper internally, but you're
    quite right that dedent() is different, and in fact merely uses the
    expandtabs() functionality of the standard string class, so this has
    nothing to do with the expand_tabs attribute I pointed out.

    -Peter
     
    Peter Hansen, Nov 19, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Razor
    Replies:
    7
    Views:
    735
    Razor
    Apr 8, 2004
  2. qwweeeit
    Replies:
    2
    Views:
    651
    qwweeeit
    Dec 14, 2005
  3. Tom Plunket

    textwrap.dedent replaces tabs?

    Tom Plunket, Dec 16, 2006, in forum: Python
    Replies:
    15
    Views:
    485
    Tom Plunket
    Dec 30, 2006
  4. rantingrick

    Tabs -vs- Spaces: Tabs should have won.

    rantingrick, Jul 16, 2011, in forum: Python
    Replies:
    95
    Views:
    1,846
    Roy Smith
    Jul 19, 2011
  5. John Kopanas
    Replies:
    2
    Views:
    295
    Gregory Brown
    Jan 29, 2007
Loading...

Share This Page