Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Delta Between Two Patch Sets: flake8-abp/flake8_abp.py

Issue 29340727: Noissue - Added flake8 extension accounting for our coding style and some other stuff (Closed)
Left Patch Set: Created April 21, 2016, 6:53 p.m.
Right Patch Set: Addressed comments, fixed two bugs, use ascii() Created May 9, 2016, 4:47 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « flake8-abp/README.md ('k') | flake8-abp/setup.py » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 # This file is part of Adblock Plus <https://adblockplus.org/>, 1 # This file is part of Adblock Plus <https://adblockplus.org/>,
2 # Copyright (C) 2006-2016 Eyeo GmbH 2 # Copyright (C) 2006-2016 Eyeo GmbH
3 # 3 #
4 # Adblock Plus is free software: you can redistribute it and/or modify 4 # Adblock Plus is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License version 3 as 5 # it under the terms of the GNU General Public License version 3 as
6 # published by the Free Software Foundation. 6 # published by the Free Software Foundation.
7 # 7 #
8 # Adblock Plus is distributed in the hope that it will be useful, 8 # Adblock Plus is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of 9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details. 11 # GNU General Public License for more details.
12 # 12 #
13 # You should have received a copy of the GNU General Public License 13 # You should have received a copy of the GNU General Public License
14 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. 14 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
15 15
16 import ast 16 import ast
17 import re 17 import re
18 import tokenize 18 import tokenize
19 import sys 19 import sys
20 20 import collections
21 __version__ = '0.1' 21
Sebastian Noack 2016/04/21 19:02:12 It would be kinda cool if we could inject the vers
22 22 try:
23 DEPRECATED_APIS = { 23 import builtins
24 ('re', 'match'): 'A101 use re.search() instead re.match()', 24 except ImportError:
25 ('codecs', 'open'): 'A102 use io.open() instead codecs.open()', 25 import __builtin__ as builtins
26
27 import pkg_resources
28
29 try:
30 ascii
31 except NameError:
32 ascii = repr
33
34 __version__ = pkg_resources.get_distribution('flake8-abp').version
35
36 DISCOURAGED_APIS = {
37 're.match': 're.search',
38 'codecs.open': 'io.open',
26 } 39 }
27 40
28 BAILOUT = (ast.Return, ast.Raise, ast.Continue, ast.Break) 41 ESSENTIAL_BUILTINS = set(dir(builtins)) - {'apply', 'buffer', 'coerce',
42 'intern', 'file'}
43
44 LEAVE_BLOCK = (ast.Return, ast.Raise, ast.Continue, ast.Break)
45 VOLATILE = object()
46
47
48 def evaluate(node):
49 try:
50 return eval(compile(ast.Expression(node), '', 'eval'), {})
51 except Exception:
52 return VOLATILE
53
54
55 def is_const(node):
56 return evaluate(node) is not VOLATILE
57
58
59 def get_identifier(node):
60 if isinstance(node, ast.Name):
61 return node.id
62 if isinstance(node, ast.Attribute) and isinstance(node.value, ast.Name):
63 return '{}.{}'.format(node.value.id, node.attr)
64
65
66 def get_statement(node):
67 return type(node).__name__.lower()
29 68
30 69
31 class TreeVisitor(ast.NodeVisitor): 70 class TreeVisitor(ast.NodeVisitor):
71 Scope = collections.namedtuple('Scope', ['node', 'names', 'globals'])
72
32 def __init__(self): 73 def __init__(self):
33 self.errors = [] 74 self.errors = []
34 self.stack = [] 75 self.scope_stack = []
35 76
36 def _visit_block(self, nodes, mandatory=False, docstring=False): 77 def _visit_block(self, nodes, block_required=False,
78 nodes_required=True, docstring=False,
79 can_have_unused_expr=False):
37 pass_node = None 80 pass_node = None
38 bailed = False 81 has_non_pass = False
82 leave_node = None
39 dead_code = False 83 dead_code = False
40 84
41 for node in nodes: 85 for i, node in enumerate(nodes):
42 if isinstance(node, ast.Pass): 86 if isinstance(node, ast.Pass):
43 pass_node = node 87 pass_node = node
44 88 else:
45 if bailed and not dead_code: 89 has_non_pass = True
90
91 if leave_node and not dead_code:
46 dead_code = True 92 dead_code = True
47 self.errors.append((node, 'A151 dead code after ' 93 statement = get_statement(leave_node)
48 'return/raise/continue/break')) 94 self.errors.append((node, 'A202 dead code after '
49 95 '{}'.format(statement)))
50 if isinstance(node, BAILOUT): 96
51 bailed = True 97 if isinstance(node, LEAVE_BLOCK):
52 98 leave_node = node
53 if not isinstance(node, ast.Expr): 99
100 if can_have_unused_expr or not isinstance(node, ast.Expr):
101 continue
102 if docstring and i == 0 and isinstance(node.value, ast.Str):
54 continue 103 continue
55 if isinstance(node.value, (ast.Call, ast.Yield)): 104 if isinstance(node.value, (ast.Call, ast.Yield)):
56 continue 105 continue
57 if docstring and node is nodes[0] and isinstance(node.value, ast.Str ): 106
58 continue 107 self.errors.append((node, 'A203 unused expression'))
59
60 self.errors.append((node, 'A152 unused expression'))
61 108
62 if pass_node: 109 if pass_node:
63 if len(nodes) > 1: 110 if not nodes_required or len(nodes) > 1:
64 self.errors.append((pass_node, 'A153 redundant pass statement')) 111 self.errors.append((pass_node, 'A204 redundant '
65 112 'pass statement'))
66 if not mandatory and all(isinstance(n, ast.Pass) for n in nodes): 113
67 self.errors.append((pass_node, 'A154 empty block')) 114 if not block_required and not has_non_pass:
68 115 self.errors.append((pass_node, 'A205 empty block'))
69 def _visit_block_node(self, node, **kwargs): 116
70 self._visit_block(node.body, **kwargs) 117 def _check_redundant_else(self, node, handlers, clause):
71 if hasattr(node, 'orelse'): 118 if not node.orelse:
72 self._visit_block(node.orelse) 119 return
73 if hasattr(node, 'finalbody'): 120
74 self._visit_block(node.finalbody) 121 for handler in handlers:
75 self.generic_visit(node) 122 for child in handler.body:
76 123 if isinstance(child, LEAVE_BLOCK):
77 visit_Try = visit_TryExcept = visit_TryFinally = _visit_block_node 124 leave_node = child
78 125 break
79 visit_ExceptHandler = visit_While = \ 126 else:
80 lambda self, node: self._visit_block_node(node, mandatory=True) 127 return
81 128
82 visit_Module = visit_ClassDef = \ 129 statement = get_statement(leave_node)
83 lambda self, node: self._visit_block_node(node, mandatory=True, 130 self.errors.append((node.orelse[0],
84 docstring=True) 131 'A206 Extraneous else statement after {} '
85 132 'in {}-clause'.format(statement, clause)))
86 def visit_Attribute(self, node): 133
87 if isinstance(node.ctx, ast.Load) and isinstance(node.value, ast.Name): 134 def visit_If(self, node):
88 error = DEPRECATED_APIS.get((node.value.id, node.attr)) 135 self._visit_block(node.body, block_required=bool(node.orelse))
89 if error: 136 self._visit_block(node.orelse)
90 self.errors.append((node, error)) 137 self._check_redundant_else(node, [node], 'if')
91 self.generic_visit(node) 138 self.generic_visit(node)
92 139
93 def visit_ImportFrom(self, node): 140 def visit_Try(self, node):
94 for alias in node.names: 141 self._visit_block(node.body, can_have_unused_expr=bool(node.handlers))
95 error = DEPRECATED_APIS.get((node.module, alias.name)) 142 self._visit_block(node.orelse)
96 if error: 143 self._visit_block(node.finalbody)
97 self.errors.append((node, error)) 144 self._check_redundant_else(node, node.handlers, 'except')
145 self.generic_visit(node)
146
147 def visit_TryExcept(self, node):
148 self._visit_block(node.body, can_have_unused_expr=True)
149 self._visit_block(node.orelse)
150 self._check_redundant_else(node, node.handlers, 'except')
151 self.generic_visit(node)
152
153 def visit_TryFinally(self, node):
154 self._visit_block(node.body)
155 self._visit_block(node.finalbody)
156 self.generic_visit(node)
157
158 def visit_ExceptHandler(self, node):
159 self._visit_block(node.body, block_required=True)
160 self.generic_visit(node)
161
162 def _visit_stored_name(self, node, name):
163 scope = self.scope_stack[-1]
164 scope.names.add(name)
165
166 if name in ESSENTIAL_BUILTINS and isinstance(scope.node,
167 ast.FunctionDef):
168 self.errors.append((node, 'A302 redefined built-in ' + name))
169
170 def visit_Name(self, node):
171 if isinstance(node.ctx, (ast.Store, ast.Param)):
172 self._visit_stored_name(node, node.id)
173
174 def visit_arg(self, node):
175 self._visit_stored_name(node, node.arg)
176
177 def _visit_with_scope(self, node):
178 scope = self.Scope(node, names=set(), globals=[])
179 self.scope_stack.append(scope)
180 self.generic_visit(node)
181 del self.scope_stack[-1]
182 return scope
183
184 def visit_Module(self, node):
185 self._visit_block(node.body, block_required=True,
186 nodes_required=False, docstring=True)
187 self._visit_with_scope(node)
188
189 def visit_FunctionDef(self, node):
190 self._visit_stored_name(node, node.name)
191 self._visit_block(node.body, block_required=True, docstring=True)
192
193 scope = self._visit_with_scope(node)
194 global_names = set()
195
196 for declaration in scope.globals:
197 for name in declaration.names:
198 if name not in scope.names or name in global_names:
199 statement = get_statement(declaration)
200 self.errors.append((declaration,
201 'A201 redundant {} declaration for '
202 '{}'.format(statement, name)))
203 else:
204 global_names.add(name)
205
206 visit_ClassDef = visit_FunctionDef
207
208 def visit_Global(self, node):
209 scope = self.scope_stack[-1]
210 scope.globals.append(node)
211
212 if isinstance(scope.node, ast.Module):
213 statement = get_statement(node)
214 self.errors.append((node, 'A201 {} declaration on '
215 'top-level'.format(statement)))
216
217 visit_Nonlocal = visit_Global
218
219 def _visit_iter(self, node):
220 if isinstance(node, (ast.Tuple, ast.Set)):
221 self.errors.append((node, 'A101 use lists for data '
222 'that have order'))
223
224 def visit_comprehension(self, node):
225 self._visit_iter(node.iter)
226 self.generic_visit(node)
227
228 def visit_For(self, node):
229 self._visit_iter(node.iter)
230 self._visit_block(node.body, block_required=True)
231 self._visit_block(node.orelse)
232 self.generic_visit(node)
233
234 def visit_While(self, node):
235 self._visit_block(node.body, block_required=True)
236 self._visit_block(node.orelse)
237 self.generic_visit(node)
98 238
99 def visit_BinOp(self, node): 239 def visit_BinOp(self, node):
100 if isinstance(node.op, ast.Mod) and isinstance(node.left, ast.Str): 240 if isinstance(node.op, ast.Mod) and isinstance(node.left, ast.Str):
101 self.errors.append((node, 'A111 use format() instead % operator ' 241 self.errors.append((node, 'A107 use format() instead of '
102 'for string formatting')) 242 '% operator for string formatting'))
103 243
104 multi_addition = (isinstance(node.op, ast.Add) and 244 multi_addition = (isinstance(node.op, ast.Add) and
105 isinstance(node.left, ast.BinOp) and 245 isinstance(node.left, ast.BinOp) and
106 isinstance(node.left.op, ast.Add)) 246 isinstance(node.left.op, ast.Add))
107 if multi_addition and (isinstance(node.left.left, ast.Str) or 247 if multi_addition and (isinstance(node.left.left, ast.Str) or
108 isinstance(node.left.right, ast.Str) or 248 isinstance(node.left.right, ast.Str) or
109 isinstance(node.right, ast.Str)): 249 isinstance(node.right, ast.Str)):
110 self.errors.append((node, 'A112 use format() instead + operator ' 250 self.errors.append((node, 'A108 use format() instead of '
111 'when concatenating >2 strings')) 251 '+ operator when concatenating '
112 252 'more than two strings'))
113 self.generic_visit(node) 253
114 254 self.generic_visit(node)
115 def visit_comprehension(self, node): 255
116 if isinstance(node.iter, (ast.Tuple, ast.Set, ast.Dict)): 256 def visit_Compare(self, node):
117 self.errors.append((node.iter, 'A121 use lists for data ' 257 left = node.left
118 'that have order')) 258 single = len(node.ops) == 1
119 self.generic_visit(node) 259
120 260 for op, right in zip(node.ops, node.comparators):
121 def visit_For(self, node): 261 membership = isinstance(op, (ast.In, ast.NotIn))
122 self._visit_block(node.body, mandatory=True) 262 symmetric = isinstance(op, (ast.Eq, ast.NotEq, ast.Is, ast.IsNot))
123 self.visit_comprehension(node) 263
264 if membership and isinstance(right, (ast.Tuple, ast.List)):
265 self.errors.append((right, 'A102 use sets for distinct '
266 'unordered data'))
267
268 consts_first = single and not membership or symmetric
269 if consts_first and is_const(left) and not is_const(right):
270 self.errors.append((left, 'A103 yoda condition'))
271
272 left = right
273
274 self.generic_visit(node)
275
276 def _check_deprecated(self, node, name):
277 substitute = DISCOURAGED_APIS.get(name)
278 if substitute:
279 self.errors.append((node, 'A301 use {}() instead of '
280 '{}()'.format(substitute, name)))
124 281
125 def visit_Call(self, node): 282 def visit_Call(self, node):
126 func = node.func 283 func = get_identifier(node.func)
127 if isinstance(func, ast.Name) and func.id in {'filter', 'map'}: 284 arg = next(iter(node.args), None)
128 if len(node.args) > 0 and isinstance(node.args[0], ast.Lambda): 285 redundant_literal = False
129 self.errors.append((node, 'A131 use a comprehension ' 286
130 'instead calling {}() with ' 287 if isinstance(arg, ast.Lambda) and func in {'map', 'filter',
131 'lambda function'.format(func.id))) 288 'imap', 'ifilter',
132 self.generic_visit(node) 289 'itertools.imap',
133 290 'itertools.ifilter'}:
134 def visit_FunctionDef(self, node): 291 self.errors.append((node, 'A104 use a comprehension '
135 self._visit_block(node.body, mandatory=True, docstring=True) 292 'instead of calling {}() with '
136 293 'lambda function'.format(func)))
137 self.stack.append((set(), [])) 294 elif isinstance(arg, (ast.List, ast.Tuple)):
138 self.generic_visit(node) 295 if func == 'dict':
139 targets, globals = self.stack.pop() 296 redundant_literal = all(isinstance(elt, (ast.Tuple, ast.List))
140 297 for elt in arg.elts)
141 for var in globals: 298 else:
142 if any(name not in targets for name in var.names): 299 redundant_literal = func in {'list', 'set', 'tuple'}
143 self.errors.append((var, 'A141 redundant global/nonlocal ' 300 elif isinstance(arg, (ast.ListComp, ast.GeneratorExp)):
144 'declaration')) 301 if func == 'dict':
145 302 redundant_literal = isinstance(arg.elt, (ast.Tuple, ast.List))
146 def visit_Name(self, node): 303 else:
147 if self.stack and isinstance(node.ctx, ast.Store): 304 redundant_literal = func in {'list', 'set'}
148 self.stack[-1][0].add(node.id) 305
149 306 if redundant_literal:
150 def visit_Global(self, node): 307 self.errors.append((node, 'A105 use a {0} literal or '
151 if self.stack: 308 'comprehension instead of calling '
152 self.stack[-1][1].append(node) 309 '{0}()'.format(func)))
153 else: 310
154 self.errors.append((node, 'A141 global/nonlocal declaration ' 311 self._check_deprecated(node, func)
155 'on top-level')) 312 self.generic_visit(node)
156 313
157 visit_Nonlocal = visit_Global 314 def visit_Import(self, node):
158 315 for alias in node.names:
159 def visit_If(self, node): 316 self._visit_stored_name(node, alias.asname or alias.name)
160 has_else = bool(node.orelse) 317
161 318 if hasattr(node, 'module'):
162 if has_else and any(isinstance(m, BAILOUT) for n in node.body): 319 self._check_deprecated(node, '{}.{}'.format(node.module,
163 self.errors.append((node, 'A159 redundant else statement after ' 320 alias.name))
164 'return/raise/continue/break ' 321
165 'in if-clause')) 322 visit_ImportFrom = visit_Import
166 323
167 self._visit_block(node.body, mandatory=has_else) 324 def visit_Assign(self, node):
168 self._visit_block(node.orelse) 325 if isinstance(node.value, ast.BinOp) and len(node.targets) == 1:
169 self.generic_visit(node) 326 target = node.targets[0]
327 left_is_target = (isinstance(target, ast.Name) and
328 isinstance(node.value.left, ast.Name) and
329 target.id == node.value.left.id)
330 if left_is_target:
331 self.errors.append((node, 'A106 use augment assignment, '
332 'e.g. x += y instead x = x + y'))
333 self.generic_visit(node)
334
335 def _visit_hash_keys(self, nodes, what):
336 keys = []
337 for node in nodes:
338 key = evaluate(node)
339 if key is VOLATILE:
340 continue
341
342 if key in keys:
343 self.errors.append((node, 'A207 duplicate ' + what))
344 continue
345
346 keys.append(key)
347
348 def visit_Dict(self, node):
349 self._visit_hash_keys(node.keys, 'key in dict')
350
351 def visit_Set(self, node):
352 self._visit_hash_keys(node.elts, 'item in set')
170 353
171 354
172 class ASTChecker(object): 355 class ASTChecker(object):
173 name = 'abp' 356 name = 'abp'
174 version = __version__ 357 version = __version__
175 358
176 def __init__(self, tree, filename): 359 def __init__(self, tree, filename):
177 self.tree = tree 360 self.tree = tree
178 361
179 def run(self): 362 def run(self):
180 visitor = TreeVisitor() 363 visitor = TreeVisitor()
181 visitor.visit(self.tree) 364 visitor.visit(self.tree)
182 365
183 for node, error in visitor.errors: 366 for node, error in visitor.errors:
184 yield (node.lineno, node.col_offset, error, type(self)) 367 yield (node.lineno, node.col_offset, error, type(self))
185 368
186 369
187 def check_non_default_encoding(physical_line, line_number): 370 def check_non_default_encoding(physical_line, line_number):
188 if (line_number <= 2 and re.search(r'^\s*#.*coding[:=]', physical_line)): 371 if line_number <= 2 and re.search(r'^\s*#.*coding[:=]', physical_line):
189 return (0, 'A201 non-default file encoding') 372 return (0, 'A303 non-default file encoding')
190 373
191 check_non_default_encoding.name = 'abp-non-default-encoding' 374 check_non_default_encoding.name = 'abp-non-default-encoding'
192 check_non_default_encoding.version = __version__ 375 check_non_default_encoding.version = __version__
193 376
194 377
195 def check_quotes(logical_line, tokens, previous_logical): 378 def check_quotes(logical_line, tokens, previous_logical):
196 first_token = True 379 first_token = True
197 offset = 0
198 380
199 for kind, token, start, end, _ in tokens: 381 for kind, token, start, end, _ in tokens:
200 if kind == tokenize.INDENT: 382 if kind == tokenize.INDENT or kind == tokenize.DEDENT:
201 offset = end[1]
202 continue 383 continue
203 384
204 if kind == tokenize.STRING: 385 if kind == tokenize.STRING:
205 pos = start[1] - offset
206 match = re.search(r'^(u)?(b)?(r)?((""")?.*)$', 386 match = re.search(r'^(u)?(b)?(r)?((""")?.*)$',
207 token, re.IGNORECASE | re.DOTALL) 387 token, re.IGNORECASE | re.DOTALL)
208 (is_unicode, is_bytes, is_raw, 388 (is_unicode, is_bytes, is_raw,
209 literal, has_doc_quotes) = match.groups() 389 literal, has_doc_quotes) = match.groups()
210 390
211 if first_token and re.search(r'^(?:(?:def|class)\s|$)', 391 if first_token and re.search(r'^(?:(?:def|class)\s|$)',
212 previous_logical): 392 previous_logical):
213 if not has_doc_quotes: 393 if not has_doc_quotes:
214 yield (pos, 'A301 use triple double quotes for docstrings') 394 yield (start, 'A109 use triple double '
215 if is_unicode or is_bytes or is_raw: 395 'quotes for docstrings')
216 yield (pos, "A302 don't use u, b or for doc strings") 396 elif is_unicode or is_bytes or is_raw:
397 yield (start, "A109 don't use u'', b'' "
398 "or r'' for doc strings")
217 elif start[0] == end[0]: 399 elif start[0] == end[0]:
218 if is_raw: 400 if is_raw:
219 literal = re.sub(r'\\(?!{})'.format(literal[0]), 401 literal = re.sub(r'\\(?!{})'.format(literal[0]),
220 '\\\\\\\\', literal) 402 '\\\\\\\\', literal)
221 403
222 if sys.version_info[0] >= 3: 404 if sys.version_info[0] >= 3:
223 if is_bytes: 405 if is_bytes:
224 literal = 'b' + literal 406 literal = 'b' + literal
225 else:
226 literal = re.sub(r'(?<!\\)\\x(?!a[0d])([a-f][0-9a-f])',
227 lambda m: chr(int(m.group(1), 16)),
228 literal)
229 elif is_unicode: 407 elif is_unicode:
230 literal = 'u' + literal 408 literal = 'u' + literal
231 409
232 if repr(eval(literal)) != literal: 410 if ascii(eval(literal)) != literal:
233 yield (pos, "A311 string literal doesn't match repr()") 411 yield (start, "A110 string literal doesn't match"
412 '{}()'.format(ascii.__name__))
234 413
235 first_token = False 414 first_token = False
236 415
237 check_quotes.name = 'abp-quotes' 416 check_quotes.name = 'abp-quotes'
238 check_quotes.version = __version__ 417 check_quotes.version = __version__
418
419
420 def check_redundant_parenthesis(logical_line, tokens):
421 start_line = tokens[0][2][0]
422 level = 0
423 statement = None
424
425 for i, (kind, token, _, end, _) in enumerate(tokens):
426 if kind == tokenize.INDENT or kind == tokenize.DEDENT:
427 continue
428
429 if statement is None:
430 # logical line doesn't start with an if, elif or while statement
431 if kind != tokenize.NAME or token not in {'if', 'elif', 'while'}:
432 break
433
434 # expression doesn't start with parenthesis
435 next_token = tokens[i + 1]
436 if next_token[:2] != (tokenize.OP, '('):
437 break
438
439 # expression is empty tuple
440 if tokens[i + 2][:2] == (tokenize.OP, ')'):
441 break
442
443 statement = token
444 pos = next_token[2]
445 continue
446
447 # expression ends on a different line, parenthesis are necessary
448 if end[0] > start_line:
449 break
450
451 if kind == tokenize.OP:
452 if token == ',':
453 # expression is non-empty tuple
454 if level == 1:
455 break
456 elif token == '(':
457 level += 1
458 elif token == ')':
459 level -= 1
460 if level == 0:
461 # outer parenthesis closed before end of expression
462 if tokens[i + 1][:2] != (tokenize.OP, ':'):
463 break
464
465 return [(pos, 'A111 redundant parenthesis for {} '
466 'statement'.format(statement))]
467
468 return []
469
470 check_redundant_parenthesis.name = 'abp-redundant-parenthesis'
471 check_redundant_parenthesis.version = __version__
LEFTRIGHT

Powered by Google App Engine
This is Rietveld