gvsig-scripting / org.gvsig.scripting / trunk / org.gvsig.scripting / org.gvsig.scripting.app / org.gvsig.scripting.app.mainplugin / src / main / resources-plugin / scripting / lib / pylint / test / functional / simplifiable_if_statement.py @ 745
History | View | Annotate | Download (3.03 KB)
1 |
"""Test that some if statement tests can be simplified."""
|
---|---|
2 |
|
3 |
# pylint: disable=missing-docstring, invalid-name
|
4 |
|
5 |
|
6 |
def test_simplifiable_1(arg): |
7 |
# Simple test that can be replaced by bool(arg)
|
8 |
if arg: # [simplifiable-if-statement] |
9 |
return True |
10 |
else:
|
11 |
return False |
12 |
|
13 |
|
14 |
def test_simplifiable_2(arg, arg2): |
15 |
# Can be reduced to bool(arg and not arg2)
|
16 |
if arg and not arg2: # [simplifiable-if-statement] |
17 |
return True |
18 |
else:
|
19 |
return False |
20 |
|
21 |
|
22 |
def test_simplifiable_3(arg, arg2): |
23 |
# Can be reduced to bool(arg and not arg2)
|
24 |
if arg and not arg2: # [simplifiable-if-statement] |
25 |
var = True
|
26 |
else:
|
27 |
var = False
|
28 |
return var
|
29 |
|
30 |
|
31 |
def test_simplifiable_4(arg): |
32 |
if arg:
|
33 |
var = True
|
34 |
else:
|
35 |
if arg == "arg1": # [simplifiable-if-statement] |
36 |
return True |
37 |
else:
|
38 |
return False |
39 |
return var
|
40 |
|
41 |
|
42 |
def test_not_necessarily_simplifiable_1(arg, arg2): |
43 |
# Can be reduced to bool(not arg and not arg2) or to
|
44 |
# `not all(N)`, which is a bit harder to understand
|
45 |
# than `any(N)` when var should be False.
|
46 |
if arg or arg2: |
47 |
var = False
|
48 |
else:
|
49 |
var = True
|
50 |
return var
|
51 |
|
52 |
|
53 |
def test_not_necessarily_simplifiabile_2(arg): |
54 |
# This could theoretically be reduced to `not arg or arg > 3`
|
55 |
# but the net result is that now the condition is harder to understand,
|
56 |
# because it requires understanding of an extra clause:
|
57 |
# * first, there is the negation of truthness with `not arg`
|
58 |
# * the second clause is `arg > 3`, which occurs when arg has a
|
59 |
# a truth value, but it implies that `arg > 3` is equivalent
|
60 |
# with `arg and arg > 3`, which means that the user must
|
61 |
# think about this assumption when evaluating `arg > 3`.
|
62 |
# The original form is easier to grasp.
|
63 |
if arg and arg <= 3: |
64 |
return False |
65 |
else:
|
66 |
return True |
67 |
|
68 |
|
69 |
def test_not_simplifiable_3(arg): |
70 |
if arg:
|
71 |
test_not_necessarily_simplifiabile_2(arg) |
72 |
test_not_necessarily_simplifiable_1(arg, arg) |
73 |
return False |
74 |
else:
|
75 |
if arg < 3: |
76 |
test_simplifiable_3(arg, 42)
|
77 |
return True |
78 |
|
79 |
|
80 |
def test_not_simplifiable_4(arg): |
81 |
# Not interested in multiple elifs
|
82 |
if arg == "any": |
83 |
return True |
84 |
elif test_not_simplifiable_3(arg) == arg:
|
85 |
return True |
86 |
else:
|
87 |
return False |
88 |
|
89 |
|
90 |
def test_not_simplifiable_5(arg): |
91 |
# Different actions in each branch
|
92 |
if arg == "any": |
93 |
return True |
94 |
else:
|
95 |
var = 42
|
96 |
return var
|
97 |
|
98 |
|
99 |
def test_not_simplifiable_6(arg): |
100 |
# Different actions in each branch
|
101 |
if arg == "any": |
102 |
var = 42
|
103 |
else:
|
104 |
return True |
105 |
return var
|
106 |
|
107 |
def test_not_simplifiable_7(arg): |
108 |
# Returning something different
|
109 |
if arg == "any": |
110 |
return 4 |
111 |
else:
|
112 |
return 5 |
113 |
|
114 |
|
115 |
def test_not_simplifiable_8(arg): |
116 |
# Only one of the branch returns something boolean
|
117 |
if arg == "any": |
118 |
return True |
119 |
else:
|
120 |
return 0 |