Skip to content

Commit

Permalink
Add a doc example for simplifiable-if-expression and simplifiable-if-…
Browse files Browse the repository at this point in the history
…statement

Refs pylint-dev#5953
Closes pylint-dev#5882
  • Loading branch information
Pierre-Sassoulas committed May 29, 2022
1 parent f4c6a25 commit 35fe42b
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 7 deletions.
9 changes: 9 additions & 0 deletions doc/data/messages/s/simplifiable-if-expression/bad.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
FLYING_THINGS = ["bird", "plane", "superman", "this example"]


def is_flying_thing(an_object):
return True if an_object in FLYING_THINGS else False # [simplifiable-if-expression]


def is_not_flying_thing(an_object):
return False if an_object in FLYING_THINGS else True # [simplifiable-if-expression]
1 change: 0 additions & 1 deletion doc/data/messages/s/simplifiable-if-expression/details.rst

This file was deleted.

10 changes: 9 additions & 1 deletion doc/data/messages/s/simplifiable-if-expression/good.py
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
# This is a placeholder for correct code for this message.
FLYING_THINGS = ["bird", "plane", "superman", "this example"]


def is_flying_thing(an_object):
return an_object in FLYING_THINGS


def is_not_flying_thing(an_object):
return an_object not in FLYING_THINGS
1 change: 1 addition & 0 deletions doc/data/messages/s/simplifiable-if-expression/related.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- `Simplifying an 'if' statement with bool() <https://stackoverflow.com/questions/49546992/>`_
9 changes: 9 additions & 0 deletions doc/data/messages/s/simplifiable-if-statement/bad.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
FLYING_THINGS = ["bird", "plane", "superman", "this example"]


def is_flying_animal(an_object):
if isinstance(an_object, Animal) and an_object in FLYING_THINGS: # [simplifiable-if-statement]
is_flying = True
else:
is_flying = False
return is_flying
1 change: 0 additions & 1 deletion doc/data/messages/s/simplifiable-if-statement/details.rst

This file was deleted.

7 changes: 6 additions & 1 deletion doc/data/messages/s/simplifiable-if-statement/good.py
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
# This is a placeholder for correct code for this message.
FLYING_THINGS = ["bird", "plane", "superman", "this example"]


def is_flying_animal(an_object):
is_flying = isinstance(an_object, Animal) and an_object.name in FLYING_THINGS
return is_flying
26 changes: 26 additions & 0 deletions tests/functional/s/simplifiable/simplifiable_if_expression.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,47 @@ 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


def convoluted_function():
a = []
b = []
if bool('special test'):
a = True
else:
b = False
return a + b


FLYING_THINGS = ["bird", "plane", "superman", "this example"]


def is_flying_animal(an_object):
is_flying = False
if isinstance(an_object, str) and an_object in FLYING_THINGS: # [simplifiable-if-statement]
is_flying = True
else:
is_flying = False
return is_flying
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
simplifiable-if-expression:8:11:8:33:test_simplifiable_1:The if expression can be replaced with 'bool(test)':UNDEFINED
simplifiable-if-expression:12:11:12:33:test_simplifiable_2:The if expression can be replaced with 'not test':UNDEFINED
simplifiable-if-expression:16:11:16:38:test_simplifiable_3:The if expression can be replaced with 'test':UNDEFINED
simplifiable-if-expression:20:11:20:38:test_simplifiable_4:The if expression can be replaced with 'not test':UNDEFINED
simplifiable-if-expression:13:11:13:33:test_simplifiable_2:The if expression can be replaced with 'not test':UNDEFINED
simplifiable-if-expression:18:11:18:38:test_simplifiable_3:The if expression can be replaced with 'test':UNDEFINED
simplifiable-if-expression:23:11:23:38:test_simplifiable_4:The if expression can be replaced with 'not test':UNDEFINED
simplifiable-if-statement:49:4:52:25:is_flying_animal:The if statement can be replaced with 'var = bool(test)':UNDEFINED

0 comments on commit 35fe42b

Please sign in to comment.