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

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

Issue 29342824: Issue 4044 - Added handling for __future__ unicode_literals import to check_quotes() (Closed)
Left Patch Set: rebase and logic error fixes Created May 20, 2016, 1:55 a.m.
Right Patch Set: removed redundant comment Created June 2, 2016, 5:45 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/tests/A109.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
(...skipping 266 matching lines...) Expand 10 before | Expand all | Expand 10 after
277 substitute = DISCOURAGED_APIS.get(name) 277 substitute = DISCOURAGED_APIS.get(name)
278 if substitute: 278 if substitute:
279 self.errors.append((node, 'A301 use {}() instead of ' 279 self.errors.append((node, 'A301 use {}() instead of '
280 '{}()'.format(substitute, name))) 280 '{}()'.format(substitute, name)))
281 281
282 def visit_Call(self, node): 282 def visit_Call(self, node):
283 func = get_identifier(node.func) 283 func = get_identifier(node.func)
284 arg = next(iter(node.args), None) 284 arg = next(iter(node.args), None)
285 redundant_literal = False 285 redundant_literal = False
286 286
287 if isinstance(arg, ast.Lambda) and func in {'map', 'filter', 287 if isinstance(arg, ast.Lambda):
288 'imap', 'ifilter', 288 if len(node.args) == 2 and func in {'map', 'filter',
289 'itertools.imap', 289 'imap', 'ifilter',
290 'itertools.ifilter'}: 290 'itertools.imap',
291 self.errors.append((node, 'A104 use a comprehension ' 291 'itertools.ifilter'}:
292 'instead of calling {}() with ' 292 self.errors.append((node, 'A104 use a comprehension '
293 'lambda function'.format(func))) 293 'instead of calling {}() with '
294 'lambda function'.format(func)))
294 elif isinstance(arg, (ast.List, ast.Tuple)): 295 elif isinstance(arg, (ast.List, ast.Tuple)):
295 if func == 'dict': 296 if func == 'dict':
296 redundant_literal = all(isinstance(elt, (ast.Tuple, ast.List)) 297 redundant_literal = all(isinstance(elt, (ast.Tuple, ast.List))
297 for elt in arg.elts) 298 for elt in arg.elts)
298 else: 299 else:
299 redundant_literal = func in {'list', 'set', 'tuple'} 300 redundant_literal = func in {'list', 'set', 'tuple'}
300 elif isinstance(arg, (ast.ListComp, ast.GeneratorExp)): 301 elif isinstance(arg, (ast.ListComp, ast.GeneratorExp)):
301 if func == 'dict': 302 if func == 'dict':
302 redundant_literal = isinstance(arg.elt, (ast.Tuple, ast.List)) 303 redundant_literal = isinstance(arg.elt, (ast.Tuple, ast.List))
303 else: 304 else:
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
368 369
369 370
370 def check_non_default_encoding(physical_line, line_number): 371 def check_non_default_encoding(physical_line, line_number):
371 if line_number <= 2 and re.search(r'^\s*#.*coding[:=]', physical_line): 372 if line_number <= 2 and re.search(r'^\s*#.*coding[:=]', physical_line):
372 return (0, 'A303 non-default file encoding') 373 return (0, 'A303 non-default file encoding')
373 374
374 check_non_default_encoding.name = 'abp-non-default-encoding' 375 check_non_default_encoding.name = 'abp-non-default-encoding'
375 check_non_default_encoding.version = __version__ 376 check_non_default_encoding.version = __version__
376 377
377 378
378 def check_quotes(logical_line, tokens, previous_logical): 379 def check_quotes(logical_line, tokens, previous_logical, checker_state):
379 first_token = True 380 first_token = True
380 global is_unicode_literals 381
382 token_strings = [t[1] for t in tokens]
383 future_import = token_strings[:3] == ['from', '__future__', 'import']
384
385 if future_import and 'unicode_literals' in token_strings:
386 checker_state['has_unicode_literals'] = True
381 387
382 for kind, token, start, end, _ in tokens: 388 for kind, token, start, end, _ in tokens:
383 if kind == tokenize.INDENT or kind == tokenize.DEDENT: 389 if kind == tokenize.INDENT or kind == tokenize.DEDENT:
384 continue 390 continue
385 391
386 if start[0] == 1:
Vasily Kuznetsov 2016/05/20 08:46:24 This `if` as well as the following one don't need
387 is_unicode_literals = False
388
389 if logical_line == 'from __future__ import unicode_literals':
Vasily Kuznetsov 2016/05/20 08:46:25 Actually this is still not completely right. It co
390 is_unicode_literals = True
391
392 if kind == tokenize.STRING: 392 if kind == tokenize.STRING:
393 match = re.search(r'^(u)?(b)?(r)?((""")?.*)$', 393 match = re.search(r'^([rub]*)([\'"]{1,3})(.*)\2$',
394 token, re.IGNORECASE | re.DOTALL) 394 token, re.IGNORECASE | re.DOTALL)
395 (is_unicode, is_bytes, is_raw, 395 prefixes, quote, text = match.groups()
396 literal, has_doc_quotes) = match.groups() 396 prefixes = prefixes.lower()
397
398 if 'u' in prefixes:
399 yield (start, 'A112 use "from __future__ import '
400 'unicode_literals" instead of '
401 'prefixing literals with "u"')
397 402
398 if first_token and re.search(r'^(?:(?:def|class)\s|$)', 403 if first_token and re.search(r'^(?:(?:def|class)\s|$)',
399 previous_logical): 404 previous_logical):
400 if not has_doc_quotes: 405 if quote != '"""':
401 yield (start, 'A109 use triple double ' 406 yield (start, 'A109 use triple double '
402 'quotes for docstrings') 407 'quotes for docstrings')
403 elif is_unicode or is_bytes or is_raw: 408 elif start[0] != end[0]:
404 yield (start, "A109 don't use u'', b'' " 409 pass
405 "or r'' for doc strings") 410 elif 'r' in prefixes:
406 elif start[0] == end[0]: 411 if quote != "'" and not (quote == '"' and "'" in text):
407 if is_raw: 412 yield (start, 'A110 use single quotes for raw string')
408 literal = re.sub(r'\\(?!{})'.format(literal[0]), 413 else:
409 '\\\\\\\\', literal) 414 prefix = ''
410
411 if sys.version_info[0] >= 3: 415 if sys.version_info[0] >= 3:
412 if is_bytes: 416 if 'b' in prefixes:
413 literal = 'b' + literal 417 prefix = 'b'
414 elif is_unicode or is_unicode_literals: 418 else:
Sebastian Noack 2016/05/20 13:48:01 Perhaps we should generate a warning when using st
Vasily Kuznetsov 2016/05/23 08:51:14 Yes, this sounds like a good idea. Unfortunately i
Sebastian Noack 2016/05/24 12:41:35 Since compatibility with Python 3 is now mandatory
Vasily Kuznetsov 2016/05/24 13:02:42 Yep. All exactly as you say. And it will actually
415 literal = 'u' + literal 419 u_literals = checker_state.get('has_unicode_literals')
416 420 if 'u' in prefixes or u_literals and 'b' not in prefixes:
421 prefix = 'u'
422
423 literal = '{0}{1}{2}{1}'.format(prefix, quote, text)
417 if ascii(eval(literal)) != literal: 424 if ascii(eval(literal)) != literal:
418 yield (start, "A110 string literal doesn't match " 425 yield (start, "A110 string literal doesn't match "
419 '{}()'.format(ascii.__name__)) 426 '{}()'.format(ascii.__name__))
420 427
421 first_token = False 428 first_token = False
422 429
423 check_quotes.name = 'abp-quotes' 430 check_quotes.name = 'abp-quotes'
424 check_quotes.version = __version__ 431 check_quotes.version = __version__
425 432
426 433
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
469 if tokens[i + 1][:2] != (tokenize.OP, ':'): 476 if tokens[i + 1][:2] != (tokenize.OP, ':'):
470 break 477 break
471 478
472 return [(pos, 'A111 redundant parenthesis for {} ' 479 return [(pos, 'A111 redundant parenthesis for {} '
473 'statement'.format(statement))] 480 'statement'.format(statement))]
474 481
475 return [] 482 return []
476 483
477 check_redundant_parenthesis.name = 'abp-redundant-parenthesis' 484 check_redundant_parenthesis.name = 'abp-redundant-parenthesis'
478 check_redundant_parenthesis.version = __version__ 485 check_redundant_parenthesis.version = __version__
LEFTRIGHT

Powered by Google App Engine
This is Rietveld