The
[https://docs.djangoproject.com/en/1.11/ref/contrib/auth/#django.contrib.auth.models.User.is_authenticated
documentation] for this attribute is misleading. It implies that that
`is_authenticated` **is** `True` for authenticated users, while in reality
the attribute has to be called or checked for equality (`==`) :
Read-only attribute which is always True (as opposed to
AnonymousUser.is_authenticated which is always False).
I suggest to add a notice/warning to the documentation, that use of *is* /
identity checking is not yet supported. There's a similar warning in the
deprecation notice for `User.is_authenticated()`
[https://docs.djangoproject.com/en/1.11/releases/1.10/#user-is-auth-anon-
deprecation here], but neither the warning nor the deprecation notice can
be found in the documentation for
`contrib.auth.models.User.is_authenticated`
--
Ticket URL: <https://code.djangoproject.com/ticket/28420>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* component: contrib.auth => Documentation
--
Ticket URL: <https://code.djangoproject.com/ticket/28420#comment:1>
* stage: Unreviewed => Accepted
* type: Uncategorized => Cleanup/optimization
Comment:
It's more Python to write `if request.user.is_authenticated:` or `if not
request.user.is_authenticated:` rather than `== True` but a note wouldn't
hurt.
--
Ticket URL: <https://code.djangoproject.com/ticket/28420#comment:2>
Comment (by Tobi):
Replying to [comment:2 Tim Graham]:
> It's more Python to write `if request.user.is_authenticated:` or `if not
request.user.is_authenticated:` rather than `== True` but a note wouldn't
hurt.
True, `== True:` is less Python, but this one does actually work. The
problem is that `if request.user.is_authenticated is True:` and `if
request.user.is_authenticated:` yield different results:
{{{
>>> AnonymousUser().is_authenticated is True
False
>>> AnonymousUser().is_authenticated is False
False
>>> if AnonymousUser().is_authenticated:
... print('Authed')
...
>>>
}}}
So different to the documentation `is_authenticated` is not an attribute
which *is* always `True` (or `False` for `AnonymousUser`), but its return
type has to be called when used with `is`, which would be the pythonic way
to check here (as `if not is_authenticated` would also catch `None`, `0`,
`''` `[]` etc).
--
Ticket URL: <https://code.djangoproject.com/ticket/28420#comment:3>
* owner: nobody => Tobi
* status: new => assigned
--
Ticket URL: <https://code.djangoproject.com/ticket/28420#comment:4>
* has_patch: 0 => 1
Comment:
[https://github.com/django/django/pull/8799 PR]
--
Ticket URL: <https://code.djangoproject.com/ticket/28420#comment:5>
* status: assigned => closed
* resolution: => fixed
Comment:
In [changeset:"aef117eb2e7805c5965adbfbfe1b5374cb58bbbe" aef117e]:
{{{
#!CommitTicketReference repository=""
revision="aef117eb2e7805c5965adbfbfe1b5374cb58bbbe"
[1.11.x] Fixed #28420 -- Doc'd 'is' comparison restriction for
User.is_authenticated/anonymous.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/28420#comment:6>