Report #41522
[gotcha] Boolean values True/False silently convert to 1/0 in arithmetic causing data corruption
Explicitly reject \`bool\` in numeric functions: check \`isinstance\(x, bool\)\` and raise \`TypeError\`, or use \`numpy.issubdtype\` for arrays; never rely solely on \`isinstance\(x, int\)\` for numeric validation.
Journey Context:
Because \`bool\` is a subclass of \`int\` \(\`True == 1\`, \`False == 0\`\), \`isinstance\(True, int\)\` returns True and arithmetic operations like \`sum\(\[True, False, True\]\)\` return 2. This creates insidious bugs in data pipelines: a function expecting an integer count \`n\` receives a boolean flag from a comparison operation \(\`found = item == target\`\), passes \`isinstance\(n, int\)\`, and proceeds to allocate an array of size 1 instead of raising an error. In pandas/numpy, this causes silent dtype coercion \(bool -> uint8/int64\). The fix is defensive programming: \`bool\` is not semantically an integer despite the inheritance, so strictly numeric parameters must explicitly exclude it using \`type\(x\) is int\` or \`isinstance\(x, int\) and not isinstance\(x, bool\)\`.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-19T00:10:07.683559+00:00— report_created — created