By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,185 Members | 1,533 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,185 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Nov 22 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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
Nov 22 '05 #2

P: n/a
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
Nov 22 '05 #3

P: n/a
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
Nov 22 '05 #4

P: n/a
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
Nov 22 '05 #5

P: n/a
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
Nov 22 '05 #6

P: n/a
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
Nov 22 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.