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 | |
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
-rw-r--r-- | pylint/checkers/refactoring.py | 13 | ||||
-rw-r--r-- | pylint/test/functional/ternary.py | 8 | ||||
-rw-r--r-- | pylint/test/functional/ternary.txt | 1 |
3 files changed, 18 insertions, 4 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(), diff --git a/pylint/test/functional/ternary.py b/pylint/test/functional/ternary.py index f76ef335c..74adbaba2 100644 --- a/pylint/test/functional/ternary.py +++ b/pylint/test/functional/ternary.py @@ -30,4 +30,10 @@ SOME_VALUE9 = SOME_VALUE1 and False or SOME_VALUE2 # [simplify-boolean-expressi YEAR = 1992 # Cannot be simplified with a ternary. -is_leap_year = YEAR % 4 == 0 and YEAR % 100 != 0 or YEAR % 400 == 0 +IS_LEAP_YEAR = YEAR % 4 == 0 and YEAR % 100 != 0 or YEAR % 400 == 0 + + +def func4(): + """"Using a Name as a condition but still emits""" + truth_value = 42 + return condition and truth_value or false_value # [consider-using-ternary] diff --git a/pylint/test/functional/ternary.txt b/pylint/test/functional/ternary.txt index e893710b1..298aa57b6 100644 --- a/pylint/test/functional/ternary.txt +++ b/pylint/test/functional/ternary.txt @@ -6,3 +6,4 @@ consider-using-ternary:24::Consider using ternary ('ERROR' if some_callable(cond consider-using-ternary:25::Consider using ternary ('greater' if SOME_VALUE1 > 3 else 'not greater'):HIGH consider-using-ternary:26::Consider using ternary ('both' if SOME_VALUE2 > 4 and SOME_VALUE3 else 'not'):HIGH simplify-boolean-expression:29::Boolean expression may be simplified to SOME_VALUE2:HIGH +consider-using-ternary:39:func4:Consider using ternary (truth_value if condition else false_value):HIGH |