[Django] #19272: gettext_lazy returns "unexpected type"

28 views
Skip to first unread message

Django

unread,
Nov 10, 2012, 9:25:35 AM11/10/12
to django-...@googlegroups.com
#19272: gettext_lazy returns "unexpected type"
------------------------------+--------------------
Reporter: tyrion | Owner: nobody
Type: Bug | Status: new
Component: Translations | Version: master
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
------------------------------+--------------------
{{{
>>> 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'
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/19272>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Nov 10, 2012, 5:08:42 PM11/10/12
to django-...@googlegroups.com
#19272: gettext_lazy returns "unexpected type"
---------------------------------+------------------------------------

Reporter: tyrion | Owner: nobody
Type: Bug | Status: new
Component: Translations | Version: master
Severity: Release blocker | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
---------------------------------+------------------------------------
Changes (by claudep):

* severity: Normal => Release blocker
* cc: regression (added)
* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0
* stage: Unreviewed => Accepted


Comment:

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.

--
Ticket URL: <https://code.djangoproject.com/ticket/19272#comment:1>

Django

unread,
Nov 10, 2012, 5:12:55 PM11/10/12
to django-...@googlegroups.com
#19272: gettext_lazy returns "unexpected type"
---------------------------------+------------------------------------

Reporter: tyrion | Owner: nobody
Type: Bug | Status: new
Component: Translations | Version: master
Severity: Release blocker | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
---------------------------------+------------------------------------
Changes (by claudep):

* has_patch: 0 => 1


--
Ticket URL: <https://code.djangoproject.com/ticket/19272#comment:2>

Django

unread,
Nov 11, 2012, 6:06:05 AM11/11/12
to django-...@googlegroups.com
#19272: gettext_lazy returns "unexpected type"
-------------------------------------+-------------------------------------

Reporter: tyrion | Owner: nobody
Type: Bug | Status: new
Component: Translations | Version: master
Severity: Release blocker | Resolution:
Keywords: | Triage Stage: Ready for
Has patch: 1 | checkin
Needs tests: 0 | Needs documentation: 0
Easy pickings: 0 | Patch needs improvement: 0
| UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by aaugustin):

* stage: Accepted => Ready for checkin


Comment:

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

--
Ticket URL: <https://code.djangoproject.com/ticket/19272#comment:3>

Django

unread,
Nov 14, 2012, 4:54:42 AM11/14/12
to django-...@googlegroups.com
#19272: gettext_lazy returns "unexpected type"
-------------------------------------+-------------------------------------
Reporter: tyrion | Owner: nobody
Type: Bug | Status: closed
Component: Translations | Version: master
Severity: Release blocker | Resolution: fixed

Keywords: | Triage Stage: Ready for
Has patch: 1 | checkin
Needs tests: 0 | Needs documentation: 0
Easy pickings: 0 | Patch needs improvement: 0
| UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Claude Paroz <claude@…>):

* status: new => closed
* resolution: => fixed


Comment:

In [changeset:"550ddc66b496473c8ee282c7ab6be5885a359d75"]:
{{{
#!CommitTicketReference repository=""
revision="550ddc66b496473c8ee282c7ab6be5885a359d75"
Fixed #19272 -- Fixed gettext_lazy returned type on Python 2

Thanks tyrion for the report.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/19272#comment:4>

Django

unread,
Nov 14, 2012, 4:55:51 AM11/14/12
to django-...@googlegroups.com
#19272: gettext_lazy returns "unexpected type"
-------------------------------------+-------------------------------------
Reporter: tyrion | Owner: nobody
Type: Bug | Status: closed
Component: Translations | Version: master

Severity: Release blocker | Resolution: fixed
Keywords: | Triage Stage: Ready for
Has patch: 1 | checkin
Needs tests: 0 | Needs documentation: 0
Easy pickings: 0 | Patch needs improvement: 0
| UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Claude Paroz <claude@…>):

In [changeset:"ebafba50a418d4a444a067a3d35cea7f98a20158"]:
{{{
#!CommitTicketReference repository=""
revision="ebafba50a418d4a444a067a3d35cea7f98a20158"
[1.5.x] Fixed #19272 -- Fixed gettext_lazy returned type on Python 2

Thanks tyrion for the report.

Backport of 550ddc66b from master.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/19272#comment:5>

Django

unread,
Nov 17, 2012, 10:13:24 AM11/17/12
to django-...@googlegroups.com
#19272: gettext_lazy returns "unexpected type"
-------------------------------------+-------------------------------------
Reporter: tyrion | Owner: nobody
Type: Bug | Status: closed
Component: Translations | Version: master

Severity: Release blocker | Resolution: fixed
Keywords: | Triage Stage: Ready for
Has patch: 1 | checkin
Needs tests: 0 | Needs documentation: 0
Easy pickings: 0 | Patch needs improvement: 0
| UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Preston Holmes <preston@…>):

In [changeset:"5566caeea85069d0560d95071d8a4592a91f6957"]:
{{{
#!CommitTicketReference repository=""
revision="5566caeea85069d0560d95071d8a4592a91f6957"


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

Thanks tyrion for the report.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/19272#comment:6>

Django

unread,
Apr 29, 2014, 6:20:11 AM4/29/14
to django-...@googlegroups.com
#19272: gettext_lazy returns "unexpected type"
-------------------------------------+-------------------------------------

Reporter: tyrion | Owner: nobody
Type: Bug | Status: new
Component: Translations | Version: master
Severity: Release blocker | Resolution:

Keywords: | Triage Stage: Ready for
Has patch: 1 | checkin
Needs tests: 0 | Needs documentation: 0
Easy pickings: 0 | Patch needs improvement: 0
| UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by ygbo):

* status: closed => new
* resolution: fixed =>


Comment:

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'

--
Ticket URL: <https://code.djangoproject.com/ticket/19272#comment:7>

Django

unread,
Apr 29, 2014, 11:10:08 AM4/29/14
to django-...@googlegroups.com
#19272: gettext_lazy returns "unexpected type"
-------------------------------------+-------------------------------------
Reporter: tyrion | Owner: nobody
Type: Bug | Status: closed
Component: Translations | Version: master
Severity: Release blocker | Resolution: fixed

Keywords: | Triage Stage: Ready for
Has patch: 1 | checkin
Needs tests: 0 | Needs documentation: 0
Easy pickings: 0 | Patch needs improvement: 0
| UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by charettes):

* status: new => closed
* resolution: => fixed


Comment:

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.

--
Ticket URL: <https://code.djangoproject.com/ticket/19272#comment:8>

Django

unread,
May 2, 2014, 12:15:39 PM5/2/14
to django-...@googlegroups.com
#19272: gettext_lazy returns "unexpected type"
-------------------------------------+-------------------------------------
Reporter: tyrion | Owner: nobody
Type: Bug | Status: closed
Component: Translations | Version: master

Severity: Release blocker | Resolution: fixed
Keywords: | Triage Stage: Ready for
Has patch: 1 | checkin
Needs tests: 0 | Needs documentation: 0
Easy pickings: 0 | Patch needs improvement: 0
| UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by ygbo):

Ok thanks,
I just logged https://code.djangoproject.com/ticket/22565

--
Ticket URL: <https://code.djangoproject.com/ticket/19272#comment:9>

Reply all
Reply to author
Forward
0 new messages