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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
|
# Copyright (c) 2014-2018, 2020 Claudiu Popa <pcmanticore@gmail.com>
# Copyright (c) 2014 Google, Inc.
# Copyright (c) 2014 LOGILAB S.A. (Paris, FRANCE) <contact@logilab.fr>
# Copyright (c) 2015 Ionel Cristian Maries <contact@ionelmc.ro>
# Copyright (c) 2016 Derek Gustafson <degustaf@gmail.com>
# Copyright (c) 2017 Ville Skyttä <ville.skytta@iki.fi>
# Copyright (c) 2018 Bryce Guinta <bryce.guinta@protonmail.com>
# Copyright (c) 2018 Bryce Guinta <bryce.paul.guinta@gmail.com>
# Copyright (c) 2018 mar-chi-pan <mar.polatoglou@gmail.com>
# Copyright (c) 2019-2020 Pierre Sassoulas <pierre.sassoulas@gmail.com>
# Copyright (c) 2019 Ashley Whetter <ashley@awhetter.co.uk>
# Copyright (c) 2020 Andrew Simmons <anjsimmo@gmail.com>
# Copyright (c) 2020 Andrew Simmons <a.simmons@deakin.edu.au>
# Copyright (c) 2020 Anthony Sottile <asottile@umich.edu>
# Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
# For details: https://github.com/PyCQA/pylint/blob/master/COPYING
import os
import re
import sys
from pathlib import Path
import astroid
from pylint.checkers import variables
from pylint.interfaces import UNDEFINED
from pylint.testutils import CheckerTestCase, Message, linter, set_config
REGR_DATA_DIR = str(Path(__file__).parent / ".." / "regrtest_data")
class TestVariablesChecker(CheckerTestCase):
CHECKER_CLASS = variables.VariablesChecker
def test_bitbucket_issue_78(self):
""" Issue 78 report a false positive for unused-module """
module = astroid.parse(
"""
from sys import path
path += ['stuff']
def func():
other = 1
return len(other)
"""
)
with self.assertNoMessages():
self.walk(module)
@set_config(ignored_modules=("argparse",))
def test_no_name_in_module_skipped(self):
"""Make sure that 'from ... import ...' does not emit a
'no-name-in-module' with a module that is configured
to be ignored.
"""
node = astroid.extract_node(
"""
from argparse import THIS_does_not_EXIST
"""
)
with self.assertNoMessages():
self.checker.visit_importfrom(node)
def test_all_elements_without_parent(self):
node = astroid.extract_node("__all__ = []")
node.value.elts.append(astroid.Const("test"))
root = node.root()
with self.assertNoMessages():
self.checker.visit_module(root)
self.checker.leave_module(root)
def test_redefined_builtin_ignored(self):
node = astroid.parse(
"""
from future.builtins import open
"""
)
with self.assertNoMessages():
self.checker.visit_module(node)
@set_config(redefining_builtins_modules=("os",))
def test_redefined_builtin_custom_modules(self):
node = astroid.parse(
"""
from os import open
"""
)
with self.assertNoMessages():
self.checker.visit_module(node)
@set_config(redefining_builtins_modules=("os",))
def test_redefined_builtin_modname_not_ignored(self):
node = astroid.parse(
"""
from future.builtins import open
"""
)
with self.assertAddsMessages(
Message("redefined-builtin", node=node.body[0], args="open")
):
self.checker.visit_module(node)
@set_config(redefining_builtins_modules=("os",))
def test_redefined_builtin_in_function(self):
node = astroid.extract_node(
"""
def test():
from os import open
"""
)
with self.assertNoMessages():
self.checker.visit_module(node.root())
self.checker.visit_functiondef(node)
def test_unassigned_global(self):
node = astroid.extract_node(
"""
def func():
global sys #@
import sys, lala
"""
)
msg = Message("global-statement", node=node, confidence=UNDEFINED)
with self.assertAddsMessages(msg):
self.checker.visit_global(node)
def test_listcomp_in_decorator(self):
"""Make sure class attributes in scope for listcomp in decorator.
https://github.com/PyCQA/pylint/issues/511
"""
module = astroid.parse(
"""
def dec(inp):
def inner(func):
print(inp)
return func
return inner
class Cls:
DATA = "foo"
@dec([x for x in DATA])
def fun(self):
pass
"""
)
with self.assertNoMessages():
self.walk(module)
def test_listcomp_in_ancestors(self):
"""Ensure list comprehensions in base classes are scoped correctly
https://github.com/PyCQA/pylint/issues/3434
"""
module = astroid.parse(
"""
import collections
l = ["a","b","c"]
class Foo(collections.namedtuple("Foo",[x+"_foo" for x in l])):
pass
"""
)
with self.assertNoMessages():
self.walk(module)
def test_return_type_annotation(self):
"""Make sure class attributes in scope for return type annotations.
https://github.com/PyCQA/pylint/issues/1976
"""
module = astroid.parse(
"""
class MyObject:
class MyType:
pass
def my_method(self) -> MyType:
pass
"""
)
with self.assertNoMessages():
self.walk(module)
class TestVariablesCheckerWithTearDown(CheckerTestCase):
CHECKER_CLASS = variables.VariablesChecker
def setup_method(self):
super().setup_method()
self._to_consume_backup = self.checker._to_consume
self.checker._to_consume = []
def teardown_method(self):
self.checker._to_consume = self._to_consume_backup
@set_config(callbacks=("callback_", "_callback"))
def test_custom_callback_string(self):
""" Test the --calbacks option works. """
node = astroid.extract_node(
"""
def callback_one(abc):
''' should not emit unused-argument. '''
"""
)
with self.assertNoMessages():
self.checker.visit_functiondef(node)
self.checker.leave_functiondef(node)
node = astroid.extract_node(
"""
def two_callback(abc, defg):
''' should not emit unused-argument. '''
"""
)
with self.assertNoMessages():
self.checker.visit_functiondef(node)
self.checker.leave_functiondef(node)
node = astroid.extract_node(
"""
def normal_func(abc):
''' should emit unused-argument. '''
"""
)
with self.assertAddsMessages(
Message("unused-argument", node=node["abc"], args="abc")
):
self.checker.visit_functiondef(node)
self.checker.leave_functiondef(node)
node = astroid.extract_node(
"""
def cb_func(abc):
''' Previous callbacks are overridden. '''
"""
)
with self.assertAddsMessages(
Message("unused-argument", node=node["abc"], args="abc")
):
self.checker.visit_functiondef(node)
self.checker.leave_functiondef(node)
@set_config(redefining_builtins_modules=("os",))
def test_redefined_builtin_modname_not_ignored(self):
node = astroid.parse(
"""
from future.builtins import open
"""
)
with self.assertAddsMessages(
Message("redefined-builtin", node=node.body[0], args="open")
):
self.checker.visit_module(node)
@set_config(redefining_builtins_modules=("os",))
def test_redefined_builtin_in_function(self):
node = astroid.extract_node(
"""
def test():
from os import open
"""
)
with self.assertNoMessages():
self.checker.visit_module(node.root())
self.checker.visit_functiondef(node)
def test_import_as_underscore(self):
node = astroid.parse(
"""
import math as _
"""
)
with self.assertNoMessages():
self.walk(node)
def test_lambda_in_classdef(self):
# Make sure lambda doesn't raises
# Undefined-method in class def
# Issue 1824
# https://github.com/PyCQA/pylint/issues/1824
node = astroid.parse(
"""
class MyObject(object):
method1 = lambda func: func()
method2 = lambda function: function()
"""
)
with self.assertNoMessages():
self.walk(node)
def test_nested_lambda(self):
"""Make sure variables from parent lambdas
aren't noted as undefined
https://github.com/PyCQA/pylint/issues/760
"""
node = astroid.parse(
"""
lambda x: lambda: x + 1
"""
)
with self.assertNoMessages():
self.walk(node)
@set_config(ignored_argument_names=re.compile("arg"))
def test_ignored_argument_names_no_message(self):
"""Make sure is_ignored_argument_names properly ignores
function arguments"""
node = astroid.parse(
"""
def fooby(arg):
pass
"""
)
with self.assertNoMessages():
self.walk(node)
@set_config(ignored_argument_names=re.compile("args|kwargs"))
def test_ignored_argument_names_starred_args(self):
node = astroid.parse(
"""
def fooby(*args, **kwargs):
pass
"""
)
with self.assertNoMessages():
self.walk(node)
class TestMissingSubmodule(CheckerTestCase):
CHECKER_CLASS = variables.VariablesChecker
@staticmethod
def test_package_all():
sys.path.insert(0, REGR_DATA_DIR)
try:
linter.check(os.path.join(REGR_DATA_DIR, "package_all"))
got = linter.reporter.finalize().strip()
assert got == "E: 3: Undefined variable name 'missing' in __all__"
finally:
sys.path.pop(0)
|