File: test_noqa.py

package info (click to toggle)
vulture 2.14-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 464 kB
  • sloc: python: 3,254; makefile: 12
file content (311 lines) | stat: -rw-r--r-- 6,361 bytes parent folder | download
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
import pytest

from vulture.core import ERROR_CODES
from vulture.noqa import NOQA_CODE_MAP, NOQA_REGEXP, _parse_error_codes

from . import check, v

assert v  # Silence pyflakes.


@pytest.mark.parametrize(
    "line, codes",
    [
        ("# noqa", ["all"]),
        ("## noqa", ["all"]),
        ("# noqa Hi, go on.", ["all"]),
        ("# noqa: V101", ["V101"]),
        ("# noqa: V101, V106", ["V101", "V106"]),
        ("# NoQA: V101,      V103, \t V104", ["V101", "V103", "V104"]),
    ],
)
def test_noqa_regex_present(line, codes):
    match = NOQA_REGEXP.search(line)
    parsed = _parse_error_codes(match)
    assert parsed == codes


@pytest.mark.parametrize(
    "line",
    [
        ("# noqa: 123V"),
        ("# noqa explanation: V012"),
        ("# noqa: ,V101"),
        ("# noqa: #noqa: V102"),
        ("# noqa: # noqa: V102"),
    ],
)
def test_noqa_regex_no_groups(line):
    assert NOQA_REGEXP.search(line).groupdict()["codes"] is None


@pytest.mark.parametrize(
    "line",
    [("#noqa"), ("##noqa"), ("# n o q a"), ("#NOQA"), ("# Hello, noqa")],
)
def test_noqa_regex_not_present(line):
    assert not NOQA_REGEXP.search(line)


def test_noqa_without_codes(v):
    v.scan(
        """\
import this  # noqa

@underground  # noqa
class Cellar:
    @property  # noqa
    def wine(self):
        grapes = True  # noqa

    @without_ice  # noqa
    def serve(self, quantity=50):
        self.quantity_served = quantity  # noqa
        return
        self.pour()  # noqa
"""
    )
    check(v.unused_attrs, [])
    check(v.unused_classes, [])
    check(v.unused_funcs, [])
    check(v.unused_imports, [])
    check(v.unused_props, [])
    check(v.unreachable_code, [])
    check(v.unused_vars, [])


def test_noqa_specific_issue_codes(v):
    v.scan(
        """\
import this  # noqa: V104

@underground  # noqa: V102
class Cellar:
    @property  # noqa: V106
    def wine(self):
        grapes = True  # noqa: V107

    @without_ice  # noqa: V103
    def serve(self, quantity=50):
        self.quantity_served = quantity  # noqa: V101
        return
        self.pour()  # noqa: V201
"""
    )
    check(v.unused_attrs, [])
    check(v.unused_classes, [])
    check(v.unused_funcs, [])
    check(v.unused_imports, [])
    check(v.unused_methods, ["serve"])
    check(v.unused_props, [])
    check(v.unreachable_code, [])
    check(v.unused_vars, [])


def test_noqa_attributes(v):
    v.scan(
        """\
something.x = 'x'  # noqa: V101
something.z = 'z'  # noqa: V107 (code for unused variable)
something.u = 'u'  # noqa
"""
    )
    check(v.unused_attrs, ["z"])


def test_noqa_classes(v):
    v.scan(
        """\
class QtWidget:  # noqa: V102
    pass

class ABC(QtWidget):
    pass  # noqa: V102 (should not ignore)

class DEF:  # noqa
    pass
"""
    )
    check(v.unused_classes, ["ABC"])


def test_noqa_functions(v):
    v.scan(
        """\
def play(tune, instrument='bongs', _hz='50'):  # noqa: V103
    pass


# noqa
def problems():  # noqa: V104
    pass  # noqa: V103

def hello(name):  # noqa
    print("Hello")
"""
    )
    check(v.unused_funcs, ["problems"])
    check(v.unused_vars, ["instrument", "tune"])


def test_noqa_imports(v):
    v.scan(
        """\
import foo
import this  # noqa: V104
import zoo
from koo import boo  # noqa
from me import *
import dis  # noqa: V101 (code for unused attr)
"""
    )
    check(v.unused_imports, ["foo", "zoo", "dis"])


def test_noqa_properties(v):
    v.scan(
        """\
class Zoo:
    @property
    def no_of_koalas(self):  # noqa
        pass

    @property
    def area(self, width, depth):  # noqa: V105
        pass

    @property  # noqa
    def entry_gates(self):
        pass

    @property  # noqa: V103 (code for unused function)
    def tickets(self):
        pass
"""
    )
    check(v.unused_props, ["no_of_koalas", "area", "tickets"])
    check(v.unused_classes, ["Zoo"])
    check(v.unused_vars, ["width", "depth"])


def test_noqa_multiple_decorators(v):
    v.scan(
        """\
@bar  # noqa: V102
class Foo:
    @property  # noqa: V106
    @make_it_cool
    @log
    def something(self):
        pass

    @coolify
    @property
    def something_else(self):  # noqa: V106
        pass

    @a
    @property
    @b  # noqa
    def abcd(self):
        pass
"""
    )
    check(v.unused_props, ["something_else", "abcd"])
    check(v.unused_classes, [])


def test_noqa_unreacahble_code(v):
    v.scan(
        """\
def shave_sheep(sheep):
    for a_sheep in sheep:
        if a_sheep.is_bald:
            continue
            a_sheep.grow_hair()  # noqa: V201
        a_sheep.shave()
    return
    for a_sheep in sheep:  # noqa: V201
        if a_sheep.still_has_hair:
            a_sheep.shave_again()
"""
    )
    check(v.unreachable_code, [])
    check(v.unused_funcs, ["shave_sheep"])


def test_noqa_variables(v):
    v.scan(
        """\
mitsi = "Mother"  # noqa: V107
harry = "Father"  # noqa
shero = "doggy"   # noqa: V101, V104 (code for unused import, attr)
shinchan.friend = ['masao']  # noqa: V107 (code for unused variable)
"""
    )
    check(v.unused_vars, ["shero"])
    check(v.unused_attrs, ["friend"])


def test_noqa_with_multiple_issue_codes(v):
    v.scan(
        """\
def world(axis):  # noqa: V103, V201
    pass


for _ in range(3):
    continue
    xyz = hello(something, else):  # noqa: V201, V107
"""
    )
    check(v.get_unused_code(), [])


def test_noqa_on_empty_line(v):
    v.scan(
        """\
# noqa
import this
# noqa
"""
    )
    check(v.unused_imports, ["this"])


def test_noqa_with_invalid_codes(v):
    v.scan(
        """\
import this  # V098, A123, F876
"""
    )
    check(v.unused_imports, ["this"])


@pytest.mark.parametrize(
    "first_file, second_file",
    [
        ("foo = None", "bar = None  # noqa"),
        ("bar = None  # noqa", "foo = None"),
    ],
)
def test_noqa_multiple_files(first_file, second_file, v):
    v.scan(first_file, filename="first_file.py")
    v.scan(second_file, filename="second_file.py")
    check(v.unused_vars, ["foo"])


def test_flake8_noqa_codes(v):
    assert NOQA_CODE_MAP["F401"] == ERROR_CODES["import"]
    assert NOQA_CODE_MAP["F841"] == ERROR_CODES["variable"]
    v.scan(
        """\
import this  # noqa: F401

def foo():
    bar = 2  # noqa: F841
"""
    )
    check(v.unused_funcs, ["foo"])
    check(v.unused_imports, [])
    check(v.unused_vars, [])