Skip to content

Commit

Permalink
Merge pull request #99 from gforcada/use-flake8-builtins
Browse files Browse the repository at this point in the history
Use flake8 builtins
  • Loading branch information
gforcada committed Dec 22, 2022
2 parents 1fe5f6e + e4c87b5 commit a7be720
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
4 changes: 2 additions & 2 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ Changelog
2.0.2 (unreleased)
------------------

- Nothing changed yet.

- Honor `--builtins` option from flake8 #73.
[gforcada]

2.0.1 (2022-11-01)
------------------
Expand Down
3 changes: 3 additions & 0 deletions flake8_builtins.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ def parse_options(cls, options):
cls.names = {
a[0] for a in inspect.getmembers(builtins) if a[0] not in cls.ignore_list
}
flake8_builtins = getattr(options, 'builtins', None)
if flake8_builtins:
cls.names.update(flake8_builtins)

def run(self):
tree = self.tree
Expand Down
17 changes: 14 additions & 3 deletions run_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,26 @@

class FakeOptions:
builtins_ignorelist = []
builtins = None

def __init__(self, ignore_list=''):
def __init__(self, ignore_list='', builtins=None):
if ignore_list:
self.builtins_ignorelist = ignore_list
if builtins:
self.builtins = builtins


def check_code(source, expected_codes=None, ignore_list=None):
def check_code(source, expected_codes=None, ignore_list=None, builtins=None):
"""Check if the given source code generates the given flake8 errors
If `expected_codes` is a string is converted to a list,
if it is not given, then it is expected to **not** generate any error.
If `ignore_list` is provided, it should be a list of names
that will be ignored if found, as if they were a builtin.
If `builtins` is provided, it should be a list of names
that will be reported if found, as if they were a builtin.
"""
if isinstance(expected_codes, str):
expected_codes = [expected_codes]
Expand All @@ -33,7 +39,7 @@ def check_code(source, expected_codes=None, ignore_list=None):
ignore_list = []
tree = ast.parse(textwrap.dedent(source))
checker = BuiltinsChecker(tree, '/home/script.py')
checker.parse_options(FakeOptions(ignore_list=ignore_list))
checker.parse_options(FakeOptions(ignore_list=ignore_list, builtins=builtins))
return_statements = list(checker.run())

assert len(return_statements) == len(expected_codes)
Expand Down Expand Up @@ -212,6 +218,11 @@ def test_custom_ignored_names():
check_code(source, ignore_list=('copyright',))


def test_flake8_builtins():
source = 'consider_this_builtin = 4'
check_code(source, ['A001'], builtins=('consider_this_builtin',))


def test_for_loop_variable():
source = """
for format in (1, 2, 3):
Expand Down

0 comments on commit a7be720

Please sign in to comment.