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

Side by Side Diff: flake8-abp/flake8_abp.py

Issue 29342824: Issue 4044 - Added handling for __future__ unicode_literals import to check_quotes() (Closed)
Patch Set: Updates readme for clarity, moves unicode prefix check out of single line and docstring if statemen… Created May 26, 2016, 9:17 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
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 26 matching lines...) Expand all
37 're.match': 're.search', 37 're.match': 're.search',
38 'codecs.open': 'io.open', 38 'codecs.open': 'io.open',
39 } 39 }
40 40
41 ESSENTIAL_BUILTINS = set(dir(builtins)) - {'apply', 'buffer', 'coerce', 41 ESSENTIAL_BUILTINS = set(dir(builtins)) - {'apply', 'buffer', 'coerce',
42 'intern', 'file'} 42 'intern', 'file'}
43 43
44 LEAVE_BLOCK = (ast.Return, ast.Raise, ast.Continue, ast.Break) 44 LEAVE_BLOCK = (ast.Return, ast.Raise, ast.Continue, ast.Break)
45 VOLATILE = object() 45 VOLATILE = object()
46 46
47 is_unicode_literals = False
Sebastian Noack 2016/05/27 12:33:48 has_unicode_literals seems to be more correct gram
48
47 49
48 def evaluate(node): 50 def evaluate(node):
49 try: 51 try:
50 return eval(compile(ast.Expression(node), '', 'eval'), {}) 52 return eval(compile(ast.Expression(node), '', 'eval'), {})
51 except Exception: 53 except Exception:
52 return VOLATILE 54 return VOLATILE
53 55
54 56
55 def is_const(node): 57 def is_const(node):
56 return evaluate(node) is not VOLATILE 58 return evaluate(node) is not VOLATILE
(...skipping 313 matching lines...) Expand 10 before | Expand all | Expand 10 after
370 def check_non_default_encoding(physical_line, line_number): 372 def check_non_default_encoding(physical_line, line_number):
371 if line_number <= 2 and re.search(r'^\s*#.*coding[:=]', physical_line): 373 if line_number <= 2 and re.search(r'^\s*#.*coding[:=]', physical_line):
372 return (0, 'A303 non-default file encoding') 374 return (0, 'A303 non-default file encoding')
373 375
374 check_non_default_encoding.name = 'abp-non-default-encoding' 376 check_non_default_encoding.name = 'abp-non-default-encoding'
375 check_non_default_encoding.version = __version__ 377 check_non_default_encoding.version = __version__
376 378
377 379
378 def check_quotes(logical_line, tokens, previous_logical): 380 def check_quotes(logical_line, tokens, previous_logical):
379 first_token = True 381 first_token = True
382 global is_unicode_literals
383
384 # check if this is beginning of file
385 if tokens[0][3][0] == 1:
386 is_unicode_literals = False
387
388 # check if in unicode_literals mode
389 token_strings = [t[1] for t in tokens]
390 if token_strings[:3] == ['from', '__future__', 'import']:
391 if 'unicode_literals' in token_strings:
Sebastian Noack 2016/05/27 12:13:55 This is an superfluous level of indentation. I'd r
Jon Sonesen 2016/06/01 17:06:59 I like that and will get it done.
392 is_unicode_literals = True
380 393
381 for kind, token, start, end, _ in tokens: 394 for kind, token, start, end, _ in tokens:
382 if kind == tokenize.INDENT or kind == tokenize.DEDENT: 395 if kind == tokenize.INDENT or kind == tokenize.DEDENT:
383 continue 396 continue
384 397
385 if kind == tokenize.STRING: 398 if kind == tokenize.STRING:
386 match = re.search(r'^(u)?(b)?(r)?((""")?.*)$', 399 match = re.search(r'^(u)?(b)?(r)?((""")?.*)$',
387 token, re.IGNORECASE | re.DOTALL) 400 token, re.IGNORECASE | re.DOTALL)
388 (is_unicode, is_bytes, is_raw, 401 (is_unicode, is_bytes, is_raw,
389 literal, has_doc_quotes) = match.groups() 402 literal, has_doc_quotes) = match.groups()
390 403
404 if is_unicode:
405 yield (start, 'A112 use "from __future__ import'
Sebastian Noack 2016/05/27 12:13:55 A space is missing here. This message would be pri
406 'unicode_literals" instead of '
407 'prefixing literals with "u"')
391 if first_token and re.search(r'^(?:(?:def|class)\s|$)', 408 if first_token and re.search(r'^(?:(?:def|class)\s|$)',
Sebastian Noack 2016/05/27 12:13:55 Since this is not an else or elif block, I think t
392 previous_logical): 409 previous_logical):
393 if not has_doc_quotes: 410 if not has_doc_quotes:
394 yield (start, 'A109 use triple double ' 411 yield (start, 'A109 use triple double '
395 'quotes for docstrings') 412 'quotes for docstrings')
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")
399 elif start[0] == end[0]: 413 elif start[0] == end[0]:
400 if is_raw: 414 if is_raw:
401 literal = re.sub(r'\\(?!{})'.format(literal[0]), 415 literal = re.sub(r'\\(?!{})'.format(literal[0]),
402 '\\\\\\\\', literal) 416 '\\\\\\\\', literal)
403
404 if sys.version_info[0] >= 3: 417 if sys.version_info[0] >= 3:
405 if is_bytes: 418 if is_bytes:
406 literal = 'b' + literal 419 literal = 'b' + literal
407 elif is_unicode: 420 elif not is_bytes:
Sebastian Noack 2016/05/27 12:33:48 Currently, the is_unicode_literals variable is nev
408 literal = 'u' + literal 421 literal = 'u' + literal
409 422
410 if ascii(eval(literal)) != literal: 423 if ascii(eval(literal)) != literal:
411 yield (start, "A110 string literal doesn't match " 424 yield (start, "A110 string literal doesn't match "
412 '{}()'.format(ascii.__name__)) 425 '{}()'.format(ascii.__name__))
413 426
414 first_token = False 427 first_token = False
415 428
416 check_quotes.name = 'abp-quotes' 429 check_quotes.name = 'abp-quotes'
417 check_quotes.version = __version__ 430 check_quotes.version = __version__
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
462 if tokens[i + 1][:2] != (tokenize.OP, ':'): 475 if tokens[i + 1][:2] != (tokenize.OP, ':'):
463 break 476 break
464 477
465 return [(pos, 'A111 redundant parenthesis for {} ' 478 return [(pos, 'A111 redundant parenthesis for {} '
466 'statement'.format(statement))] 479 'statement'.format(statement))]
467 480
468 return [] 481 return []
469 482
470 check_redundant_parenthesis.name = 'abp-redundant-parenthesis' 483 check_redundant_parenthesis.name = 'abp-redundant-parenthesis'
471 check_redundant_parenthesis.version = __version__ 484 check_redundant_parenthesis.version = __version__
OLDNEW

Powered by Google App Engine
This is Rietveld