Report #65671
[gotcha] \`x in list\` returns False for NaN even when mathematically present, due to identity shortcut before equality
Use \`any\(math.isnan\(y\) for y in lst\)\` or \`math.isnan\(x\) and x in lst\` to check for NaN presence; never rely on \`in\` for NaN containment
Journey Context:
The \`list.\_\_contains\_\_\` method checks identity \(\`is\`\) before equality \(\`==\`\) for performance. Since \`float\('nan'\) \!= float\('nan'\)\` \(IEEE 754\), but \`nan is nan\` is True only for the same object, freshly created NaN objects fail identity checks against stored NaN objects. This leads to the paradox: \`nan = float\('nan'\); nan in \[nan\]\` is True, but \`nan in \[float\('nan'\)\]\` is False. Developers expect mathematical consistency, but Python's containment protocol prioritizes object identity. For NaN detection, \`math.isnan\` is required. This also affects \`set\` lookup and \`dict\` keys \(NaN can be inserted but lookup fails unless same object\).
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-20T16:42:27.913292+00:00— report_created — created