[Python-Dev] doctest, unicode repr, and 2to3

Georg Brandl g.brandl at gmx.net
Fri Mar 5 23:01:23 CET 2010


Am 05.03.2010 20:37, schrieb Terry Reedy:
> On 3/4/2010 11:11 PM, "Martin v. Löwis" wrote:
>> Johan Harjano ran into an interesting problem when trying to run the
>> Django test suite under Python 3.1.
>>
>> Django has doctests of the form
>>
>>>>> a6.headline
>> u'Default headline'
>>
>> Even when converting the doctest with 2to3, the expected output is
>> unmodified. However, in 3.x, the expected output will change (i.e. not
>> produce an u"" prefix anymore).
>>
>> Now, it might be possible to reformulate the test case (e.g. use print()
>> instead of relying on repr), however, this is undesirable as a) the test
>> should continue to test in 2.x that the result object is a unicode
>> string, and b) it makes the test less readable.
>>
>> I would like to find a solution where this gets automatically corrected,
>> e.g. through 2to3, or through changes to doctest, or through changes of
>> str.__repr__.
>>
>> Any proposal appreciated.
> 
> What is the easiest thing that works?
> 
> If 2to3 can fix string literals and fix code within doc strings, would 
> it be difficult to fix expected strings within doc strings?

Yes.  Expected output from doctests can be anything.  Doing conversions
on it that are correct only for Python code is potentially producing many
false positives.  Heuristics need to be applied that can get very intricate.

> On the otherhand, as Foord pointed out,

Prefect?

> the 'u' prefix is something of a 
> CPythonism not required by the language def, so an 'ignore leading u on 
> expected output' flag would be useful. But 'correcting'
> 
>  >>> a,b
> u'ah', u'hah'
> 
> is too much to expect. Doctest is both useful and fragile because it 
> *almost* imitates human testing methods.

Yes, it is documentation and tests at the same time :)

Georg



More information about the Python-Dev mailing list