#19272 closed Bug (fixed)
gettext_lazy returns "unexpected type"
Reported by: | Germano Gabbianelli | Owned by: | nobody |
---|---|---|---|
Component: | Translations | Version: | dev |
Severity: | Release blocker | Keywords: | |
Cc: | regression | Triage Stage: | Ready for checkin |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
>>> from django.utils.translation import gettext_lazy >>> gettext_lazy("test").upper() Traceback (most recent call last): File "<console>", line 1, in <module> File "/home/tyrion/.virtualenvs/djangobug/src/django/django/utils/functional.py", line 121, in __wrapper__ raise TypeError("Lazy object returned unexpected type.") TypeError: Lazy object returned unexpected type. >>>
This is because gettext
returns an unicode string, but gettext_lazy
is defined as:
gettext_lazy = lazy(gettext, str)
>>> from django.utils.translation import gettext >>> from django.utils.functional import lazy >>> gettext("test") u'test' >>> gettext_lazy = lazy(gettext, unicode) >>> gettext_lazy("test") <django.utils.functional.__proxy__ object at 0x975cfec> >>> _.upper() u'TEST'
Attachments (1)
Change History (10)
comment:1 by , 12 years ago
Cc: | added |
---|---|
Severity: | Normal → Release blocker |
Triage Stage: | Unreviewed → Accepted |
comment:2 by , 12 years ago
Has patch: | set |
---|
comment:3 by , 12 years ago
Triage Stage: | Accepted → Ready for checkin |
---|
The patch looks good to me. (I didn't apply it or run the tests.)
comment:4 by , 12 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
comment:7 by , 11 years ago
Resolution: | fixed |
---|---|
Status: | closed → new |
The issue still occurs with pgettext_lazy:
In [1]: from django.utils.translation import pgettext_lazy
In [2]: s = pgettext_lazy("a context", "foo bar") # using bytestring
In [3]: s.upper()
/usr/lib/python2.7/dist-packages/django/db/backends/sqlite3/base.py:53: RuntimeWarning: SQLite received a naive datetime (2014-04-29 12:18:28.940685) while time zone support is active.
RuntimeWarning)
TypeError Traceback (most recent call last)
/usr/lib/python2.7/dist-packages/django/core/management/commands/shell.pyc in <module>()
/usr/lib/python2.7/dist-packages/django/utils/functional.pyc in wrapper(self, *args, kw)
121 if t in self.dispatch:
122 return self.dispatch[t][funcname](res, *args, kw)
--> 123 raise TypeError("Lazy object returned unexpected type.")
124
125 if klass not in cls.dispatch:
TypeError: Lazy object returned unexpected type.
In [4]: s = pgettext_lazy("a context", u"foo bar") # using unicode string
comment:8 by , 11 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
Please don't re-open a ticket that has been closed and fixed (the reported issue here is about gettext_lazy
).
Open a new ticket that refers to this one for the pgettext_lazy
issue by making sure to use formatting (wrap your code between {{{
and }}}
).
You can go ahead and mark the ticket as an accepted bug from the beginning since I managed to reproduce with you provided test case.
I'm not a fan of non-unicode gettext calls, so my suggestion would be to replace it by
ugettext_lazy
. However this is a regression and gettext_lazy should not transform the message to unicode on Python 2.