Django: Ticket #4530: doctests don't play well with i18n: they override the value of _ builtinhttps://code.djangoproject.com/ticket/4530
<p>
Doctests are being run in Python's interactive mode, which means -- among other things -- that any expresion returning a value other than None will overwrite the value of the _ builtin. This causes problems if you are trying to test views that use _().
</p>
<p>
Running the doctests from the attached tests.py file results in an exception:
</p>
<pre class="wiki">======================================================================
FAIL: Doctest: doctestproject.doctestapp.tests
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/marcink/checkout/django/django/test/_doctest.py", line 2170, in runTest
raise self.failureException(self.format_failure(new.getvalue()))
AssertionError: Failed doctest test for doctestproject.doctestapp.tests
File "/home/marcink/tmp/doctestproject/doctestapp/tests.py", line 0, in tests
----------------------------------------------------------------------
File "/home/marcink/tmp/doctestproject/doctestapp/tests.py", line 9, in doctestproject.doctestapp.tests
Failed example:
_('Test marked for translation')
Exception raised:
Traceback (most recent call last):
File "/home/marcink/checkout/django/django/test/_doctest.py", line 1249, in __run
compileflags, 1) in test.globs
File "&lt;doctest doctestproject.doctestapp.tests[1]&gt;", line 1, in ?
_('Test marked for translation')
TypeError: 'str' object is not callable
----------------------------------------------------------------------
File "/home/marcink/tmp/doctestproject/doctestapp/tests.py", line 16, in doctestproject.doctestapp.tests
Failed example:
((_ == first_time_gettext) or (_ == gettext))
Expected:
True
Got:
False
----------------------------------------------------------------------
Ran 2 tests in 0.014s
FAILED (failures=1)
</pre><p>
The attached patch solves the problem at the expense of incompatibility with the standard doctest behavior. I imagine some people do write doctests that depend on the _ variable, but the use of gettext's _() is so common in Django apps that a slight rewrite of those doctests will be easier than rewriting internationalized apps.
</p>
en-usDjangohttps://www.djangoproject.com/s/img/site/hdr_logo.gifhttps://code.djangoproject.com/ticket/4530
Trac 1.0.2marcin@…Mon, 11 Jun 2007 12:27:51 GMTattachment sethttps://code.djangoproject.com/ticket/4530
https://code.djangoproject.com/ticket/4530
<ul>
<li><strong>attachment</strong>
set to <em>tests.py</em>
</li>
</ul>
Ticketmarcin@…Mon, 11 Jun 2007 12:29:59 GMTattachment sethttps://code.djangoproject.com/ticket/4530
https://code.djangoproject.com/ticket/4530
<ul>
<li><strong>attachment</strong>
set to <em>fix_doctest_underscore_problem.diff</em>
</li>
</ul>
TicketmtredinnickMon, 11 Jun 2007 12:39:05 GMTstatus changed; needs_better_patch, resolution, needs_tests, needs_docs sethttps://code.djangoproject.com/ticket/4530#comment:1
https://code.djangoproject.com/ticket/4530#comment:1
<ul>
<li><strong>status</strong>
changed from <em>new</em> to <em>closed</em>
</li>
<li><strong>needs_better_patch</strong>
unset
</li>
<li><strong>resolution</strong>
set to <em>duplicate</em>
</li>
<li><strong>needs_tests</strong>
unset
</li>
<li><strong>needs_docs</strong>
unset
</li>
</ul>
<p>
This has come up before and the decision we've made (in an old django-developer's thread) is to remove <tt>_()</tt> from builtins. Particularly with unicode support, people need to be thinking about which translation function they are using as their "default", so forcing explicit importing of gettext() or ugettext() (or one of the <tt>*_lazy()</tt> variants) is reasonable.
</p>
<p>
It also conflicts with using the shell, for exactly the same reason.
</p>
<p>
Most of the work has already been done in the unicode branch. Once that has been merged with trunk, we'll fix up the remaining pieces of <a class="closed ticket" href="https://code.djangoproject.com/ticket/2920" title="defect: Putting _() into builtins causes trouble with doctests (closed: fixed)">#2920</a> and close that. Adding a single import into any files using _() won't take people long when that happens.
</p>
<p>
Closing this as a dupe of <a class="closed ticket" href="https://code.djangoproject.com/ticket/2920" title="defect: Putting _() into builtins causes trouble with doctests (closed: fixed)">#2920</a>.
</p>
Ticket