Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix incorrect consider-using-ternary when condition is inferable as False #5227

Merged
merged 3 commits into from
Oct 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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