Inspect how dedent treats mixed tab and space prefixes
Also published in our primary archive.
textwrap.dedent() does not treat a tab and four spaces as one shared indentation prefix. To inspect mixed indentation, print repr() of both the original text and the dedented result, then normalize only if your fixture policy explicitly says tabs represent a chosen number of spaces.
The input has \t before alpha and four literal spaces before beta. They may appear aligned in an editor configured with a four-column tab width, but they are different character sequences. Direct dedent() finds no common whitespace prefix and leaves both lines unchanged. The first two output lines expose that result rather than relying on visual alignment.
For this fixture, the declared policy is “a tab means four spaces.” expandtabs(4) applies that conversion before dedent(), producing 'alpha\nbeta\n'. The final assertion demonstrates the intended normalized control, but it does not establish that four columns is universally correct. A source format, linter, or protocol may require a different tab width—or prohibit tabs altogether. Keep that choice adjacent to the fixture so it is reviewable.
The textwrap.dedent documentation specifically notes that tabs and spaces are not equal when finding a common leading margin; str.expandtabs performs the separate expansion used here. These APIs are long established; Python 3.8+ is a practical minimum.
AI assistance disclosure: this article was drafted with AI assistance and uses a synthetic in-memory fixture.
from textwrap import dedent
text = "\talpha\n beta\n"
direct = dedent(text)
# Fixture policy: one tab represents four spaces.
normalized = dedent(text.expandtabs(4))
assert direct == text
assert normalized == "alpha\nbeta\n"
print(repr(text))
print(repr(direct))
print(repr(normalized))
'\talpha\n beta\n'
'\talpha\n beta\n'
'alpha\nbeta\n'