Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion pre_commit_hooks/debug_statement_hook.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@
'wdb',
}

DEBUG_CALL_STATEMENTS = {
'breakpoint',
'print',
}


class Debug(NamedTuple):
line: int
Expand All @@ -45,7 +50,10 @@ def visit_ImportFrom(self, node: ast.ImportFrom) -> None:

def visit_Call(self, node: ast.Call) -> None:
"""python3.7+ breakpoint()"""
if isinstance(node.func, ast.Name) and node.func.id == 'breakpoint':
if (
isinstance(node.func, ast.Name) and
node.func.id in DEBUG_CALL_STATEMENTS
):
st = Debug(node.lineno, node.col_offset, node.func.id, 'called')
self.breakpoints.append(st)
self.generic_visit(node)
Expand Down
6 changes: 6 additions & 0 deletions tests/debug_statement_hook_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ def test_finds_breakpoint():
assert visitor.breakpoints == [Debug(1, 0, 'breakpoint', 'called')]


def test_finds_print():
visitor = DebugStatementParser()
visitor.visit(ast.parse('print()'))
assert visitor.breakpoints == [Debug(1, 0, 'print', 'called')]


def test_returns_one_for_failing_file(tmpdir):
f_py = tmpdir.join('f.py')
f_py.write('def f():\n import pdb; pdb.set_trace()')
Expand Down