Opened 3 weeks ago

Closed 37 hours ago

#36899 closed New feature (fixed)

Implement `Session.__bool__`

Reported by: Jake Howard Owned by: Amar
Component: contrib.sessions Version: dev
Severity: Normal Keywords:
Cc: jaffar Khan Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

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).

Change History (15)

comment:1 by jaffar Khan, 3 weeks ago

Cc: jaffar Khan added

comment:2 by Ahmed Asar, 3 weeks ago

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!

comment:3 by Natalia Bidart, 3 weeks ago

Triage Stage: UnreviewedAccepted
Type: Cleanup/optimizationNew feature

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.

comment:4 by Jacob Walls, 3 weeks ago

Jake, any thought about how/where to doc this? Minor incompatible change? Doubt we want a deprecation path.

comment:5 by Amar, 3 weeks ago

Owner: set to Amar
Status: newassigned

comment:6 by Ahmed Asar, 3 weeks ago

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!

in reply to:  6 comment:7 by Amar, 3 weeks ago

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

comment:8 by Amar, 3 weeks ago

Has patch: set

comment:11 by Amar, 5 days ago

Triage Stage: AcceptedReady for checkin

comment:12 by Natalia Bidart, 5 days ago

Triage Stage: Ready for checkinAccepted

Amar, you can not set the flag "Ready for Checkin" in your own PR, please read carefully the contributing docs.

comment:13 by Tim Graham, 5 days ago

Triage Stage: AcceptedReady for checkin

The PR has an approval from Jake.

comment:14 by Amar, 5 days ago

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.

comment:15 by Jacob Walls, 2 days ago

Easy pickings: unset
Needs tests: set
Patch needs improvement: set
Triage Stage: Ready for checkinAccepted

comment:16 by Jacob Walls, 38 hours ago

Needs tests: unset
Patch needs improvement: unset
Triage Stage: AcceptedReady for checkin
Version: 6.0dev

comment:17 by Jacob Walls <jacobtylerwalls@…>, 37 hours ago

Resolution: fixed
Status: assignedclosed

In 158fd81:

Fixed #36899 -- Implemented SessionBase.bool.

Note: See TracTickets for help on using tickets.
Back to Top