Report #11660
[gotcha] Boolean operators and/or return operands not booleans
Use bool\(x and y\) when a boolean is strictly required for serialization or type-sensitive contexts; otherwise embrace the behavior for default values. Never assume result is True/False.
Journey Context:
Python's and and or are short-circuit operators that return the last evaluated operand, not a coerced boolean. 1 and 2 returns 2, while 0 or \[\] returns \[\]. This enables concise default values \(x or 'default'\) but causes subtle bugs when the result is passed to functions requiring actual booleans \(JSON serializers, database drivers expecting BIT/Bool, or bitwise operations\). The fix requires explicit bool\(\) wrapping when the downstream consumer requires a boolean type, converting the operand to True/False. Alternatively, conditional expressions \(x and y\) is True provide strict type checking. This differs from other languages where boolean operators return primitive booleans.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T13:51:41.988159+00:00— report_created — created