Opened 17 years ago
Closed 17 years ago
#4530 closed (duplicate)
doctests don't play well with i18n: they override the value of _ builtin
Reported by: | Owned by: | Adrian Holovaty | |
---|---|---|---|
Component: | Testing framework | Version: | dev |
Severity: | Keywords: | doctests, gettext, translation | |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
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 tests.py file results in an exception:
====================================================================== 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 "<doctest doctestproject.doctestapp.tests[1]>", 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)
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)
Change History (3)
by , 17 years ago
by , 17 years ago
Attachment: | fix_doctest_underscore_problem.diff added |
---|
comment:1 by , 17 years ago
Resolution: | → duplicate |
---|---|
Status: | new → 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.