summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTomer Chachamu <tomer.chachamu@gmail.com>2018-09-20 08:15:35 +0100
committerClaudiu Popa <pcmanticore@gmail.com>2018-09-20 09:15:35 +0200
commit9c35a56906221b7f766f8a160f4fdd0f286232e2 (patch)
tree174b87e3256a5018e044bbd0d682d17553bd2ed3
parent66f71dbd6411b66eac95ff8e41ccb54b8bb8d396 (diff)
downloadpylint-git-9c35a56906221b7f766f8a160f4fdd0f286232e2.tar.gz
Added check simplifiable-if-expression (#2502)
Similar to simplifiable-if-statement, it finds trivial ternary statements and suggested replacing them. Paired-with: Richard Goodman <richardg@brandwatch.com>
-rw-r--r--CONTRIBUTORS.txt2
-rw-r--r--ChangeLog4
-rw-r--r--pylint/checkers/refactoring.py34
-rw-r--r--pylint/test/functional/boolean_datetime.py2
-rw-r--r--pylint/test/functional/simplifiable_if_expression.py27
-rw-r--r--pylint/test/functional/simplifiable_if_expression.txt4
6 files changed, 72 insertions, 1 deletions
diff --git a/CONTRIBUTORS.txt b/CONTRIBUTORS.txt
index 54cd46b84..4e2297f18 100644
--- a/CONTRIBUTORS.txt
+++ b/CONTRIBUTORS.txt
@@ -231,3 +231,5 @@ contributors:
* Yuri Gribov: contributor
* Drew Risinger: committer (docs)
+
+* Tomer Chachamu, Richard Goodman: simplifiable-if-expression \ No newline at end of file
diff --git a/ChangeLog b/ChangeLog
index 34dbb2016..2562bfc82 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -7,6 +7,10 @@ What's New in Pylint 2.2?
Release date: TBA
+ * Add a new check, ``simplifiable-if-expression`` for expressions like ``True if cond else False``.
+
+ Close #2487
+
* ``too-few-public-methods`` is not reported for ``typing.NamedTuple``
Close #2459
diff --git a/pylint/checkers/refactoring.py b/pylint/checkers/refactoring.py
index caadd791e..dd19dfbe5 100644
--- a/pylint/checkers/refactoring.py
+++ b/pylint/checkers/refactoring.py
@@ -200,6 +200,11 @@ class RefactoringChecker(checkers.BaseTokenChecker):
"Also it is faster since you don't need to create another "
"transient list",
),
+ "R1719": (
+ "The if expression can be replaced with %s",
+ "simplifiable-if-expression",
+ "Used when an if expression can be replaced with " "'bool(test)'. ",
+ ),
}
options = (
(
@@ -465,6 +470,35 @@ class RefactoringChecker(checkers.BaseTokenChecker):
self._check_superfluous_else_return(node)
self._check_consider_get(node)
+ @utils.check_messages("simplifiable-if-expression")
+ def visit_ifexp(self, node):
+ self._check_simplifiable_ifexp(node)
+
+ def _check_simplifiable_ifexp(self, node):
+ if not isinstance(node.body, astroid.Const) or not isinstance(
+ node.orelse, astroid.Const
+ ):
+ return
+
+ if not isinstance(node.body.value, bool) or not isinstance(
+ node.orelse.value, bool
+ ):
+ return
+
+ if isinstance(node.test, astroid.Compare):
+ test_reduced_to = "test"
+ else:
+ test_reduced_to = "bool(test)"
+
+ if (node.body.value, node.orelse.value) == (True, False):
+ reduced_to = "'{}'".format(test_reduced_to)
+ elif (node.body.value, node.orelse.value) == (False, True):
+ reduced_to = "'not test'"
+ else:
+ return
+
+ self.add_message("simplifiable-if-expression", node=node, args=(reduced_to,))
+
@utils.check_messages(
"too-many-nested-blocks", "inconsistent-return-statements", "useless-return"
)
diff --git a/pylint/test/functional/boolean_datetime.py b/pylint/test/functional/boolean_datetime.py
index 9838b81fc..5afc5a42e 100644
--- a/pylint/test/functional/boolean_datetime.py
+++ b/pylint/test/functional/boolean_datetime.py
@@ -1,5 +1,5 @@
""" Checks for boolean uses of datetime.time. """
-# pylint: disable=superfluous-parens,print-statement,no-absolute-import,consider-using-ternary
+# pylint: disable=superfluous-parens,print-statement,no-absolute-import,consider-using-ternary,simplifiable-if-expression
import datetime
if datetime.time(0, 0, 0): # [boolean-datetime]
diff --git a/pylint/test/functional/simplifiable_if_expression.py b/pylint/test/functional/simplifiable_if_expression.py
new file mode 100644
index 000000000..bcfdf890b
--- /dev/null
+++ b/pylint/test/functional/simplifiable_if_expression.py
@@ -0,0 +1,27 @@
+"""Test that some if expressions can be simplified."""
+
+# pylint: disable=missing-docstring, invalid-name
+
+
+def test_simplifiable_1(arg):
+ # Simple test that can be replaced by bool(arg)
+ return True if arg else False # [simplifiable-if-expression]
+
+def test_simplifiable_2(arg):
+ # Simple test that can be replaced by not arg
+ return False if arg else True # [simplifiable-if-expression]
+
+def test_simplifiable_3(arg):
+ # Simple test that can be replaced by arg == 1
+ return True if arg == 1 else False # [simplifiable-if-expression]
+
+def test_simplifiable_4(arg):
+ # Simple test that can be replaced by not (arg == 1)
+ return False if arg == 1 else True # [simplifiable-if-expression]
+
+def test_not_simplifiable(arg):
+ x = True if arg else True
+ y = 0 if arg else 1
+ t = False if arg != 1 else False
+ t2 = None if arg > 3 else False
+ return x, y, t, t2
diff --git a/pylint/test/functional/simplifiable_if_expression.txt b/pylint/test/functional/simplifiable_if_expression.txt
new file mode 100644
index 000000000..b35e08462
--- /dev/null
+++ b/pylint/test/functional/simplifiable_if_expression.txt
@@ -0,0 +1,4 @@
+simplifiable-if-expression:8:test_simplifiable_1:The if expression can be replaced with 'bool(test)'
+simplifiable-if-expression:12:test_simplifiable_2:The if expression can be replaced with 'not test'
+simplifiable-if-expression:16:test_simplifiable_3:The if expression can be replaced with 'test'
+simplifiable-if-expression:20:test_simplifiable_4:The if expression can be replaced with 'not test' \ No newline at end of file