Consider allowing boolean tests of arbitrary values to fail
brandjon opened this issue · 3 comments
The spec requires that
any value may be converted to [...] a Boolean truth value using the expression
bool(x)
.
This FR would consider relaxing this to allow truth-checking to fail for certain types, so e.g. if some_application_defined_type:
could be an error.
This would enable banning (via a dynamic error) certain patterns that are error-prone, such as Bazel's select
, where a boolean could be inadvertently conflated with an object wrapping a computation yielding a boolean. See bazelbuild/bazel#14506.
Downsides: This would effectively prohibit programmatically taking the truthiness of arbitrary values, including where this is done implicitly (without calling bool()
), and break existing code that does this. A more minor detail is that it would require implementaitons (particularly the Java interpreter) to propagate an exception where one is not declared today.
Alternatives: Rely on linting, style guides, and documentation to mitigate the damage caused by that kind of confusion.
Personally I'm hesitant to restrict truth-checking in this way.
Note that it would also break some patterns, like x or default_value
See update in aforementioned bug. We want to do this, at least for implicit bool conversion.
Personally I'm hesitant to restrict truth-checking in this way.
Me too. Truthiness has long been a part of Python style, and revoking it would be an enormously incompatible change.