summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTim Graham <timograham@gmail.com>2019-02-05 09:19:49 -0500
committerTim Graham <timograham@gmail.com>2019-02-06 14:12:06 -0500
commitd55e88292723764a16f0689c73bc7e739dfa6047 (patch)
treeb089db0867f0439b7998cf03c7cd0de28f4362f9
parent3bb6a4390c0a57da991fcb1c0642b9b3fccff751 (diff)
Refs #27753 -- Deprecated django.utils.encoding.force_text() and smart_text().
-rw-r--r--django/utils/encoding.py18
-rw-r--r--docs/internals/deprecation.txt2
-rw-r--r--docs/ref/utils.txt4
-rw-r--r--docs/releases/3.0.txt8
-rw-r--r--tests/utils_tests/test_encoding_deprecations.py24
5 files changed, 54 insertions, 2 deletions
diff --git a/django/utils/encoding.py b/django/utils/encoding.py
index 42adb45b8e..94b63762db 100644
--- a/django/utils/encoding.py
+++ b/django/utils/encoding.py
@@ -1,9 +1,11 @@
import codecs
import datetime
import locale
+import warnings
from decimal import Decimal
from urllib.parse import quote
+from django.utils.deprecation import RemovedInDjango40Warning
from django.utils.functional import Promise
@@ -97,8 +99,20 @@ def force_bytes(s, encoding='utf-8', strings_only=False, errors='strict'):
return str(s).encode(encoding, errors)
-smart_text = smart_str
-force_text = force_str
+def smart_text(s, encoding='utf-8', strings_only=False, errors='strict'):
+ warnings.warn(
+ 'smart_text() is deprecated in favor of smart_str().',
+ RemovedInDjango40Warning, stacklevel=2,
+ )
+ return smart_str(s, encoding, strings_only, errors)
+
+
+def force_text(s, encoding='utf-8', strings_only=False, errors='strict'):
+ warnings.warn(
+ 'force_text() is deprecated in favor of force_str().',
+ RemovedInDjango40Warning, stacklevel=2,
+ )
+ return force_str(s, encoding, strings_only, errors)
def iri_to_uri(iri):
diff --git a/docs/internals/deprecation.txt b/docs/internals/deprecation.txt
index 8172e2c8a7..4f9296bce8 100644
--- a/docs/internals/deprecation.txt
+++ b/docs/internals/deprecation.txt
@@ -18,6 +18,8 @@ details on these changes.
* ``django.utils.http.urlquote()``, ``urlquote_plus()``, ``urlunquote()``, and
``urlunquote_plus()`` will be removed.
+* ``django.utils.encoding.force_text()`` and ``smart_text()`` will be removed.
+
.. _deprecation-removed-in-3.1:
3.1
diff --git a/docs/ref/utils.txt b/docs/ref/utils.txt
index 62625cf62c..9621db525b 100644
--- a/docs/ref/utils.txt
+++ b/docs/ref/utils.txt
@@ -232,11 +232,15 @@ The functions defined in this module share the following properties:
.. function:: smart_text(s, encoding='utf-8', strings_only=False, errors='strict')
+ .. deprecated:: 3.0
+
Alias of :func:`force_str` for backwards compatibility, especially in code
that supports Python 2.
.. function:: force_text(s, encoding='utf-8', strings_only=False, errors='strict')
+ .. deprecated:: 3.0
+
Alias of :func:`force_str` for backwards compatibility, especially in code
that supports Python 2.
diff --git a/docs/releases/3.0.txt b/docs/releases/3.0.txt
index eb02f19121..6c5eb0ccb5 100644
--- a/docs/releases/3.0.txt
+++ b/docs/releases/3.0.txt
@@ -292,6 +292,14 @@ Miscellaneous
Features deprecated in 3.0
==========================
+``django.utils.encoding.force_text()`` and ``smart_text()``
+-----------------------------------------------------------
+
+The ``smart_text()`` and ``force_text()`` aliases (since Django 2.0) of
+``smart_str()`` and ``force_str()`` are deprecated. Ignore this deprecation if
+your code supports Python 2 as the behavior of ``smart_str()`` and
+``force_str()`` is different there.
+
Miscellaneous
-------------
diff --git a/tests/utils_tests/test_encoding_deprecations.py b/tests/utils_tests/test_encoding_deprecations.py
new file mode 100644
index 0000000000..c775ce5f66
--- /dev/null
+++ b/tests/utils_tests/test_encoding_deprecations.py
@@ -0,0 +1,24 @@
+from django.test import SimpleTestCase, ignore_warnings
+from django.utils.deprecation import RemovedInDjango40Warning
+from django.utils.encoding import force_text, smart_text
+from django.utils.functional import SimpleLazyObject
+from django.utils.translation import gettext_lazy
+
+
+@ignore_warnings(category=RemovedInDjango40Warning)
+class TestDeprecatedEncodingUtils(SimpleTestCase):
+
+ def test_force_text(self):
+ s = SimpleLazyObject(lambda: 'x')
+ self.assertIs(type(force_text(s)), str)
+
+ def test_smart_text(self):
+ class Test:
+ def __str__(self):
+ return 'ŠĐĆŽćžšđ'
+
+ lazy_func = gettext_lazy('x')
+ self.assertIs(smart_text(lazy_func), lazy_func)
+ self.assertEqual(smart_text(Test()), '\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111')
+ self.assertEqual(smart_text(1), '1')
+ self.assertEqual(smart_text('foo'), 'foo')