From eba093e8b02989af1857b1915907ca0897f565ff Mon Sep 17 00:00:00 2001 From: Tim Graham Date: Sat, 31 Dec 2016 11:46:40 -0500 Subject: [PATCH] Refs #25847 -- Removed support for User.is_(anonymous|authenticated) as methods. Per deprecation timeline. --- django/contrib/auth/base_user.py | 5 ++-- django/contrib/auth/models.py | 5 ++-- django/utils/deprecation.py | 43 --------------------------- docs/ref/contrib/auth.txt | 10 ------- docs/releases/2.0.txt | 3 ++ docs/topics/auth/customizing.txt | 10 ------- tests/auth_tests/test_basic.py | 42 -------------------------- tests/utils_tests/test_deprecation.py | 31 ------------------- 8 files changed, 7 insertions(+), 142 deletions(-) delete mode 100644 tests/utils_tests/test_deprecation.py diff --git a/django/contrib/auth/base_user.py b/django/contrib/auth/base_user.py index 9ad5cde87f..104748ff69 100644 --- a/django/contrib/auth/base_user.py +++ b/django/contrib/auth/base_user.py @@ -12,7 +12,6 @@ from django.contrib.auth.hashers import ( ) from django.db import models from django.utils.crypto import get_random_string, salted_hmac -from django.utils.deprecation import CallableFalse, CallableTrue from django.utils.encoding import force_text, python_2_unicode_compatible from django.utils.translation import ugettext_lazy as _ @@ -91,7 +90,7 @@ class AbstractBaseUser(models.Model): Always return False. This is a way of comparing User objects to anonymous users. """ - return CallableFalse + return False @property def is_authenticated(self): @@ -99,7 +98,7 @@ class AbstractBaseUser(models.Model): Always return True. This is a way to tell if the user has been authenticated in templates. """ - return CallableTrue + return True def set_password(self, raw_password): self.password = make_password(raw_password) diff --git a/django/contrib/auth/models.py b/django/contrib/auth/models.py index 6055526941..d91412eb5c 100644 --- a/django/contrib/auth/models.py +++ b/django/contrib/auth/models.py @@ -9,7 +9,6 @@ from django.core.mail import send_mail from django.db import models from django.db.models.manager import EmptyManager from django.utils import six, timezone -from django.utils.deprecation import CallableFalse, CallableTrue from django.utils.encoding import python_2_unicode_compatible from django.utils.translation import ugettext_lazy as _ @@ -443,11 +442,11 @@ class AnonymousUser(object): @property def is_anonymous(self): - return CallableTrue + return True @property def is_authenticated(self): - return CallableFalse + return False def get_username(self): return self.username diff --git a/django/utils/deprecation.py b/django/utils/deprecation.py index 2963e60214..b862a161b2 100644 --- a/django/utils/deprecation.py +++ b/django/utils/deprecation.py @@ -84,49 +84,6 @@ class DeprecationInstanceCheck(type): return super(DeprecationInstanceCheck, self).__instancecheck__(instance) -class CallableBool: - """ - An boolean-like object that is also callable for backwards compatibility. - """ - do_not_call_in_templates = True - - def __init__(self, value): - self.value = value - - def __bool__(self): - return self.value - - def __call__(self): - warnings.warn( - "Using user.is_authenticated() and user.is_anonymous() as a method " - "is deprecated. Remove the parentheses to use it as an attribute.", - RemovedInDjango20Warning, stacklevel=2 - ) - return self.value - - def __nonzero__(self): # Python 2 compatibility - return self.value - - def __repr__(self): - return 'CallableBool(%r)' % self.value - - def __eq__(self, other): - return self.value == other - - def __ne__(self, other): - return self.value != other - - def __or__(self, other): - return bool(self.value or other) - - def __hash__(self): - return hash(self.value) - - -CallableFalse = CallableBool(False) -CallableTrue = CallableBool(True) - - class MiddlewareMixin(object): def __init__(self, get_response=None): self.get_response = get_response diff --git a/docs/ref/contrib/auth.txt b/docs/ref/contrib/auth.txt index 6395949e3e..87dd580051 100644 --- a/docs/ref/contrib/auth.txt +++ b/docs/ref/contrib/auth.txt @@ -137,11 +137,6 @@ Attributes (representing the currently logged-in user), you should know this attribute is ``True`` for any :class:`~models.User` instance. - .. versionchanged:: 1.10 - - In older versions, this was a method. Backwards-compatibility - support for using it as a method will be removed in Django 2.0. - .. attribute:: is_anonymous Read-only attribute which is always ``False``. This is a way of @@ -150,11 +145,6 @@ Attributes :attr:`~django.contrib.auth.models.User.is_authenticated` to this attribute. - .. versionchanged:: 1.10 - - In older versions, this was a method. Backwards-compatibility - support for using it as a method will be removed in Django 2.0. - .. attribute:: username_validator .. versionadded:: 1.10 diff --git a/docs/releases/2.0.txt b/docs/releases/2.0.txt index 5751972308..f02520cb9e 100644 --- a/docs/releases/2.0.txt +++ b/docs/releases/2.0.txt @@ -358,3 +358,6 @@ these features. * The shim for supporting custom related manager classes without a ``_apply_rel_filters()`` method is removed. + +* Using ``User.is_authenticated()`` and ``User.is_anonymous()`` as methods + rather than properties is no longer be supported. diff --git a/docs/topics/auth/customizing.txt b/docs/topics/auth/customizing.txt index ff84fad19f..40675d2ef6 100644 --- a/docs/topics/auth/customizing.txt +++ b/docs/topics/auth/customizing.txt @@ -695,11 +695,6 @@ The following attributes and methods are available on any subclass of (representing the currently logged-in user), you should know this attribute is ``True`` for any :class:`~models.User` instance. - .. versionchanged:: 1.10 - - In older versions, this was a method. Backwards-compatibility - support for using it as a method will be removed in Django 2.0. - .. attribute:: models.AbstractBaseUser.is_anonymous Read-only attribute which is always ``False``. This is a way of @@ -707,11 +702,6 @@ The following attributes and methods are available on any subclass of objects. Generally, you should prefer using :attr:`~models.User.is_authenticated` to this attribute. - .. versionchanged:: 1.10 - - In older versions, this was a method. Backwards-compatibility - support for using it as a method will be removed in Django 2.0. - .. method:: models.AbstractBaseUser.set_password(raw_password) Sets the user's password to the given raw string, taking care of the diff --git a/tests/auth_tests/test_basic.py b/tests/auth_tests/test_basic.py index 2e18260e4c..4555e28270 100644 --- a/tests/auth_tests/test_basic.py +++ b/tests/auth_tests/test_basic.py @@ -1,8 +1,6 @@ # -*- coding: utf-8 -*- from __future__ import unicode_literals -import warnings - from django.contrib.auth import get_user, get_user_model from django.contrib.auth.models import AnonymousUser, User from django.core.exceptions import ImproperlyConfigured @@ -56,26 +54,6 @@ class BasicTestCase(TestCase): with self.assertRaises(IntegrityError): User.objects.create_user(omega_username) - def test_is_anonymous_authenticated_method_deprecation(self): - deprecation_message = ( - 'Using user.is_authenticated() and user.is_anonymous() as a ' - 'method is deprecated. Remove the parentheses to use it as an ' - 'attribute.' - ) - u = User.objects.create_user('testuser', 'test@example.com', 'testpw') - # Backwards-compatibility callables - with warnings.catch_warnings(record=True) as warns: - warnings.simplefilter('always') - self.assertFalse(u.is_anonymous()) - self.assertEqual(len(warns), 1) - self.assertEqual(str(warns[0].message), deprecation_message) - - with warnings.catch_warnings(record=True) as warns: - warnings.simplefilter('always') - self.assertTrue(u.is_authenticated()) - self.assertEqual(len(warns), 1) - self.assertEqual(str(warns[0].message), deprecation_message) - def test_user_no_email(self): "Users can be created without an email" u = User.objects.create_user('testuser1') @@ -101,26 +79,6 @@ class BasicTestCase(TestCase): self.assertEqual(a.groups.all().count(), 0) self.assertEqual(a.user_permissions.all().count(), 0) - def test_anonymous_user_is_anonymous_authenticated_method_deprecation(self): - a = AnonymousUser() - deprecation_message = ( - 'Using user.is_authenticated() and user.is_anonymous() as a ' - 'method is deprecated. Remove the parentheses to use it as an ' - 'attribute.' - ) - # Backwards-compatibility callables - with warnings.catch_warnings(record=True) as warns: - warnings.simplefilter('always') # prevent warnings from appearing as errors - self.assertTrue(a.is_anonymous()) - self.assertEqual(len(warns), 1) - self.assertEqual(str(warns[0].message), deprecation_message) - - with warnings.catch_warnings(record=True) as warns: - warnings.simplefilter('always') # prevent warnings from appearing as errors - self.assertFalse(a.is_authenticated()) - self.assertEqual(len(warns), 1) - self.assertEqual(str(warns[0].message), deprecation_message) - def test_superuser(self): "Check the creation and properties of a superuser" super = User.objects.create_superuser('super', 'super@example.com', 'super') diff --git a/tests/utils_tests/test_deprecation.py b/tests/utils_tests/test_deprecation.py deleted file mode 100644 index 56ba259a2f..0000000000 --- a/tests/utils_tests/test_deprecation.py +++ /dev/null @@ -1,31 +0,0 @@ -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) - - def test_or(self): - self.assertIs(CallableTrue | CallableTrue, True) - self.assertIs(CallableTrue | CallableFalse, True) - self.assertIs(CallableFalse | CallableTrue, True) - self.assertIs(CallableFalse | CallableFalse, False) - - self.assertIs(CallableTrue | True, True) - self.assertIs(CallableTrue | False, True) - self.assertIs(CallableFalse | True, True) - self.assertFalse(CallableFalse | False, False) - - def test_set_membership(self): - self.assertIs(CallableTrue in {True}, True) - self.assertIs(CallableFalse not in {True}, True)