/
test_900_rules.py
97 lines (84 loc) · 2.57 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
# 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",
(
"""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]'"
}