OLD | NEW |
| (Empty) |
1 # This file is part of Adblock Plus <https://adblockplus.org/>, | |
2 # Copyright (C) 2006-present eyeo GmbH | |
3 # | |
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 | |
6 # published by the Free Software Foundation. | |
7 # | |
8 # Adblock Plus is distributed in the hope that it will be useful, | |
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
11 # GNU General Public License for more details. | |
12 # | |
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/>. | |
15 | |
16 import ast | |
17 import re | |
18 import tokenize | |
19 import sys | |
20 import collections | |
21 | |
22 try: | |
23 import builtins | |
24 except ImportError: | |
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-eyeo').version | |
35 | |
36 DISCOURAGED_APIS = { | |
37 're.match': 're.search', | |
38 'codecs.open': 'io.open', | |
39 } | |
40 | |
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, namespace): | |
49 try: | |
50 return eval(compile(ast.Expression(node), '', 'eval'), namespace) | |
51 except Exception: | |
52 return VOLATILE | |
53 | |
54 | |
55 def is_const(node): | |
56 namespace = {'__builtins__': {'True': True, 'False': False, 'None': None}} | |
57 return evaluate(node, namespace) is not VOLATILE | |
58 | |
59 | |
60 def get_identifier(node): | |
61 if isinstance(node, ast.Name): | |
62 return node.id | |
63 if isinstance(node, ast.Attribute) and isinstance(node.value, ast.Name): | |
64 return '{}.{}'.format(node.value.id, node.attr) | |
65 | |
66 | |
67 def get_statement(node): | |
68 return type(node).__name__.lower() | |
69 | |
70 | |
71 def get_descendant_nodes(node): | |
72 for child in ast.iter_child_nodes(node): | |
73 yield (child, node) | |
74 for nodes in get_descendant_nodes(child): | |
75 yield nodes | |
76 | |
77 | |
78 class TreeVisitor(ast.NodeVisitor): | |
79 Scope = collections.namedtuple('Scope', ['node', 'names', 'globals']) | |
80 | |
81 def __init__(self): | |
82 self.errors = [] | |
83 self.scope_stack = [] | |
84 | |
85 def _visit_block(self, nodes, block_required=False, | |
86 nodes_required=True, docstring=False, | |
87 can_have_unused_expr=False): | |
88 pass_node = None | |
89 has_non_pass = False | |
90 leave_node = None | |
91 dead_code = False | |
92 | |
93 for i, node in enumerate(nodes): | |
94 if isinstance(node, ast.Pass): | |
95 pass_node = node | |
96 else: | |
97 has_non_pass = True | |
98 | |
99 if leave_node and not dead_code: | |
100 dead_code = True | |
101 statement = get_statement(leave_node) | |
102 self.errors.append((node, 'A202 dead code after ' | |
103 '{}'.format(statement))) | |
104 | |
105 if isinstance(node, LEAVE_BLOCK): | |
106 leave_node = node | |
107 | |
108 if can_have_unused_expr or not isinstance(node, ast.Expr): | |
109 continue | |
110 if docstring and i == 0 and isinstance(node.value, ast.Str): | |
111 continue | |
112 | |
113 non_literal_expr_nodes = (ast.Call, ast.Yield) | |
114 try: | |
115 non_literal_expr_nodes += (ast.YieldFrom,) | |
116 except AttributeError: | |
117 pass | |
118 if isinstance(node.value, non_literal_expr_nodes): | |
119 continue | |
120 | |
121 self.errors.append((node, 'A203 unused expression')) | |
122 | |
123 if pass_node: | |
124 if not nodes_required or len(nodes) > 1: | |
125 self.errors.append((pass_node, 'A204 redundant ' | |
126 'pass statement')) | |
127 | |
128 if not block_required and not has_non_pass: | |
129 self.errors.append((pass_node, 'A205 empty block')) | |
130 | |
131 def _check_redundant_else(self, node, handlers, clause): | |
132 if not node.orelse: | |
133 return | |
134 | |
135 for handler in handlers: | |
136 for child in handler.body: | |
137 if isinstance(child, LEAVE_BLOCK): | |
138 leave_node = child | |
139 break | |
140 else: | |
141 return | |
142 | |
143 statement = get_statement(leave_node) | |
144 self.errors.append((node.orelse[0], | |
145 'A206 Extraneous else statement after {} ' | |
146 'in {}-clause'.format(statement, clause))) | |
147 | |
148 def visit_If(self, node): | |
149 self._visit_block(node.body, block_required=bool(node.orelse)) | |
150 self._visit_block(node.orelse) | |
151 self._check_redundant_else(node, [node], 'if') | |
152 self.generic_visit(node) | |
153 | |
154 def visit_Try(self, node): | |
155 self._visit_block(node.body, can_have_unused_expr=bool(node.handlers)) | |
156 self._visit_block(node.orelse) | |
157 self._visit_block(node.finalbody) | |
158 self._check_redundant_else(node, node.handlers, 'except') | |
159 self.generic_visit(node) | |
160 | |
161 def visit_TryExcept(self, node): | |
162 self._visit_block(node.body, can_have_unused_expr=True) | |
163 self._visit_block(node.orelse) | |
164 self._check_redundant_else(node, node.handlers, 'except') | |
165 self.generic_visit(node) | |
166 | |
167 def visit_TryFinally(self, node): | |
168 self._visit_block(node.body) | |
169 self._visit_block(node.finalbody) | |
170 self.generic_visit(node) | |
171 | |
172 def visit_ExceptHandler(self, node): | |
173 self._visit_block(node.body, block_required=True) | |
174 self.generic_visit(node) | |
175 | |
176 def _visit_stored_name(self, node, name): | |
177 scope = self.scope_stack[-1] | |
178 scope.names.add(name) | |
179 | |
180 if name in ESSENTIAL_BUILTINS and isinstance(scope.node, | |
181 ast.FunctionDef): | |
182 self.errors.append((node, 'A302 redefined built-in ' + name)) | |
183 | |
184 def visit_Name(self, node): | |
185 if isinstance(node.ctx, (ast.Store, ast.Param)): | |
186 self._visit_stored_name(node, node.id) | |
187 | |
188 def visit_arg(self, node): | |
189 self._visit_stored_name(node, node.arg) | |
190 | |
191 def _visit_with_scope(self, node): | |
192 scope = self.Scope(node, names=set(), globals=[]) | |
193 self.scope_stack.append(scope) | |
194 self.generic_visit(node) | |
195 del self.scope_stack[-1] | |
196 return scope | |
197 | |
198 def visit_Module(self, node): | |
199 self._visit_block(node.body, block_required=True, | |
200 nodes_required=False, docstring=True) | |
201 self._visit_with_scope(node) | |
202 | |
203 def visit_FunctionDef(self, node): | |
204 self._visit_stored_name(node, node.name) | |
205 self._visit_block(node.body, block_required=True, docstring=True) | |
206 | |
207 scope = self._visit_with_scope(node) | |
208 global_names = set() | |
209 | |
210 for declaration in scope.globals: | |
211 for name in declaration.names: | |
212 if name not in scope.names or name in global_names: | |
213 statement = get_statement(declaration) | |
214 self.errors.append((declaration, | |
215 'A201 redundant {} declaration for ' | |
216 '{}'.format(statement, name))) | |
217 else: | |
218 global_names.add(name) | |
219 | |
220 visit_ClassDef = visit_FunctionDef | |
221 | |
222 def visit_Global(self, node): | |
223 scope = self.scope_stack[-1] | |
224 scope.globals.append(node) | |
225 | |
226 if isinstance(scope.node, ast.Module): | |
227 statement = get_statement(node) | |
228 self.errors.append((node, 'A201 {} declaration on ' | |
229 'top-level'.format(statement))) | |
230 | |
231 visit_Nonlocal = visit_Global | |
232 | |
233 def _visit_iter(self, node): | |
234 if isinstance(node, (ast.Tuple, ast.Set)): | |
235 self.errors.append((node, 'A101 use lists for data ' | |
236 'that have order')) | |
237 | |
238 def visit_comprehension(self, node): | |
239 self._visit_iter(node.iter) | |
240 self.generic_visit(node) | |
241 | |
242 def visit_For(self, node): | |
243 self._visit_iter(node.iter) | |
244 self._visit_block(node.body, block_required=True) | |
245 self._visit_block(node.orelse) | |
246 self.generic_visit(node) | |
247 | |
248 def visit_While(self, node): | |
249 self._visit_block(node.body, block_required=True) | |
250 self._visit_block(node.orelse) | |
251 self.generic_visit(node) | |
252 | |
253 def visit_BinOp(self, node): | |
254 if isinstance(node.op, ast.Mod) and isinstance(node.left, ast.Str): | |
255 self.errors.append((node, 'A107 use format() instead of ' | |
256 '% operator for string formatting')) | |
257 | |
258 multi_addition = (isinstance(node.op, ast.Add) and | |
259 isinstance(node.left, ast.BinOp) and | |
260 isinstance(node.left.op, ast.Add)) | |
261 if multi_addition and (isinstance(node.left.left, ast.Str) or | |
262 isinstance(node.left.right, ast.Str) or | |
263 isinstance(node.right, ast.Str)): | |
264 self.errors.append((node, 'A108 use format() instead of ' | |
265 '+ operator when concatenating ' | |
266 'more than two strings')) | |
267 | |
268 self.generic_visit(node) | |
269 | |
270 def visit_Compare(self, node): | |
271 left = node.left | |
272 single = len(node.ops) == 1 | |
273 | |
274 for op, right in zip(node.ops, node.comparators): | |
275 membership = isinstance(op, (ast.In, ast.NotIn)) | |
276 symmetric = isinstance(op, (ast.Eq, ast.NotEq, ast.Is, ast.IsNot)) | |
277 | |
278 if membership and isinstance(right, (ast.Tuple, ast.List)): | |
279 self.errors.append((right, 'A102 use sets for distinct ' | |
280 'unordered data')) | |
281 | |
282 consts_first = single and not membership or symmetric | |
283 if consts_first and is_const(left) and not is_const(right): | |
284 self.errors.append((left, 'A103 yoda condition')) | |
285 | |
286 left = right | |
287 | |
288 self.generic_visit(node) | |
289 | |
290 def _check_deprecated(self, node, name): | |
291 substitute = DISCOURAGED_APIS.get(name) | |
292 if substitute: | |
293 self.errors.append((node, 'A301 use {}() instead of ' | |
294 '{}()'.format(substitute, name))) | |
295 | |
296 def visit_Call(self, node): | |
297 func = get_identifier(node.func) | |
298 arg = next(iter(node.args), None) | |
299 redundant_literal = False | |
300 | |
301 if isinstance(arg, ast.Lambda): | |
302 if len(node.args) == 2 and func in {'map', 'filter', | |
303 'imap', 'ifilter', | |
304 'itertools.imap', | |
305 'itertools.ifilter'}: | |
306 self.errors.append((node, 'A104 use a comprehension ' | |
307 'instead of calling {}() with ' | |
308 'lambda function'.format(func))) | |
309 elif isinstance(arg, (ast.List, ast.Tuple)): | |
310 if func == 'dict': | |
311 redundant_literal = all(isinstance(elt, (ast.Tuple, ast.List)) | |
312 for elt in arg.elts) | |
313 else: | |
314 redundant_literal = func in {'list', 'set', 'tuple'} | |
315 elif isinstance(arg, (ast.ListComp, ast.GeneratorExp)): | |
316 if func == 'dict': | |
317 redundant_literal = isinstance(arg.elt, (ast.Tuple, ast.List)) | |
318 else: | |
319 redundant_literal = func in {'list', 'set'} | |
320 | |
321 if redundant_literal: | |
322 self.errors.append((node, 'A105 use a {0} literal or ' | |
323 'comprehension instead of calling ' | |
324 '{0}()'.format(func))) | |
325 | |
326 self._check_deprecated(node, func) | |
327 self.generic_visit(node) | |
328 | |
329 def visit_Import(self, node): | |
330 for alias in node.names: | |
331 self._visit_stored_name(node, alias.asname or alias.name) | |
332 | |
333 if hasattr(node, 'module'): | |
334 self._check_deprecated(node, '{}.{}'.format(node.module, | |
335 alias.name)) | |
336 | |
337 visit_ImportFrom = visit_Import | |
338 | |
339 def visit_Assign(self, node): | |
340 if isinstance(node.value, ast.BinOp) and len(node.targets) == 1: | |
341 target = node.targets[0] | |
342 left_is_target = (isinstance(target, ast.Name) and | |
343 isinstance(node.value.left, ast.Name) and | |
344 target.id == node.value.left.id) | |
345 if left_is_target: | |
346 self.errors.append((node, 'A106 use augment assignment, ' | |
347 'e.g. x += y instead x = x + y')) | |
348 self.generic_visit(node) | |
349 | |
350 def _visit_hash_keys(self, nodes, what): | |
351 keys = [] | |
352 namespace = collections.defaultdict(object, vars(builtins)) | |
353 for node in nodes: | |
354 key = evaluate(node, namespace) | |
355 if key is VOLATILE: | |
356 continue | |
357 | |
358 if key in keys: | |
359 self.errors.append((node, 'A207 duplicate ' + what)) | |
360 continue | |
361 | |
362 keys.append(key) | |
363 | |
364 def visit_Dict(self, node): | |
365 self._visit_hash_keys(node.keys, 'key in dict') | |
366 | |
367 def visit_Set(self, node): | |
368 self._visit_hash_keys(node.elts, 'item in set') | |
369 | |
370 | |
371 def check_ast(tree): | |
372 visitor = TreeVisitor() | |
373 visitor.visit(tree) | |
374 | |
375 for node, error in visitor.errors: | |
376 yield (node.lineno, node.col_offset, error, None) | |
377 | |
378 | |
379 def check_non_default_encoding(physical_line, line_number): | |
380 if line_number <= 2 and re.search(r'^\s*#.*coding[:=]', physical_line): | |
381 return (0, 'A303 non-default file encoding') | |
382 | |
383 | |
384 def check_quotes(logical_line, tokens, previous_logical, checker_state): | |
385 first_token = True | |
386 | |
387 token_strings = [t[1] for t in tokens] | |
388 future_import = token_strings[:3] == ['from', '__future__', 'import'] | |
389 | |
390 if future_import and 'unicode_literals' in token_strings: | |
391 checker_state['has_unicode_literals'] = True | |
392 | |
393 for kind, token, start, end, _ in tokens: | |
394 if kind == tokenize.INDENT or kind == tokenize.DEDENT: | |
395 continue | |
396 | |
397 if kind == tokenize.STRING: | |
398 match = re.search(r'^([rub]*)([\'"]{1,3})(.*)\2$', | |
399 token, re.IGNORECASE | re.DOTALL) | |
400 prefixes, quote, text = match.groups() | |
401 prefixes = prefixes.lower() | |
402 | |
403 if 'u' in prefixes: | |
404 yield (start, 'A112 use "from __future__ import ' | |
405 'unicode_literals" instead of ' | |
406 'prefixing literals with "u"') | |
407 | |
408 if first_token and re.search(r'^(?:(?:def|class)\s|$)', | |
409 previous_logical): | |
410 pass # Ignore docstrings | |
411 elif start[0] != end[0]: | |
412 pass # Ignore multiline strings | |
413 elif 'r' in prefixes: | |
414 if quote != "'" and not (quote == '"' and "'" in text): | |
415 yield (start, 'A110 use single quotes for raw string') | |
416 else: | |
417 prefix = '' | |
418 if sys.version_info[0] >= 3: | |
419 if 'b' in prefixes: | |
420 prefix = 'b' | |
421 else: | |
422 u_literals = checker_state.get('has_unicode_literals') | |
423 if 'u' in prefixes or u_literals and 'b' not in prefixes: | |
424 prefix = 'u' | |
425 | |
426 literal = '{0}{1}{2}{1}'.format(prefix, quote, text) | |
427 if ascii(eval(literal)) != literal: | |
428 yield (start, "A110 string literal doesn't match " | |
429 '{}()'.format(ascii.__name__)) | |
430 | |
431 first_token = False | |
432 | |
433 | |
434 def check_redundant_parenthesis(tree, lines, file_tokens): | |
435 orig = ast.dump(tree) | |
436 nodes = get_descendant_nodes(tree) | |
437 stack = [] | |
438 | |
439 for i, (kind, token, _, _, _) in enumerate(file_tokens): | |
440 if kind != tokenize.OP: | |
441 continue | |
442 | |
443 if token == '(': | |
444 stack.append(i) | |
445 elif token == ')': | |
446 start = stack.pop() | |
447 sample = lines[:] | |
448 | |
449 for pos in [i, start]: | |
450 _, _, (lineno, col1), (_, col2), _ = file_tokens[pos] | |
451 lineno -= 1 | |
452 sample[lineno] = (sample[lineno][:col1] + | |
453 sample[lineno][col2:]) | |
454 | |
455 try: | |
456 modified = ast.parse(''.join(sample)) | |
457 except SyntaxError: | |
458 # Parentheses syntactically required. | |
459 continue | |
460 | |
461 # Parentheses logically required. | |
462 if orig != ast.dump(modified): | |
463 continue | |
464 | |
465 pos = file_tokens[start][2] | |
466 while True: | |
467 node, parent = next(nodes) | |
468 if pos < (getattr(node, 'lineno', -1), | |
469 getattr(node, 'col_offset', -1)): | |
470 break | |
471 | |
472 # Allow redundant parentheses for readability, | |
473 # when creating tuples (but not when unpacking variables), | |
474 # nested operations and comparisons inside assignments. | |
475 is_tuple = ( | |
476 isinstance(node, ast.Tuple) and not ( | |
477 isinstance(parent, (ast.For, ast.comprehension)) and | |
478 node == parent.target or | |
479 isinstance(parent, ast.Assign) and | |
480 node in parent.targets | |
481 ) | |
482 ) | |
483 is_nested_op = ( | |
484 isinstance(node, (ast.BinOp, ast.BoolOp)) and | |
485 isinstance(parent, (ast.BinOp, ast.BoolOp)) | |
486 ) | |
487 is_compare_in_assign = ( | |
488 isinstance(parent, (ast.Assign, ast.keyword)) and | |
489 any(isinstance(x, ast.Compare) for x in ast.walk(node)) | |
490 ) | |
491 if is_tuple or is_nested_op or is_compare_in_assign: | |
492 continue | |
493 | |
494 yield (pos[0], pos[1], 'A111 redundant parenthesis', None) | |
495 | |
496 | |
497 class DefaultConfigOverride: | |
498 def __init__(self, _): | |
499 pass | |
500 | |
501 @classmethod | |
502 def add_options(cls, parser): | |
503 parser.extend_default_ignore([ | |
504 # We don't want to make doc strings mandatory | |
505 # but merely lint existing doc strings. | |
506 'D1', | |
507 # Adding a comma after variable args/kwargs | |
508 # is a syntax error in Python 2 (and <= 3.4). | |
509 'C815', | |
510 ]) | |
511 | |
512 # Remove everything but W503 & W504 from the built-in default ignores. | |
513 parser.parser.defaults['ignore'] = 'W503,W504' | |
514 | |
515 | |
516 for checker in [check_ast, check_non_default_encoding, check_quotes, | |
517 check_redundant_parenthesis, DefaultConfigOverride]: | |
518 checker.name = 'eyeo' | |
519 checker.version = __version__ | |
OLD | NEW |