Skip to content

Commit

Permalink
Fix incorrect consider-using-ternary when condition is inferable …
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
areveny committed Oct 29, 2021
1 parent e871387 commit 8c7e2fa
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 2 deletions.
4 changes: 4 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions doc/whatsnew/2.12.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions pylint/checkers/refactoring/refactoring_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -1393,10 +1393,10 @@ def visit_return(self, node: nodes.Return) -> None:
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"
Expand Down
6 changes: 6 additions & 0 deletions tests/functional/t/ternary.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
1 change: 1 addition & 0 deletions tests/functional/t/ternary.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 8c7e2fa

Please sign in to comment.