forked from JBKahn/flake8-debugger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flake8_debugger.py
144 lines (122 loc) · 6.13 KB
/
flake8_debugger.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
"""Extension for flake8 that finds usage of the debugger."""
import ast
import sys
from itertools import chain
import pycodestyle
try:
from flake8.engine import pep8 as stdin_utils
except ImportError:
from flake8 import utils as stdin_utils
__version__ = "4.0.0"
DEBUGGER_ERROR_CODE = "T100"
debuggers = {
"pdb": ["set_trace"],
"pudb": ["set_trace"],
"ipdb": ["set_trace", "sset_trace"],
"IPython.terminal.embed": ["InteractiveShellEmbed"],
"IPython.frontend.terminal.embed": ["InteractiveShellEmbed"],
}
if sys.version_info >= (3, 7):
debuggers["builtins"] = ["breakpoint"]
class DebuggerFinder(ast.NodeVisitor):
def __init__(self, *args, **kwargs):
super(DebuggerFinder, self).__init__(*args, **kwargs)
self.debuggers_used = {}
self.debuggers_traces_redefined = {}
self.debuggers_traces_names = {}
self.debugger_traces_imported = {}
self.debuggers_names = {}
self.debuggers_redefined = {}
self.debuggers_imported = {}
def visit_Call(self, node):
if sys.version_info >= (3, 7) and getattr(node.func, "id", None) == "breakpoint":
entry = self.debuggers_used.setdefault((node.lineno, node.col_offset), [])
entry.append("{0} trace found: breakpoint used".format(DEBUGGER_ERROR_CODE))
debugger_method_names = list(chain(self.debuggers_traces_names.values(), *debuggers.values()))
is_debugger_function = getattr(node.func, "id", None) in debugger_method_names
if is_debugger_function:
if node.func.id in self.debuggers_traces_names.values():
debugger_method = next(
item[0] for item in self.debuggers_traces_names.items() if item[1] == node.func.id
)
entry = self.debuggers_used.setdefault((node.lineno, node.col_offset), [])
if debugger_method == node.func.id:
entry.append("{0} trace found: {1} used".format(DEBUGGER_ERROR_CODE, node.func.id))
else:
entry.append(
"{0} trace found: {1} used as {2}".format(DEBUGGER_ERROR_CODE, debugger_method, node.func.id)
)
is_debugger_attribute = getattr(node.func, "attr", None) in debugger_method_names
if is_debugger_attribute:
caller = getattr(node.func.value, "id", None)
entry = self.debuggers_used.setdefault((node.lineno, node.col_offset), [])
if caller in self.debuggers_names.values():
entry.append("{0} trace found: {1}.{2} used".format(DEBUGGER_ERROR_CODE, caller, node.func.attr))
else:
entry.append("{0} trace found: {1} used".format(DEBUGGER_ERROR_CODE, node.func.attr))
self.generic_visit(node)
def visit_Import(self, node):
for name_node in node.names:
if name_node.name in list(debuggers.keys()):
if name_node.asname is not None:
self.debuggers_names[name_node.name] = name_node.asname
entry = self.debuggers_redefined.setdefault((node.lineno, node.col_offset), [])
entry.append(
"{0} import for {1} found as {2}".format(DEBUGGER_ERROR_CODE, name_node.name, name_node.asname)
)
# Unlike the other imports, we don't want to consider all builtin imports as worthy of flagging.
elif name_node.name != "builtins":
self.debuggers_names[name_node.name] = name_node.name
entry = self.debuggers_imported.setdefault((node.lineno, node.col_offset), [])
entry.append("{0} import for {1} found".format(DEBUGGER_ERROR_CODE, name_node.name))
def visit_ImportFrom(self, node):
if node.module in list(debuggers.keys()):
for name_node in node.names:
if name_node.name in debuggers[node.module]:
if name_node.asname is not None:
self.debuggers_traces_names[name_node.name] = name_node.asname
entry = self.debuggers_traces_redefined.setdefault((node.lineno, node.col_offset), [])
entry.append(
"{0} import for {1} found as {2}".format(
DEBUGGER_ERROR_CODE, name_node.name, name_node.asname
)
)
else:
self.debuggers_traces_names[name_node.name] = name_node.name
entry = self.debugger_traces_imported.setdefault((node.lineno, node.col_offset), [])
entry.append("{0} import for {1} found".format(DEBUGGER_ERROR_CODE, name_node.name))
class DebuggerChecker(object):
options = None
name = "flake8-debugger"
version = __version__
def __init__(self, tree, filename):
self.tree = tree
self.filename = filename
self.lines = None
def load_file(self):
if self.filename in ("stdin", "-", None):
self.filename = "stdin"
self.lines = stdin_utils.stdin_get_value().splitlines(True)
else:
self.lines = pycodestyle.readlines(self.filename)
if not self.tree:
self.tree = ast.parse("".join(self.lines))
def run(self):
if not self.tree or not self.lines:
self.load_file()
parser = DebuggerFinder()
parser.visit(self.tree)
for error, messages in parser.debuggers_used.items():
if not pycodestyle.noqa(self.lines[error[0] - 1]):
for message in messages:
yield (error[0], error[1], message, DebuggerChecker)
for error, messages in chain(
parser.debuggers_traces_redefined.items(),
parser.debugger_traces_imported.items(),
parser.debuggers_redefined.items(),
parser.debuggers_imported.items(),
):
if error not in parser.debuggers_used:
if not pycodestyle.noqa(self.lines[error[0] - 1]):
for message in messages:
yield (error[0], error[1], message, DebuggerChecker)