10
10
11
11
class FakeOptions :
12
12
builtins_ignorelist = []
13
+ builtins = None
13
14
14
- def __init__ (self , ignore_list = '' ):
15
+ def __init__ (self , ignore_list = '' , builtins = None ):
15
16
if ignore_list :
16
17
self .builtins_ignorelist = ignore_list
18
+ if builtins :
19
+ self .builtins = builtins
17
20
18
21
19
- def check_code (source , expected_codes = None , ignore_list = None ):
22
+ def check_code (source , expected_codes = None , ignore_list = None , builtins = None ):
20
23
"""Check if the given source code generates the given flake8 errors
21
24
22
25
If `expected_codes` is a string is converted to a list,
23
26
if it is not given, then it is expected to **not** generate any error.
24
27
25
28
If `ignore_list` is provided, it should be a list of names
26
29
that will be ignored if found, as if they were a builtin.
30
+
31
+ If `builtins` is provided, it should be a list of names
32
+ that will be reported if found, as if they were a builtin.
27
33
"""
28
34
if isinstance (expected_codes , str ):
29
35
expected_codes = [expected_codes ]
@@ -33,7 +39,7 @@ def check_code(source, expected_codes=None, ignore_list=None):
33
39
ignore_list = []
34
40
tree = ast .parse (textwrap .dedent (source ))
35
41
checker = BuiltinsChecker (tree , '/home/script.py' )
36
- checker .parse_options (FakeOptions (ignore_list = ignore_list ))
42
+ checker .parse_options (FakeOptions (ignore_list = ignore_list , builtins = builtins ))
37
43
return_statements = list (checker .run ())
38
44
39
45
assert len (return_statements ) == len (expected_codes )
@@ -212,6 +218,11 @@ def test_custom_ignored_names():
212
218
check_code (source , ignore_list = ('copyright' ,))
213
219
214
220
221
+ def test_flake8_builtins ():
222
+ source = 'consider_this_builtin = 4'
223
+ check_code (source , ['A001' ], builtins = ('consider_this_builtin' ,))
224
+
225
+
215
226
def test_for_loop_variable ():
216
227
source = """
217
228
for format in (1, 2, 3):
0 commit comments