Skip to content

Commit c5a939b

Browse files
committed
fix debug statement hook to catch imports using a dundle method
1 parent 31903ea commit c5a939b

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

pre_commit_hooks/debug_statement_hook.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ def visit_ImportFrom(self, node: ast.ImportFrom) -> None:
4444
self.breakpoints.append(st)
4545

4646
def visit_Call(self, node: ast.Call) -> None:
47+
if isinstance(node, ast.Call) and len(node.args):
48+
if isinstance(node.args[0], ast.Constant) and node.args[0].value in DEBUG_STATEMENTS:
49+
st = Debug(node.lineno, node.col_offset, node.args[0].value, 'imported')
50+
self.breakpoints.append(st)
51+
4752
"""python3.7+ breakpoint()"""
4853
if isinstance(node.func, ast.Name) and node.func.id == 'breakpoint':
4954
st = Debug(node.lineno, node.col_offset, node.func.id, 'called')

tests/debug_statement_hook_test.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ def test_finds_debug_import_from_import():
2626
assert visitor.breakpoints == [Debug(1, 0, 'pudb', 'imported')]
2727

2828

29+
def test_finds_debug_import_when_using_dunder_import():
30+
visitor = DebugStatementParser()
31+
visitor.visit(ast.parse('__import__("pdb").set_trace()'))
32+
assert visitor.breakpoints == [Debug(1, 0, 'pdb', 'imported')]
33+
34+
2935
def test_finds_breakpoint():
3036
visitor = DebugStatementParser()
3137
visitor.visit(ast.parse('breakpoint()'))

0 commit comments

Comments
 (0)