I can't figure out how to avoid this doctest error:
Failed example:
print(test())
Expected:
output
<BLANKLINE>
Got:
output
<BLANKLINE>
For this code
def test():
r'''Produce string according to specification.
>>> print(test())
output
<BLANKLINE>
'''
return '\toutput\n'
I have put a tab literal into the source code, line 5 in front of output
.
It looks like doctest (or python docstrings?) ignores that tab literal and converts it to four spaces.
The so-called "Expected" value is literally not what my source specifies it to be.
What's a solution for this?
I don't want to replace the print statement with
>>> test()
'\toutput\n'
Because a huge part of why I usually like doctests is because they demonstrate examples, and the most important part of this function I am writing is the shape of the output.
The tabs in the docstring get expanded to 8 spaces, but the tabs in the output are not expanded.
From the doctest documentation (emphasis added):
Unfortunately, all examples of directive use in the doctest docs have been mangled by Sphinx. Here's how you would use
NORMALIZE_WHITESPACE
:Note that this treats all runs of whitespace as equal, rather than disabling the tab processing. Disabling the tab processing is absurdly cumbersome, and it's impossible through the usual doctest interface - you would need to subclass
DocTestParser
, manually reimplement doctest parsing, then construct instances ofDocTestFinder
,DocTestRunner
, and yourDocTestParser
subclass and call their methods manually.