You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This is a continuation of #1559 and #2058 which concern the message consider-using-ternary and the behavior on statements like a and b or c where b might be False.
#1559 made the check emit simplify-boolean-expression if b is falsey, suggesting simplifying to c.
#2058 tries to infer the value of b if it is an expression. However, I think the logic is a bit faulty.
if inferred_truth_value in (None, astroid.Uninferable):
truth_boolean_value = True
else:
truth_boolean_value = truth_value.bool_value()
if truth_boolean_value is False:
message = 'simplify-boolean-expression'
suggestion = false_value.as_string()
else:
message = 'consider-using-ternary'
suggestion = '{truth} if {cond} else {false}'.format(
truth=truth_value.as_string(),
cond=cond.as_string(),
false=false_value.as_string()
)
If truth_value is any variable, then truth_boolean_value.bool_value() is Uninferable. This means we always suggest consider-using-ternary unless truth_value is a falsey literal. The integration test on this ticket only tests the case where truth_value is truthy, and even then it doesn't suggest consider-using-ternary because truth_boolean_value is True, but because it is Uninferable.
My fix is to change truth_boolean_value = truth_value.bool_value() to truth_boolean_value = inferred_truth_value.bool_value(). I think this was the original intention. This will try to infer the boolean value of b, and sees that it is False, and suggests simplify-boolean-expression instead.
Bug description
Configuration
No response
Command used
Pylint output
Expected behavior
Success.
Pylint version
OS / Environment
Fedora 34
Additional dependencies
No response
The text was updated successfully, but these errors were encountered: