Skip to content

Commit 83c3ad3

Browse files
Add a doc example for simplifiable-if-expression and simplifiable-if-statement
Refs #5953 Closes #5882
1 parent f4c6a25 commit 83c3ad3

8 files changed

Lines changed: 51 additions & 4 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
FLYING_THINGS = ["bird", "plane", "superman", "this example"]
2+
3+
4+
def is_flying_thing(an_object):
5+
return True if an_object in FLYING_THINGS else False # [simplifiable-if-expression]

doc/data/messages/s/simplifiable-if-expression/details.rst

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1-
# This is a placeholder for correct code for this message.
1+
FLYING_THINGS = ["bird", "plane", "superman", "this example"]
2+
3+
4+
def is_flying_thing(an_object):
5+
return an_object in FLYING_THINGS
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- `Simplifying an 'if' statement with bool() <https://stackoverflow.com/questions/49546992/>`_
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
FLYING_THINGS = ["bird", "plane", "superman", "this example"]
2+
3+
4+
def is_flying_animal(an_object):
5+
if isinstance(an_object, Animal) and an_object in FLYING_THINGS: # [simplifiable-if-statement]
6+
is_flying = True
7+
else:
8+
is_flying = False
9+
return is_flying

doc/data/messages/s/simplifiable-if-statement/details.rst

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,6 @@
1-
# This is a placeholder for correct code for this message.
1+
FLYING_THINGS = ["bird", "plane", "superman", "this example"]
2+
3+
4+
def is_flying_animal(an_object):
5+
is_flying = isinstance(an_object, Animal) and an_object.name in FLYING_THINGS
6+
return is_flying

tests/functional/s/simplifiable/simplifiable_if_expression.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,46 @@ def test_simplifiable_1(arg):
77
# Simple test that can be replaced by bool(arg)
88
return True if arg else False # [simplifiable-if-expression]
99

10+
1011
def test_simplifiable_2(arg):
1112
# Simple test that can be replaced by not arg
1213
return False if arg else True # [simplifiable-if-expression]
1314

15+
1416
def test_simplifiable_3(arg):
1517
# Simple test that can be replaced by arg == 1
1618
return True if arg == 1 else False # [simplifiable-if-expression]
1719

20+
1821
def test_simplifiable_4(arg):
1922
# Simple test that can be replaced by not (arg == 1)
2023
return False if arg == 1 else True # [simplifiable-if-expression]
2124

25+
2226
def test_not_simplifiable(arg):
2327
x = True if arg else True
2428
y = 0 if arg else 1
2529
t = False if arg != 1 else False
2630
t2 = None if arg > 3 else False
2731
return x, y, t, t2
32+
33+
34+
def x():
35+
a = []
36+
b = []
37+
if bool('special test'):
38+
a = True
39+
else:
40+
b = False
41+
return a + b
42+
43+
FLYING_THINGS = ["bird", "plane", "superman", "this example"]
44+
45+
46+
def is_flying_animal(an_object):
47+
is_flying = False
48+
if isinstance(an_object, str) and an_object in FLYING_THINGS: # [simplifiable-if-statement]
49+
is_flying = True
50+
else:
51+
is_flying = False
52+
return is_flying

0 commit comments

Comments
 (0)