Opened 3 years ago

Closed 3 years ago

Last modified 3 years ago

#18772 closed Bug (fixed)

force_unicode needs to go through deprecation policy

Reported by: adrian Owned by: nobody
Component: Python 3 Version: master
Severity: Release blocker Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


Looks like django.utils.text.force_unicode was removed recently, without providing a backwards-compatible shim. The method had been documented (and I use it at work) -- so imagine my surprise when updating my Django trunk code resulted in a hard breaking of my site.

Apparently django.utils.encoding.force_text is the new thing going forward. I haven't been paying attention to the developments here, so I'm not comfortable writing a patch for something so fundamental to the framework... :-/

Change History (7)

comment:1 Changed 3 years ago by aaugustin

  • Version changed from 1.4 to master

force_unicode is still there on Python 2 and there isn't any plan to deprecate it:

>>> from django.utils.encoding import force_unicode
>>> force_unicode('bla bla')
u'bla bla'

However, by design, it doesn't exist on Python 3 because unicode doesn't exist at all on Python 3.

You seem to assume a 100% backwards-compatibility guarantee when moving from Python 2 to Python 3. This isn't possible, because some Python 2 concepts or constructs don't exist in Python 3.

In addition to the Python porting — for instance renaming __unicode__ to __str__ everywhere, there will be some minor changes to Django libraries — for instance, renaming smart_unicode to smart_str.

While we're making good progress on the code, the documentation remains to be written. We'll have to explain such requirements in the documentation before announcing that Python 3 is officially supported.

comment:2 Changed 3 years ago by adrian

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

Sorry I was unclear -- I'm on Python 2 (not Python 3), and the following results in an ImportError on current master:

from django.utils.text import force_unicode

I can confirm django.utils.encoding.force_unicode exists, but for some reason my legacy code is using django.utils.text, not django.utils.encoding. Looks like I was importing it from an incorrect place here, and it was working only by chance (django.utils.text imported force_unicode itself, putting it in the module-level namespace).

So it seems like this is NOT a problem for people who have imported force_unicode from the correct place. :-) I'll close it -- sorry for the noise!

comment:3 Changed 3 years ago by aaugustin

Oh, sorry, I missed the django.utils.text.force_unicode in your original report.

comment:4 Changed 3 years ago by adrian

  • Resolution invalid deleted
  • Status changed from closed to reopened

You know, I'm looking through some third-party libraries, and a fair number of them have from django.utils.text instead of from django.utils.encoding when importing force_unicode. It might be worth adding a from django.utils.encoding import force_unicode to the top of django/utils/ so that it doesn't break.

comment:5 Changed 3 years ago by aaugustin

That makes sense. If the problem bit you it may just bite anyone.

Do you know if force_unicode used to live in django.utils.text?

comment:6 Changed 3 years ago by adrian

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

Looks like it never lived in django.utils.text. Here's the file from five years ago, when the unicode branch was merged:

So it's purely my own stupidity for importing it from there. Still, I've seen some other bits of code rely on that, so I just added the import (along with a comment) in django.utils.text.

comment:7 Changed 3 years ago by Adrian Holovaty <adrian@…>

In [fd04e711d2a5440d7a828644ee65b88bf00c83b5]:

Added import of force_unicode to utils/
For backwards compatibility with stupid people like me. Refs #18772.

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