mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2026-03-30 18:26:53 +00:00
fix debug statement hook to catch imports using a dundle method
This commit is contained in:
parent
31903eabdb
commit
c5a939b530
2 changed files with 11 additions and 0 deletions
|
|
@ -44,6 +44,11 @@ class DebugStatementParser(ast.NodeVisitor):
|
|||
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')
|
||||
|
|
|
|||
|
|
@ -26,6 +26,12 @@ def test_finds_debug_import_from_import():
|
|||
assert visitor.breakpoints == [Debug(1, 0, 'pudb', 'imported')]
|
||||
|
||||
|
||||
def test_finds_debug_import_when_using_dunder_import():
|
||||
visitor = DebugStatementParser()
|
||||
visitor.visit(ast.parse('__import__("pdb").set_trace()'))
|
||||
assert visitor.breakpoints == [Debug(1, 0, 'pdb', 'imported')]
|
||||
|
||||
|
||||
def test_finds_breakpoint():
|
||||
visitor = DebugStatementParser()
|
||||
visitor.visit(ast.parse('breakpoint()'))
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue