[Django] #36899: Implement `Session.__bool__`

3 views
Skip to first unread message

Django

unread,
Feb 2, 2026, 11:08:48 AMFeb 2
to django-...@googlegroups.com
#36899: Implement `Session.__bool__`
-------------------------------------+-------------------------------------
Reporter: Jake Howard | Type:
| Cleanup/optimization
Status: new | Component:
| contrib.sessions
Version: 6.0 | Severity: Normal
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
It's often useful to check whether the user has a session, for example to
avoid creating one unnecessarily. Since `request.session` is always
populated when `SessionMiddleware` is used, it's better to check whether
the session is empty.

#36898 documents `Session.is_empty()` which achieves this, but it could
also be nice if `Session` supported a boolean check directly through
`__bool__`, which would just call `is_empty` internally.

This would mean `if request.session` works as expected. The downside being
`if getattr(request, "session", None)` would be `False` when sessions are
being used, but the session is empty (arguably `hasattr` would be better
there anyway).
--
Ticket URL: <https://code.djangoproject.com/ticket/36899>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Feb 2, 2026, 11:13:29 AMFeb 2
to django-...@googlegroups.com
#36899: Implement `Session.__bool__`
-------------------------------------+-------------------------------------
Reporter: Jake Howard | Owner: (none)
Type: | Status: new
Cleanup/optimization |
Component: contrib.sessions | Version: 6.0
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by jaffar Khan):

* cc: jaffar Khan (added)

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

Django

unread,
Feb 2, 2026, 12:23:33 PMFeb 2
to django-...@googlegroups.com
#36899: Implement `Session.__bool__`
-------------------------------------+-------------------------------------
Reporter: Jake Howard | Owner: (none)
Type: | Status: new
Cleanup/optimization |
Component: contrib.sessions | Version: 6.0
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Ahmed Asar):

Hi, I'd like to work on this ** ticket and submit a patch** if that's
okay.
Please let me know if there’s anything specific I should consider.
Thanks!
--
Ticket URL: <https://code.djangoproject.com/ticket/36899#comment:2>

Django

unread,
Feb 2, 2026, 1:40:42 PMFeb 2
to django-...@googlegroups.com
#36899: Implement `Session.__bool__`
----------------------------------+------------------------------------
Reporter: Jake Howard | Owner: (none)
Type: New feature | Status: new
Component: contrib.sessions | Version: 6.0
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
----------------------------------+------------------------------------
Changes (by Natalia Bidart):

* stage: Unreviewed => Accepted
* type: Cleanup/optimization => New feature

Comment:

Thank you Jake, I think the request makes sense. Personally I would have
suggested a single ticket for this and documenting `is_empty`, but I see
Jacob prefers otherwise so +1 from me.
--
Ticket URL: <https://code.djangoproject.com/ticket/36899#comment:3>

Django

unread,
Feb 2, 2026, 2:01:56 PMFeb 2
to django-...@googlegroups.com
#36899: Implement `Session.__bool__`
----------------------------------+------------------------------------
Reporter: Jake Howard | Owner: (none)
Type: New feature | Status: new
Component: contrib.sessions | Version: 6.0
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
----------------------------------+------------------------------------
Comment (by Jacob Walls):

Jake, any thought about how/where to doc this? Minor incompatible change?
Doubt we want a deprecation path.
--
Ticket URL: <https://code.djangoproject.com/ticket/36899#comment:4>

Django

unread,
Feb 2, 2026, 2:38:46 PMFeb 2
to django-...@googlegroups.com
#36899: Implement `Session.__bool__`
----------------------------------+------------------------------------
Reporter: Jake Howard | Owner: Amar
Type: New feature | Status: assigned
Component: contrib.sessions | Version: 6.0
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
----------------------------------+------------------------------------
Changes (by Amar):

* owner: (none) => Amar
* status: new => assigned

--
Ticket URL: <https://code.djangoproject.com/ticket/36899#comment:5>

Django

unread,
Feb 2, 2026, 6:50:29 PMFeb 2
to django-...@googlegroups.com
#36899: Implement `Session.__bool__`
----------------------------------+------------------------------------
Reporter: Jake Howard | Owner: Amar
Type: New feature | Status: assigned
Component: contrib.sessions | Version: 6.0
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
----------------------------------+------------------------------------
Comment (by Ahmed Asar):

Hi Amar,
I just wanted to mention that I had commented earlier expressing interest
in working on this ticket.
If you’re already planning to take it, no worries at all 👍
But if you’re open to it, I’d be happy to work on the patch and submit a
PR.
Thanks!
--
Ticket URL: <https://code.djangoproject.com/ticket/36899#comment:6>

Django

unread,
Feb 2, 2026, 8:03:35 PMFeb 2
to django-...@googlegroups.com
#36899: Implement `Session.__bool__`
----------------------------------+------------------------------------
Reporter: Jake Howard | Owner: Amar
Type: New feature | Status: assigned
Component: contrib.sessions | Version: 6.0
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
----------------------------------+------------------------------------
Comment (by Amar):

Hi Ahmed,

Thanks a lot for your interest in this ticket, and sorry about the timing
— I didn’t notice your earlier comment before picking it up.

I’ve already worked on a patch and submitted a PR for this ticket here:
https://github.com/django/django/pull/20631
--
Ticket URL: <https://code.djangoproject.com/ticket/36899#comment:7>

Django

unread,
Feb 2, 2026, 8:03:48 PMFeb 2
to django-...@googlegroups.com
#36899: Implement `Session.__bool__`
----------------------------------+------------------------------------
Reporter: Jake Howard | Owner: Amar
Type: New feature | Status: assigned
Component: contrib.sessions | Version: 6.0
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
----------------------------------+------------------------------------
Changes (by Amar):

* has_patch: 0 => 1

--
Ticket URL: <https://code.djangoproject.com/ticket/36899#comment:8>

Django

unread,
Feb 2, 2026, 8:10:05 PMFeb 2
to django-...@googlegroups.com
#36899: Implement `Session.__bool__`
----------------------------------+------------------------------------
Reporter: Jake Howard | Owner: Amar
Type: New feature | Status: assigned
Component: contrib.sessions | Version: 6.0
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
----------------------------------+------------------------------------
Comment (by Ahmed Asar):

Hi Amar,
Greetings to our dear brothers and sisters in Mauritania💗
--
Ticket URL: <https://code.djangoproject.com/ticket/36899#comment:9>

Django

unread,
Feb 2, 2026, 8:21:22 PMFeb 2
to django-...@googlegroups.com
#36899: Implement `Session.__bool__`
----------------------------------+------------------------------------
Reporter: Jake Howard | Owner: Amar
Type: New feature | Status: assigned
Component: contrib.sessions | Version: 6.0
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
----------------------------------+------------------------------------
Comment (by Amar):

Replying to [comment:9 Ahmed Asar]:

Thank you for the kind message, much appreciated.
--
Ticket URL: <https://code.djangoproject.com/ticket/36899#comment:10>

Django

unread,
Feb 17, 2026, 12:59:03 PMFeb 17
to django-...@googlegroups.com
#36899: Implement `Session.__bool__`
-------------------------------------+-------------------------------------
Reporter: Jake Howard | Owner: Amar
Type: New feature | Status: assigned
Component: contrib.sessions | Version: 6.0
Severity: Normal | Resolution:
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Amar):

* stage: Accepted => Ready for checkin

--
Ticket URL: <https://code.djangoproject.com/ticket/36899#comment:11>

Django

unread,
Feb 17, 2026, 1:48:39 PMFeb 17
to django-...@googlegroups.com
#36899: Implement `Session.__bool__`
----------------------------------+------------------------------------
Reporter: Jake Howard | Owner: Amar
Type: New feature | Status: assigned
Component: contrib.sessions | Version: 6.0
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
----------------------------------+------------------------------------
Changes (by Natalia Bidart):

* stage: Ready for checkin => Accepted

Comment:

Amar, you can not set the flag "Ready for Checkin" in your own PR, please
read carefully the contributing docs.
--
Ticket URL: <https://code.djangoproject.com/ticket/36899#comment:12>

Django

unread,
Feb 17, 2026, 2:49:53 PMFeb 17
to django-...@googlegroups.com
#36899: Implement `Session.__bool__`
-------------------------------------+-------------------------------------
Reporter: Jake Howard | Owner: Amar
Type: New feature | Status: assigned
Component: contrib.sessions | Version: 6.0
Severity: Normal | Resolution:
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham):

* stage: Accepted => Ready for checkin

Comment:

The PR has an approval from Jake.
--
Ticket URL: <https://code.djangoproject.com/ticket/36899#comment:13>

Django

unread,
Feb 17, 2026, 3:50:54 PMFeb 17
to django-...@googlegroups.com
#36899: Implement `Session.__bool__`
-------------------------------------+-------------------------------------
Reporter: Jake Howard | Owner: Amar
Type: New feature | Status: assigned
Component: contrib.sessions | Version: 6.0
Severity: Normal | Resolution:
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Amar):

Sorry about that, Natalia. I saw Jake's approval on the PR and figured the
Trac status update had just been missed, so I jumped the gun trying to be
helpful.
I’ll make sure to leave the triage flags to the fellows from now on.
Thanks for the catch, and thanks Tim for the follow-up.
--
Ticket URL: <https://code.djangoproject.com/ticket/36899#comment:14>

Django

unread,
Feb 20, 2026, 5:53:20 PMFeb 20
to django-...@googlegroups.com
#36899: Implement `Session.__bool__`
----------------------------------+------------------------------------
Reporter: Jake Howard | Owner: Amar
Type: New feature | Status: assigned
Component: contrib.sessions | Version: 6.0
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 1 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
----------------------------------+------------------------------------
Changes (by Jacob Walls):

* easy: 1 => 0
* needs_better_patch: 0 => 1
* needs_tests: 0 => 1
* stage: Ready for checkin => Accepted

--
Ticket URL: <https://code.djangoproject.com/ticket/36899#comment:15>

Django

unread,
Feb 21, 2026, 9:47:32 AM (13 days ago) Feb 21
to django-...@googlegroups.com
#36899: Implement `Session.__bool__`
-------------------------------------+-------------------------------------
Reporter: Jake Howard | Owner: Amar
Type: New feature | Status: assigned
Component: contrib.sessions | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Jacob Walls):

* needs_better_patch: 1 => 0
* needs_tests: 1 => 0
* stage: Accepted => Ready for checkin
* version: 6.0 => dev

--
Ticket URL: <https://code.djangoproject.com/ticket/36899#comment:16>

Django

unread,
Feb 21, 2026, 9:58:00 AM (13 days ago) Feb 21
to django-...@googlegroups.com
#36899: Implement `Session.__bool__`
-------------------------------------+-------------------------------------
Reporter: Jake Howard | Owner: Amar
Type: New feature | Status: closed
Component: contrib.sessions | Version: dev
Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Jacob Walls <jacobtylerwalls@…>):

* resolution: => fixed
* status: assigned => closed

Comment:

In [changeset:"158fd81ef5a5647d27eb3065063284f9ee0a3ca4" 158fd81]:
{{{#!CommitTicketReference repository=""
revision="158fd81ef5a5647d27eb3065063284f9ee0a3ca4"
Fixed #36899 -- Implemented SessionBase.__bool__.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/36899#comment:17>
Reply all
Reply to author
Forward
0 new messages