[Django] #28259: Make bool(AnonymousUser) evaluate to false

12 views
Skip to first unread message

Django

unread,
May 31, 2017, 12:20:45 PM5/31/17
to django-...@googlegroups.com
#28259: Make bool(AnonymousUser) evaluate to false
---------------------------------------------+------------------------
Reporter: Linus Lewandowski | Owner: nobody
Type: New feature | Status: new
Component: contrib.auth | Version: master
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
---------------------------------------------+------------------------
I suggest adding !__bool!__() method returning False to the AnonymousUser.

This way it'll be possible to check if the user is authenticated by simply
writing

{{{
if request.user:
}}}

It's a frequent source of bugs (at least for me, but probably I'm not
alone) that right now this code returns True for anonymous users.

--
Ticket URL: <https://code.djangoproject.com/ticket/28259>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
May 31, 2017, 12:40:08 PM5/31/17
to django-...@googlegroups.com
#28259: Make bool(AnonymousUser) evaluate to false
-----------------------------------+--------------------------------------

Reporter: Linus Lewandowski | Owner: nobody
Type: New feature | Status: new
Component: contrib.auth | Version: master
Severity: Normal | Resolution:

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-----------------------------------+--------------------------------------

Comment (by Tim Graham):

The Django test suite passes with the change but I feel like that could
have some backwards compatibility concerns. Also "explicit is better than
implicit"? You could write to the DevelopersMailingList to get other
opinions.

--
Ticket URL: <https://code.djangoproject.com/ticket/28259#comment:1>

Django

unread,
May 31, 2017, 1:45:50 PM5/31/17
to django-...@googlegroups.com
#28259: Make bool(AnonymousUser) evaluate to false
-----------------------------------+--------------------------------------

Reporter: Linus Lewandowski | Owner: nobody
Type: New feature | Status: closed
Component: contrib.auth | Version: master
Severity: Normal | Resolution: wontfix

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-----------------------------------+--------------------------------------
Changes (by Tim Graham):

* status: new => closed
* resolution: => wontfix


Comment:

[https://groups.google.com/d/topic/django-
developers/rGWdttZO06g/discussion django-developers discussion]

Closing for now as Adam's reply on the mailing list confirms my intuition:
"We already have if `request.user.is_authenticated` and
`request.user.is_anonymous` which are both more explicit and pythonic.
Additionally all python classes, and thus instances of User atm, are by
default truthy, so implementing this custom `__bool__` introduces space
for more subtle bugs, e.g. the result of `filter([request.user])` would
change."

--
Ticket URL: <https://code.djangoproject.com/ticket/28259#comment:2>

Django

unread,
Jan 31, 2018, 11:09:40 PM1/31/18
to django-...@googlegroups.com
#28259: Make bool(AnonymousUser) evaluate to false
-----------------------------------+--------------------------------------

Reporter: Linus Lewandowski | Owner: nobody
Type: New feature | Status: closed
Component: contrib.auth | Version: master
Severity: Normal | Resolution: wontfix

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-----------------------------------+--------------------------------------

Comment (by sureshvv):

request.user.is_authenticated and request.user.is_anonymous

too verbose to check a boolean attribute. Totally "unpythonic".

--
Ticket URL: <https://code.djangoproject.com/ticket/28259#comment:3>

Reply all
Reply to author
Forward
0 new messages