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 , 3 weeks ago
| Cc: | added |
|---|
comment:2 by , 3 weeks ago
comment:3 by , 3 weeks ago
| Triage Stage: | Unreviewed → Accepted |
|---|---|
| Type: | Cleanup/optimization → New 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 , 3 weeks ago
Jake, any thought about how/where to doc this? Minor incompatible change? Doubt we want a deprecation path.
comment:5 by , 3 weeks ago
| Owner: | set to |
|---|---|
| Status: | new → assigned |
follow-up: 7 comment:6 by , 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!
comment:7 by , 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 , 3 weeks ago
| Has patch: | set |
|---|
comment:11 by , 5 days ago
| Triage Stage: | Accepted → Ready for checkin |
|---|
comment:12 by , 5 days ago
| Triage Stage: | Ready for checkin → Accepted |
|---|
Amar, you can not set the flag "Ready for Checkin" in your own PR, please read carefully the contributing docs.
comment:13 by , 5 days ago
| Triage Stage: | Accepted → Ready for checkin |
|---|
The PR has an approval from Jake.
comment:14 by , 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 , 2 days ago
| Easy pickings: | unset |
|---|---|
| Needs tests: | set |
| Patch needs improvement: | set |
| Triage Stage: | Ready for checkin → Accepted |
comment:16 by , 38 hours ago
| Needs tests: | unset |
|---|---|
| Patch needs improvement: | unset |
| Triage Stage: | Accepted → Ready for checkin |
| Version: | 6.0 → dev |
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!