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

0 views
Skip to first unread message

Django

unread,
Feb 2, 2026, 11:08:48 AM (3 days ago) Feb 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 AM (3 days ago) Feb 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 PM (3 days ago) Feb 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 PM (2 days ago) Feb 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 PM (2 days ago) Feb 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 PM (2 days ago) Feb 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 PM (2 days ago) Feb 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 PM (2 days ago) Feb 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 PM (2 days ago) Feb 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 PM (2 days ago) Feb 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 PM (2 days ago) Feb 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>
Reply all
Reply to author
Forward
0 new messages