diff options
author | Arianna Y <92831762+areveny@users.noreply.github.com> | 2021-10-29 12:44:18 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-10-29 21:44:18 +0200 |
commit | 8c7e2fae6fee28944764e643e65e729a58a5473c (patch) | |
tree | 2dc743324eea667906bcafcc5a313620d40dd8c7 | |
parent | e8713873813bfab5fafb04b0fb8b5221011faa41 (diff) | |
download | pylint-git-8c7e2fae6fee28944764e643e65e729a58a5473c.tar.gz |
Fix incorrect ``consider-using-ternary`` when condition is inferable as False (#5227)
* Fix incorrect ``consider-using-ternary`` when condition is inferrable as False
* Properly infer the condition in old ternary statements and suggest ``simplify-boolean-expression`` over ``consider-using-ternary`` if it is False
-rw-r--r-- | ChangeLog | 4 | ||||
-rw-r--r-- | doc/whatsnew/2.12.rst | 4 | ||||
-rw-r--r-- | pylint/checkers/refactoring/refactoring_checker.py | 4 | ||||
-rw-r--r-- | tests/functional/t/ternary.py | 6 | ||||
-rw-r--r-- | tests/functional/t/ternary.txt | 1 |
5 files changed, 17 insertions, 2 deletions
@@ -11,6 +11,10 @@ Release date: TBA .. Put new features here and also in 'doc/whatsnew/2.12.rst' +* Fix ``simplify-boolean-expression`` when condition can be inferred as False. + + Closes #5200 + * Fix exception when pyreverse parses ``property function`` of a class. * Add an optional extension ``consider-using-any-or-all`` : Emitted when a ``for`` loop only diff --git a/doc/whatsnew/2.12.rst b/doc/whatsnew/2.12.rst index e4bb8ac47..0f16e4bdd 100644 --- a/doc/whatsnew/2.12.rst +++ b/doc/whatsnew/2.12.rst @@ -64,6 +64,10 @@ Extensions Other Changes ============= +* Fix ``simplify-boolean-expression`` when condition can be inferred as False. + + Closes #5200 + * Fix exception when pyreverse parses ``property function`` of a class. * Improve and flatten ``unused-wildcard-import`` message diff --git a/pylint/checkers/refactoring/refactoring_checker.py b/pylint/checkers/refactoring/refactoring_checker.py index e8e403e1b..7929d77e0 100644 --- a/pylint/checkers/refactoring/refactoring_checker.py +++ b/pylint/checkers/refactoring/refactoring_checker.py @@ -1393,10 +1393,10 @@ class RefactoringChecker(checkers.BaseTokenChecker): return inferred_truth_value = utils.safe_infer(truth_value) - if inferred_truth_value in (None, astroid.Uninferable): + if inferred_truth_value is None or inferred_truth_value == astroid.Uninferable: truth_boolean_value = True else: - truth_boolean_value = truth_value.bool_value() + truth_boolean_value = inferred_truth_value.bool_value() if truth_boolean_value is False: message = "simplify-boolean-expression" diff --git a/tests/functional/t/ternary.py b/tests/functional/t/ternary.py index deac01e3c..58171942f 100644 --- a/tests/functional/t/ternary.py +++ b/tests/functional/t/ternary.py @@ -31,3 +31,9 @@ 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] + + +def func5(): + """"Using a Name that infers to False as a condition does not emit""" + falsy_value = False + return condition and falsy_value or false_value # [simplify-boolean-expression] diff --git a/tests/functional/t/ternary.txt b/tests/functional/t/ternary.txt index d78aa45a6..1df60e2f7 100644 --- a/tests/functional/t/ternary.txt +++ b/tests/functional/t/ternary.txt @@ -5,3 +5,4 @@ consider-using-ternary:19:0::Consider using ternary ('greater' if SOME_VALUE1 > consider-using-ternary:20:0::Consider using ternary ('both' if SOME_VALUE2 > 4 and SOME_VALUE3 else 'not') simplify-boolean-expression:23:0::Boolean expression may be simplified to SOME_VALUE2 consider-using-ternary:33:4:func4:Consider using ternary (truth_value if condition else false_value) +simplify-boolean-expression:39:4:func5:Boolean expression may be simplified to false_value |