Opened 91 minutes ago
Last modified 17 minutes ago
#36899 new Cleanup/optimization
Implement `Session.__bool__`
| Reported by: | Jake Howard | Owned by: | |
|---|---|---|---|
| Component: | contrib.sessions | Version: | 6.0 |
| Severity: | Normal | Keywords: | |
| Cc: | jaffar Khan | Triage Stage: | Unreviewed |
| Has patch: | no | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | yes | 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).
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!