Opened 12 years ago

Closed 11 years ago

Last modified 11 years ago

#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)

19272-1.diff (1.3 KB ) - added by Claude Paroz 12 years ago.
Preserve bytestrings in gettext[_lazy] calls

Download all attachments as: .zip

Change History (10)

comment:1 by Claude Paroz, 12 years ago

Cc: regression added
Severity: NormalRelease blocker
Triage Stage: UnreviewedAccepted

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.

by Claude Paroz, 12 years ago

Attachment: 19272-1.diff added

Preserve bytestrings in gettext[_lazy] calls

comment:2 by Claude Paroz, 12 years ago

Has patch: set

comment:3 by Aymeric Augustin, 12 years ago

Triage Stage: AcceptedReady for checkin

The patch looks good to me. (I didn't apply it or run the tests.)

comment:4 by Claude Paroz <claude@…>, 12 years ago

Resolution: fixed
Status: newclosed

In 550ddc66b496473c8ee282c7ab6be5885a359d75:

Fixed #19272 -- Fixed gettext_lazy returned type on Python 2

Thanks tyrion for the report.

comment:5 by Claude Paroz <claude@…>, 12 years ago

In ebafba50a418d4a444a067a3d35cea7f98a20158:

[1.5.x] Fixed #19272 -- Fixed gettext_lazy returned type on Python 2

Thanks tyrion for the report.
Backport of 550ddc66b from master.

comment:6 by Preston Holmes <preston@…>, 12 years ago

In 5566caeea85069d0560d95071d8a4592a91f6957:

Fixed #19272 -- Fixed gettext_lazy returned type on Python 2

Thanks tyrion for the report.

comment:7 by ygbo, 11 years ago

Resolution: fixed
Status: closednew

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>()
----> 1 s.upper()

/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

In [5]: s.upper()
Out[5]: u'FOO BAR'
Last edited 11 years ago by Simon Charette (previous) (diff)

comment:8 by Simon Charette, 11 years ago

Resolution: fixed
Status: newclosed

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.

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