/
test_900_rules.py
195 lines (168 loc) · 4.71 KB
/
test_900_rules.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# Third party
import pytest
# First party
from tests import _results
def test_sim901():
results = _results("bool(a == b)")
assert results == {"1:0 SIM901 Use 'a == b' instead of 'bool(a == b)'"}
@pytest.mark.parametrize(
"s",
(
"foo(a, b, True)",
"set_foo(a, b, True)",
),
ids=[
"basic",
"set_multiple",
],
)
def test_sim902(s):
error_messages = _results(s)
assert any("SIM902" in error_message for error_message in error_messages)
@pytest.mark.parametrize(
"s",
(
"foo(a, b, foo=True)",
"dict.get('foo', True)",
"set_visible(True)",
"line.set_visible(True)",
"partial(foo, True)",
"partial(foo, bar=True)",
),
ids=[
"kw_arg_is_used",
"dict_get",
"boolean_setter_function",
"boolean_setter_method",
"partial_arg",
"partial_kwarg",
],
)
def test_sim902_false_positive_check(s):
error_messages = _results(s)
for error_message in error_messages:
assert "SIM902" not in error_message
@pytest.mark.parametrize(
"s",
("foo(a, b, 123123)", "foo(a, b, 123.123)"),
ids=["int", "float"],
)
def test_sim903_true_positive_check(s):
error_messages = _results(s)
assert any("SIM903" in error_message for error_message in error_messages)
@pytest.mark.parametrize(
"s",
(
"dict.get('foo', 123)",
"set_foo(1.23)",
"line.set_foo(1.23)",
"partial(foo, 1, 2, 3)",
"min(0.5, g_norm)",
"QColor(53, 53, 53, 128)",
),
ids=[
"get_exception",
"set_function",
"set_method",
"partial",
"min",
"color",
],
)
def test_sim903_false_positive_check(s):
error_messages = _results(s)
for error_message in error_messages:
assert "SIM903" not in error_message
def test_sim903_insert_exception():
ret = _results("sys.path.insert(0, 'foo')")
assert ret == set()
def test_sim903_range_exception():
ret = _results("range(42)")
assert ret == set()
@pytest.mark.parametrize(
"s",
(
"""a = { }
a['b'] = 'c'""",
pytest.param(
"""a = { }
a['b'] = ba""",
marks=pytest.mark.xfail,
),
),
ids=["minimal", "contains-name"],
)
def test_sim904(s):
results = _results(s)
assert results == {"1:0 SIM904 Initialize dictionary 'a' directly"}
@pytest.mark.parametrize(
"s",
(
# Credits to MetRonnie for the following example
# https://github.com/MartinThoma/flake8-simplify/issues/99
"""my_dict = {
'foo': [1, 2, 3, 4],
'bar': [5, 6, 7, 8]
}
my_dict['both'] = [item for _list in my_dict.values() for item in _list]""",
# Credits to Skylion007 for the following two examples
# https://github.com/MartinThoma/flake8-simplify/issues/100
"""perf = {"total_time": end_time - start_time}
perf["frame_time"] = perf["total_time"] / total_frames
perf["fps"] = 1.0 / perf["frame_time"]
perf["time_per_step"] = time_per_step
perf["avg_sim_step_time"] = total_sim_step_time / total_frames""",
"""perf = {"a": 1}
perf["b"] = perf["a"] / 10""",
),
ids=["issue-99", "issue-100-1", "issue-100-2"],
)
def test_sim904_false_positives(s):
results = _results(s)
for result in results:
assert "SIM904" not in result
def test_sim905():
results = _results("""domains = "de com net org".split()""")
assert results == {
'1:10 SIM905 Use \'["de", "com", "net", "org"]\' '
"instead of '\"de com net org\".split()'"
}
@pytest.mark.parametrize(
("s", "msg"),
(
# Credits to Skylion007 for the following example
# https://github.com/MartinThoma/flake8-simplify/issues/101
(
"os.path.join(a,os.path.join(b,c))",
"1:0 SIM906 Use 'os.path.join(a, b, c)' "
"instead of 'os.path.join(a, os.path.join(b, c))'",
),
(
"os.path.join(a,os.path.join('b',c))",
"1:0 SIM906 Use 'os.path.join(a, 'b', c)' "
"instead of 'os.path.join(a, os.path.join('b', c))'",
),
),
ids=["base", "str-arg"],
)
def test_sim906(s, msg):
results = _results(s)
assert results == {msg}
def test_sim907():
results = _results(
"""def foo(a: Union[int, None]) -> bool:
return a"""
)
assert results == {
"1:11 SIM907 Use 'Optional[int]' instead of 'Union[int, None]'"
}
def test_sim908():
results = _results(
"""name = "some_default"
if "some_key" in some_dict:
name = some_dict["some_key"]"""
)
assert results == {
"2:0 SIM908 Use 'some_dict.get(\"some_key\")' instead of "
'\'if "some_key" in some_dict: some_dict["some_key"]\''
}