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
5 changes: 5 additions & 0 deletions pre_commit_hooks/debug_statement_hook.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ def visit_ImportFrom(self, node: ast.ImportFrom) -> None:
self.breakpoints.append(st)

def visit_Call(self, node: ast.Call) -> None:
if isinstance(node, ast.Call) and len(node.args):
if isinstance(node.args[0], ast.Constant) and node.args[0].value in DEBUG_STATEMENTS:
st = Debug(node.lineno, node.col_offset, node.args[0].value, 'imported')
self.breakpoints.append(st)

"""python3.7+ breakpoint()"""
if isinstance(node.func, ast.Name) and node.func.id == 'breakpoint':
st = Debug(node.lineno, node.col_offset, node.func.id, 'called')
Expand Down
22 changes: 22 additions & 0 deletions tests/debug_statement_hook_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import ast

import pytest

from pre_commit_hooks.debug_statement_hook import Debug
from pre_commit_hooks.debug_statement_hook import DebugStatementParser
from pre_commit_hooks.debug_statement_hook import main
Expand All @@ -26,6 +28,26 @@ def test_finds_debug_import_from_import():
assert visitor.breakpoints == [Debug(1, 0, 'pudb', 'imported')]


@pytest.mark.parametrize(
'debugger_module', (
'bpdb',
'ipdb',
'pdb',
'pdbr',
'pudb',
'pydevd_pycharm',
'q',
'rdb',
'rpdb',
'wdb',
),
)
def test_finds_debug_import_when_using_dunder_import(debugger_module):
visitor = DebugStatementParser()
visitor.visit(ast.parse(f'__import__("{debugger_module}").set_trace()'))
assert visitor.breakpoints == [Debug(1, 0, debugger_module, 'imported')]


def test_finds_breakpoint():
visitor = DebugStatementParser()
visitor.visit(ast.parse('breakpoint()'))
Expand Down