Skip to content

Commit b3461fb

Browse files
authored
Merge pull request #86 from gforcada/builtins-whitelist
Builtins ignorelist
2 parents 552face + 2dff078 commit b3461fb

File tree

4 files changed

+52
-4
lines changed

4 files changed

+52
-4
lines changed

CHANGES.rst

+2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ Changelog
1818

1919
- Overhaul GitHub actions to test on actual supported python versions. [gforcada]
2020

21+
- New flake8 option `--builtins-ignorelist` to specify a list of builtins to ignore. [gsingh93]
22+
2123
1.5.3 (2020-05-14)
2224
------------------
2325

README.rst

+8
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,14 @@ Install with pip::
7373

7474
$ pip install flake8-builtins
7575

76+
Options
77+
-------
78+
79+
One can use `--builtins-ignorelist` option, or configuration option,
80+
to ignore a custom list of builtins::
81+
82+
$ flake8 --builtins-ignorelist id,copyright *.py
83+
7684
Requirements
7785
------------
7886
- Python 3.7, 3.8, 3.9

flake8_builtins.py

+22-2
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,16 @@
55

66
from flake8 import utils as stdin_utils
77

8-
WHITE_LIST = {
8+
IGNORE_LIST = {
99
'__name__',
1010
'__doc__',
1111
'credits',
1212
'_',
1313
}
1414

1515

16-
BUILTINS = [a[0] for a in inspect.getmembers(builtins) if a[0] not in WHITE_LIST]
16+
# Will be initialized in `BuiltinsChecker.parse_options`
17+
BUILTINS = None
1718

1819
if sys.version_info >= (3, 8):
1920
NamedExpr = ast.NamedExpr
@@ -32,6 +33,25 @@ def __init__(self, tree, filename):
3233
self.tree = tree
3334
self.filename = filename
3435

36+
def add_options(option_manager):
37+
option_manager.add_option(
38+
'--builtins-ignorelist',
39+
metavar='builtins',
40+
parse_from_config=True,
41+
comma_separated_list=True,
42+
help='A comma separated list of builtins to skip checking',
43+
)
44+
45+
def parse_options(option_manager, options, args):
46+
global BUILTINS
47+
48+
if options.builtins_ignorelist is not None:
49+
IGNORE_LIST.update(options.builtins_ignorelist)
50+
51+
BUILTINS = [
52+
a[0] for a in inspect.getmembers(builtins) if a[0] not in IGNORE_LIST
53+
]
54+
3555
def run(self):
3656
tree = self.tree
3757

run_tests.py

+20-2
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,33 @@
99
from flake8_builtins import BuiltinsChecker
1010

1111

12+
class FakeOptions:
13+
builtins_ignorelist = []
14+
15+
def __init__(self, ignore_list=''):
16+
if ignore_list:
17+
self.builtins_ignorelist = ignore_list
18+
19+
1220
class TestBuiltins(unittest.TestCase):
13-
def check_code(self, source, expected_codes=None):
21+
def check_code(self, source, expected_codes=None, ignore_list=None):
1422
"""Check if the given source code generates the given flake8 errors
1523
1624
If `expected_codes` is a string is converted to a list,
1725
if it is not given, then it is expected to **not** generate any error.
26+
27+
If `ignore_list` is provided, it should be a list of names
28+
that will be ignored if found, as if they were a builtin.
1829
"""
1930
if isinstance(expected_codes, str):
2031
expected_codes = [expected_codes]
2132
elif expected_codes is None:
2233
expected_codes = []
34+
if ignore_list is None:
35+
ignore_list = []
2336
tree = ast.parse(textwrap.dedent(source))
2437
checker = BuiltinsChecker(tree, '/home/script.py')
38+
checker.parse_options(FakeOptions(ignore_list=ignore_list), None)
2539
return_statements = list(checker.run())
2640

2741
self.assertEqual(
@@ -169,13 +183,17 @@ def bla():
169183
"""
170184
self.check_code(source, ['A001', 'A001'])
171185

172-
def test_ignore_whitelisted_names(self):
186+
def test_default_ignored_names(self):
173187
source = """
174188
class MyClass(object):
175189
__name__ = 4
176190
"""
177191
self.check_code(source)
178192

193+
def test_custom_ignored_names(self):
194+
source = 'copyright = 4'
195+
self.check_code(source, ignore_list=('copyright',))
196+
179197
def test_for_loop_variable(self):
180198
source = """
181199
for format in (1, 2, 3):

0 commit comments

Comments
 (0)