Opened 8 years ago

Closed 8 years ago

#4530 closed (duplicate)

doctests don't play well with i18n: they override the value of _ builtin

Reported by: marcin@… Owned by: adrian
Component: Testing framework Version: master
Severity: Keywords: doctests, gettext, translation
Cc: Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:


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 _().

Running the doctests from the attached file results in an exception:

FAIL: Doctest: doctestproject.doctestapp.tests
Traceback (most recent call last):
  File "/home/marcink/checkout/django/django/test/", 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/", line 0, in tests

File "/home/marcink/tmp/doctestproject/doctestapp/", 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/", line 1249, in __run
        compileflags, 1) in test.globs
      File "<doctest doctestproject.doctestapp.tests[1]>", line 1, in ?
        _('Test marked for translation')
    TypeError: 'str' object is not callable
File "/home/marcink/tmp/doctestproject/doctestapp/", line 16, in doctestproject.doctestapp.tests
Failed example:
    ((_ == first_time_gettext) or (_ == gettext))

Ran 2 tests in 0.014s

FAILED (failures=1)

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.

Attachments (2) (469 bytes) - added by marcin@… 8 years ago.
fix_doctest_underscore_problem.diff (1.3 KB) - added by marcin@… 8 years ago.

Download all attachments as: .zip

Change History (3)

Changed 8 years ago by marcin@…

Changed 8 years ago by marcin@…

comment:1 Changed 8 years ago by mtredinnick

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to duplicate
  • Status changed from new to closed

This has come up before and the decision we've made (in an old django-developer's thread) is to remove _() 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 *_lazy() variants) is reasonable.

It also conflicts with using the shell, for exactly the same reason.

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 #2920 and close that. Adding a single import into any files using _() won't take people long when that happens.

Closing this as a dupe of #2920.

Note: See TracTickets for help on using tickets.
Back to Top