Skip to content

Commit 12f60d6

Browse files
committed
Allow customizing the module denylist
1 parent 6e513a2 commit 12f60d6

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

pre_commit_hooks/debug_statement_hook.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,24 @@ def check_file(filename: str) -> int:
7474
def main(argv: Sequence[str] | None = None) -> int:
7575
parser = argparse.ArgumentParser()
7676
parser.add_argument('filenames', nargs='*', help='Filenames to run')
77+
parser.add_argument(
78+
'--forbid',
79+
type=str, action='append',
80+
help='Extra module name(s) to forbid',
81+
)
82+
parser.add_argument(
83+
'--allow',
84+
type=str,
85+
action='append',
86+
help='Extra module name(s) to allow',
87+
)
7788
args = parser.parse_args(argv)
7889

90+
for name in args.forbid or ():
91+
DEBUG_STATEMENTS.add(name)
92+
for name in args.allow or ():
93+
DEBUG_STATEMENTS.discard(name)
94+
7995
retv = 0
8096
for filename in args.filenames:
8197
retv |= check_file(filename)

tests/debug_statement_hook_test.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,20 @@ def test_finds_breakpoint():
3232
assert visitor.breakpoints == [Debug(1, 0, 'breakpoint', 'called')]
3333

3434

35+
def test_allow(tmpdir):
36+
f_py = tmpdir.join('f.py')
37+
f_py.write('import q')
38+
ret = main([str(f_py), '--allow', 'q'])
39+
assert ret == 0
40+
41+
42+
def test_forbid(tmpdir):
43+
f_py = tmpdir.join('f.py')
44+
f_py.write('import foo')
45+
ret = main([str(f_py), '--forbid', 'foo'])
46+
assert ret == 1
47+
48+
3549
def test_returns_one_for_failing_file(tmpdir):
3650
f_py = tmpdir.join('f.py')
3751
f_py.write('def f():\n import pdb; pdb.set_trace()')

0 commit comments

Comments
 (0)