diff options
author | Claudiu Popa <pcmanticore@gmail.com> | 2018-07-08 12:21:51 +0200 |
---|---|---|
committer | Claudiu Popa <pcmanticore@gmail.com> | 2018-07-08 12:21:51 +0200 |
commit | aa0ef8582711ab60461b4379af6bcd817bcc59ca (patch) | |
tree | 80d211dd7ea517ac24a7188dbd43128da25d2c3a /pylint/checkers/refactoring.py | |
parent | 0d4aedafdd474cc503f744241e2d48a188302206 (diff) | |
download | pylint-git-aa0ef8582711ab60461b4379af6bcd817bcc59ca.tar.gz |
Infer the value of the truth_value before looking for consider-using-ternary and simplifiable-boolean-expression
The reason for that is that a Name node, for instance, will not have a bool_value()
implementation, thus it will default to returning Uninferable. In order to avoid
that, just infer the object before verifying anything about it.
Related to #2058
Diffstat (limited to 'pylint/checkers/refactoring.py')
-rw-r--r-- | pylint/checkers/refactoring.py | 13 |
1 files changed, 10 insertions, 3 deletions
diff --git a/pylint/checkers/refactoring.py b/pylint/checkers/refactoring.py index fa24169b2..df554f791 100644 --- a/pylint/checkers/refactoring.py +++ b/pylint/checkers/refactoring.py @@ -703,13 +703,20 @@ class RefactoringChecker(checkers.BaseTokenChecker): else: return - if truth_value.bool_value() is False: + if all(isinstance(value, astroid.Compare) for value in (truth_value, false_value)): + return + + inferred_truth_value = utils.safe_infer(truth_value) + 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' - if all(isinstance(value, astroid.Compare) for value in (truth_value, false_value)): - return suggestion = '{truth} if {cond} else {false}'.format( truth=truth_value.as_string(), cond=cond.as_string(), |