Backport 1.8 3802

Backport #3802: `BUG: do a boolean check on casting bool to other types`