#27154 closed Cleanup/optimization (fixed)
Allow comparing CallableFalse/CallableTrue with bitwise or
| Reported by: | Tom Christie | Owned by: | Olexander Yermakov |
|---|---|---|---|
| Component: | contrib.auth | Version: | 1.10 |
| Severity: | Release blocker | Keywords: | |
| Cc: | Triage Stage: | Ready for checkin | |
| Has patch: | yes | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | yes | UI/UX: | no |
Description
Original raised as a ticket in REST framework: https://github.com/tomchristie/django-rest-framework/issues/4439
Something that may confuse users calling .is_authenticated...
There's a slight difference in behavior between how the CallableFalse/CallableTrue instances behave compare to how False/True behave.
The Python boolean type happens to support the bitwise OR operator, and its behavior in this case is exactly equivalent to the or operator. Eg. False | True returns True
The backwards compat CallableFalse/CallableTrue instances do not support this usage, and will fail with eg. TypeError: unsupported operand type(s) for |: 'instance' and 'bool'
Their behavior is reasonable enough, and the user should really be using the boolean or operator, but we might introduce slightly less friction (ie don't raise a slightly opaque error message to the user) if we simply support and allow the bitwise operator in the same way as the boolean equivalents.
I'm probably a +0 on resolving this, as think it'd save a few folks a bit of head banging. Certainly wouldn't dispute a wontfix categorization.
Tip for easy pickings folks: Should be resolvable by including an __or__ method on the CallableFalse/CallableTrue classes.
Change History (7)
comment:1 by , 9 years ago
| Triage Stage: | Unreviewed → Accepted |
|---|
comment:2 by , 9 years ago
| Severity: | Normal → Release blocker |
|---|
The implementation can follow commit 54afa960d1ee8c63635225a0f0a2489971b5aab5.
comment:3 by , 9 years ago
| Owner: | changed from to |
|---|---|
| Status: | new → assigned |
comment:5 by , 9 years ago
| Has patch: | set |
|---|---|
| Summary: | Minor difference in behavior between CallableFalse/CallableTrue and False/True. → Allow comparing CallableFalse/CallableTrue with bitwise or |
| Triage Stage: | Accepted → Ready for checkin |
The change seems simple enough and I don't see any drawbacks to adding it.
If we can make our users' lives easier, I'm +1 on it.
If anyone is interested in tackling this, the
CallableFalse/CallableTrueclasses are defined indjango/utils/deprecation.py.Once the change is made, I think we should also discuss backporting it.
Thanks.