summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTim Graham <timograham@gmail.com>2016-08-02 09:32:00 -0400
committerTim Graham <timograham@gmail.com>2016-08-02 11:01:08 -0400
commit54afa960d1ee8c63635225a0f0a2489971b5aab5 (patch)
tree89cdc33c2ffd2bb7ce034004fc8bb2d9a3a67c51
parentd95c669c29a0403cd1098664f0ef863fae8b7c98 (diff)
Fixed #26988 -- Improved/clarified User.is_authenticated/anonymous compatibility.
Thanks marktranchant for the report and review.
-rw-r--r--django/utils/deprecation.py6
-rw-r--r--docs/releases/1.10.1.txt3
-rw-r--r--docs/releases/1.10.txt8
-rw-r--r--tests/utils_tests/test_deprecation.py16
4 files changed, 33 insertions, 0 deletions
diff --git a/django/utils/deprecation.py b/django/utils/deprecation.py
index 5bff40f474..020f69b032 100644
--- a/django/utils/deprecation.py
+++ b/django/utils/deprecation.py
@@ -110,6 +110,12 @@ class CallableBool:
def __repr__(self):
return 'CallableBool(%r)' % self.value
+ def __eq__(self, other):
+ return self.value == other
+
+ def __ne__(self, other):
+ return self.value != other
+
CallableFalse = CallableBool(False)
CallableTrue = CallableBool(True)
diff --git a/docs/releases/1.10.1.txt b/docs/releases/1.10.1.txt
index 2f4fd581b9..86cd087bdf 100644
--- a/docs/releases/1.10.1.txt
+++ b/docs/releases/1.10.1.txt
@@ -11,3 +11,6 @@ Bugfixes
* Fixed a crash in MySQL connections where ``SELECT @@SQL_AUTO_IS_NULL``
doesn't return a result (:ticket:`26991`).
+
+* Allowed ``User.is_authenticated`` and ``User.is_anonymous`` properties to be
+ compared using ``==`` and ``!=`` (:ticket:`26988`).
diff --git a/docs/releases/1.10.txt b/docs/releases/1.10.txt
index 1f6ebc3a7e..88209cb1cf 100644
--- a/docs/releases/1.10.txt
+++ b/docs/releases/1.10.txt
@@ -1061,6 +1061,14 @@ method, e.g.::
If you override these methods in a custom user model, you must change them to
properties or attributes.
+Django uses a ``CallableBool`` object to allow these attributes to work as both
+a property and a method. Thus, until the deprecation period ends, you cannot
+compare these properties using the ``is`` operator. That is, the following
+won't work::
+
+ if request.user.is_authenticated is True:
+ ...
+
Custom manager classes available through ``prefetch_related`` must define a ``_apply_rel_filters()`` method
-----------------------------------------------------------------------------------------------------------
diff --git a/tests/utils_tests/test_deprecation.py b/tests/utils_tests/test_deprecation.py
new file mode 100644
index 0000000000..41627ce1b3
--- /dev/null
+++ b/tests/utils_tests/test_deprecation.py
@@ -0,0 +1,16 @@
+from django.test import SimpleTestCase
+from django.utils.deprecation import CallableFalse, CallableTrue
+
+
+class TestCallableBool(SimpleTestCase):
+ def test_true(self):
+ self.assertTrue(CallableTrue)
+ self.assertEqual(CallableTrue, True)
+ self.assertFalse(CallableTrue != True) # noqa: E712
+ self.assertNotEqual(CallableTrue, False)
+
+ def test_false(self):
+ self.assertFalse(CallableFalse)
+ self.assertEqual(CallableFalse, False)
+ self.assertFalse(CallableFalse != False) # noqa: E712
+ self.assertNotEqual(CallableFalse, True)