Skip to content

Commit 6ed369d

Browse files
authored
Merge pull request #283 from pre-commit/breakpoint
debug-statements: detect python3.7+ breakpoint()
2 parents 904a061 + 18b3ab7 commit 6ed369d

File tree

4 files changed

+64
-98
lines changed

4 files changed

+64
-98
lines changed

pre_commit_hooks/debug_statement_hook.py

Lines changed: 31 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -8,32 +8,33 @@
88

99

1010
DEBUG_STATEMENTS = {'pdb', 'ipdb', 'pudb', 'q', 'rdb'}
11+
Debug = collections.namedtuple('Debug', ('line', 'col', 'name', 'reason'))
1112

1213

13-
DebugStatement = collections.namedtuple(
14-
'DebugStatement', ['name', 'line', 'col'],
15-
)
16-
17-
18-
class ImportStatementParser(ast.NodeVisitor):
14+
class DebugStatementParser(ast.NodeVisitor):
1915
def __init__(self):
20-
self.debug_import_statements = []
16+
self.breakpoints = []
2117

2218
def visit_Import(self, node):
23-
for node_name in node.names:
24-
if node_name.name in DEBUG_STATEMENTS:
25-
self.debug_import_statements.append(
26-
DebugStatement(node_name.name, node.lineno, node.col_offset),
27-
)
19+
for name in node.names:
20+
if name.name in DEBUG_STATEMENTS:
21+
st = Debug(node.lineno, node.col_offset, name.name, 'imported')
22+
self.breakpoints.append(st)
2823

2924
def visit_ImportFrom(self, node):
3025
if node.module in DEBUG_STATEMENTS:
31-
self.debug_import_statements.append(
32-
DebugStatement(node.module, node.lineno, node.col_offset),
33-
)
26+
st = Debug(node.lineno, node.col_offset, node.module, 'imported')
27+
self.breakpoints.append(st)
3428

29+
def visit_Call(self, node):
30+
"""python3.7+ breakpoint()"""
31+
if isinstance(node.func, ast.Name) and node.func.id == 'breakpoint':
32+
st = Debug(node.lineno, node.col_offset, node.func.id, 'called')
33+
self.breakpoints.append(st)
34+
self.generic_visit(node)
3535

36-
def check_file_for_debug_statements(filename):
36+
37+
def check_file(filename):
3738
try:
3839
ast_obj = ast.parse(open(filename, 'rb').read(), filename=filename)
3940
except SyntaxError:
@@ -42,34 +43,30 @@ def check_file_for_debug_statements(filename):
4243
print('\t' + traceback.format_exc().replace('\n', '\n\t'))
4344
print()
4445
return 1
45-
visitor = ImportStatementParser()
46+
47+
visitor = DebugStatementParser()
4648
visitor.visit(ast_obj)
47-
if visitor.debug_import_statements:
48-
for debug_statement in visitor.debug_import_statements:
49-
print(
50-
'{}:{}:{} - {} imported'.format(
51-
filename,
52-
debug_statement.line,
53-
debug_statement.col,
54-
debug_statement.name,
55-
),
56-
)
57-
return 1
58-
else:
59-
return 0
6049

50+
for bp in visitor.breakpoints:
51+
print(
52+
'{}:{}:{} - {} {}'.format(
53+
filename, bp.line, bp.col, bp.name, bp.reason,
54+
),
55+
)
56+
57+
return int(bool(visitor.breakpoints))
6158

62-
def debug_statement_hook(argv=None):
59+
60+
def main(argv=None):
6361
parser = argparse.ArgumentParser()
6462
parser.add_argument('filenames', nargs='*', help='Filenames to run')
6563
args = parser.parse_args(argv)
6664

6765
retv = 0
6866
for filename in args.filenames:
69-
retv |= check_file_for_debug_statements(filename)
70-
67+
retv |= check_file(filename)
7168
return retv
7269

7370

7471
if __name__ == '__main__':
75-
exit(debug_statement_hook())
72+
exit(main())

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
'check-vcs-permalinks = pre_commit_hooks.check_vcs_permalinks:main',
4747
'check-xml = pre_commit_hooks.check_xml:check_xml',
4848
'check-yaml = pre_commit_hooks.check_yaml:check_yaml',
49-
'debug-statement-hook = pre_commit_hooks.debug_statement_hook:debug_statement_hook',
49+
'debug-statement-hook = pre_commit_hooks.debug_statement_hook:main',
5050
'detect-aws-credentials = pre_commit_hooks.detect_aws_credentials:main',
5151
'detect-private-key = pre_commit_hooks.detect_private_key:detect_private_key',
5252
'double-quote-string-fixer = pre_commit_hooks.string_fixer:main',

testing/resources/file_with_debug.notpy

Lines changed: 0 additions & 5 deletions
This file was deleted.

tests/debug_statement_hook_test.py

Lines changed: 32 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -4,86 +4,60 @@
44

55
import ast
66

7-
import pytest
8-
9-
from pre_commit_hooks.debug_statement_hook import debug_statement_hook
10-
from pre_commit_hooks.debug_statement_hook import DebugStatement
11-
from pre_commit_hooks.debug_statement_hook import ImportStatementParser
7+
from pre_commit_hooks.debug_statement_hook import Debug
8+
from pre_commit_hooks.debug_statement_hook import DebugStatementParser
9+
from pre_commit_hooks.debug_statement_hook import main
1210
from testing.util import get_resource_path
1311

1412

15-
@pytest.fixture
16-
def ast_with_no_debug_imports():
17-
return ast.parse(
18-
"""
19-
import foo
20-
import bar
21-
import baz
22-
from foo import bar
23-
""",
24-
)
25-
26-
27-
@pytest.fixture
28-
def ast_with_debug_import_form_1():
29-
return ast.parse(
30-
"""
31-
32-
import ipdb; ipdb.set_trace()
33-
34-
""",
35-
)
36-
37-
38-
@pytest.fixture
39-
def ast_with_debug_import_form_2():
40-
return ast.parse(
41-
"""
13+
def test_no_breakpoints():
14+
visitor = DebugStatementParser()
15+
visitor.visit(ast.parse('import os\nfrom foo import bar\n'))
16+
assert visitor.breakpoints == []
4217

43-
from pudb import set_trace; set_trace()
4418

45-
""",
46-
)
19+
def test_finds_debug_import_attribute_access():
20+
visitor = DebugStatementParser()
21+
visitor.visit(ast.parse('import ipdb; ipdb.set_trace()'))
22+
assert visitor.breakpoints == [Debug(1, 0, 'ipdb', 'imported')]
4723

4824

49-
def test_returns_no_debug_statements(ast_with_no_debug_imports):
50-
visitor = ImportStatementParser()
51-
visitor.visit(ast_with_no_debug_imports)
52-
assert visitor.debug_import_statements == []
25+
def test_finds_debug_import_from_import():
26+
visitor = DebugStatementParser()
27+
visitor.visit(ast.parse('from pudb import set_trace; set_trace()'))
28+
assert visitor.breakpoints == [Debug(1, 0, 'pudb', 'imported')]
5329

5430

55-
def test_returns_one_form_1(ast_with_debug_import_form_1):
56-
visitor = ImportStatementParser()
57-
visitor.visit(ast_with_debug_import_form_1)
58-
assert visitor.debug_import_statements == [
59-
DebugStatement('ipdb', 3, 0),
60-
]
31+
def test_finds_breakpoint():
32+
visitor = DebugStatementParser()
33+
visitor.visit(ast.parse('breakpoint()'))
34+
assert visitor.breakpoints == [Debug(1, 0, 'breakpoint', 'called')]
6135

6236

63-
def test_returns_one_form_2(ast_with_debug_import_form_2):
64-
visitor = ImportStatementParser()
65-
visitor.visit(ast_with_debug_import_form_2)
66-
assert visitor.debug_import_statements == [
67-
DebugStatement('pudb', 3, 0),
68-
]
69-
70-
71-
def test_returns_one_for_failing_file():
72-
ret = debug_statement_hook([get_resource_path('file_with_debug.notpy')])
37+
def test_returns_one_for_failing_file(tmpdir):
38+
f_py = tmpdir.join('f.py')
39+
f_py.write('def f():\n import pdb; pdb.set_trace()')
40+
ret = main([f_py.strpath])
7341
assert ret == 1
7442

7543

7644
def test_returns_zero_for_passing_file():
77-
ret = debug_statement_hook([__file__])
45+
ret = main([__file__])
7846
assert ret == 0
7947

8048

8149
def test_syntaxerror_file():
82-
ret = debug_statement_hook([get_resource_path('cannot_parse_ast.notpy')])
50+
ret = main([get_resource_path('cannot_parse_ast.notpy')])
8351
assert ret == 1
8452

8553

8654
def test_non_utf8_file(tmpdir):
8755
f_py = tmpdir.join('f.py')
8856
f_py.write_binary('# -*- coding: cp1252 -*-\nx = "€"\n'.encode('cp1252'))
89-
assert debug_statement_hook((f_py.strpath,)) == 0
57+
assert main((f_py.strpath,)) == 0
58+
59+
60+
def test_py37_breakpoint(tmpdir):
61+
f_py = tmpdir.join('f.py')
62+
f_py.write('def f():\n breakpoint()\n')
63+
assert main((f_py.strpath,)) == 1

0 commit comments

Comments
 (0)