Opened 3 years ago

Closed 15 months ago

Last modified 15 months ago

#19272 closed Bug (fixed)

gettext_lazy returns "unexpected type"

Reported by: tyrion Owned by: nobody
Component: Translations Version: master
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 claudep 3 years ago.
Preserve bytestrings in gettext[_lazy] calls

Download all attachments as: .zip

Change History (10)

comment:1 Changed 3 years ago by claudep

  • Cc regression added
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Severity changed from Normal to Release blocker
  • Triage Stage changed from Unreviewed to Accepted

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.

Changed 3 years ago by claudep

Preserve bytestrings in gettext[_lazy] calls

comment:2 Changed 3 years ago by claudep

  • Has patch set

comment:3 Changed 3 years ago by aaugustin

  • Triage Stage changed from Accepted to Ready for checkin

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

comment:4 Changed 3 years ago by Claude Paroz <claude@…>

  • Resolution set to fixed
  • Status changed from new to closed

In 550ddc66b496473c8ee282c7ab6be5885a359d75:

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

Thanks tyrion for the report.

comment:5 Changed 3 years ago by Claude Paroz <claude@…>

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 Changed 3 years ago by Preston Holmes <preston@…>

In 5566caeea85069d0560d95071d8a4592a91f6957:

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

Thanks tyrion for the report.

comment:7 Changed 15 months ago by ygbo

  • Resolution fixed deleted
  • Status changed from closed to 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>()
----> 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 15 months ago by charettes (previous) (diff)

comment:8 Changed 15 months ago by charettes

  • Resolution set to fixed
  • Status changed from new to 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.

comment:9 Changed 15 months ago by ygbo

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