Opened 12 years ago

Closed 12 years ago

Last modified 12 years ago

#18772 closed Bug (fixed)

force_unicode needs to go through deprecation policy

Reported by: Adrian Holovaty Owned by: nobody
Component: Python 3 Version: dev
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

Description

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 by Aymeric Augustin, 12 years ago

Version: 1.4master

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 by Adrian Holovaty, 12 years ago

Resolution: invalid
Status: newclosed

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 by Aymeric Augustin, 12 years ago

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

comment:4 by Adrian Holovaty, 12 years ago

Resolution: invalid
Status: closedreopened

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/text.py so that it doesn't break.

comment:5 by Aymeric Augustin, 12 years ago

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 by Adrian Holovaty, 12 years ago

Resolution: fixed
Status: reopenedclosed

Looks like it never lived in django.utils.text. Here's the file from five years ago, when the unicode branch was merged:
https://github.com/django/django/blob/953badbea5a04159adbfa970f5805c0232b6a401/django/utils/text.py

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 by Adrian Holovaty <adrian@…>, 12 years ago

In [fd04e711d2a5440d7a828644ee65b88bf00c83b5]:

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

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