Opened 3 years ago

Closed 2 years ago

#20231 closed Bug (fixed)

django.utils.text.smart_strip does not work with lazy strings (TypeError)

Reported by: bmispelon Owned by: nobody
Component: Utilities Version: master
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


>>> from django.utils.text import smart_split
>>> from django.utils.translation import ugettext_lazy as _
>>> smart_split(_('a b c d'))
<django.utils.functional.lazy.<locals>.__proxy__ object at 0x7f53e20b0e10>
>>> list(smart_split(_('a b c d')))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "./django/utils/", line 128, in __wrapper__
    raise TypeError("Lazy object returned unexpected type.")
TypeError: Lazy object returned unexpected type.

This is caused by this line:

I don't think the allow_lazy decorator can be applied to a generator function so I think the line should be removed altogether.

If we remove the decorator, then lazy strings are evaluated when the generator is called.
This is not exactly backwards-incompatible since this function never actually worked with lazy strings in the first place.

Change History (3)

comment:1 Changed 3 years ago by claudep

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted

+1 to remove allow_lazy here. As soon as you begin to manipulate strings with splitting and such, the lazyness of the string has to be dropped anyway, in my opinion. The fact that the issue didn't emerge until now is also a good sign that the functionality is not so crucial.

comment:2 Changed 2 years ago by bmispelon

  • Has patch set

I've prepared a pull request for this ticket that include fixes for 3 other ones:

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

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

In e7b9c11c3f87e235e643c340d38325fbd4257047:

Fixed #20231 -- Don't use allow_lazy on smart_split

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