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: rebase and logic error fixes Created May 20, 2016, 1:55 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 359 matching lines...) Expand 10 before | Expand all | Expand 10 after
370 def check_non_default_encoding(physical_line, line_number): 370 def check_non_default_encoding(physical_line, line_number):
371 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):
372 return (0, 'A303 non-default file encoding') 372 return (0, 'A303 non-default file encoding')
373 373
374 check_non_default_encoding.name = 'abp-non-default-encoding' 374 check_non_default_encoding.name = 'abp-non-default-encoding'
375 check_non_default_encoding.version = __version__ 375 check_non_default_encoding.version = __version__
376 376
377 377
378 def check_quotes(logical_line, tokens, previous_logical): 378 def check_quotes(logical_line, tokens, previous_logical):
379 first_token = True 379 first_token = True
380 is_unicode_literals = False 380 global is_unicode_literals
381 381
382 for kind, token, start, end, _ in tokens: 382 for kind, token, start, end, _ in tokens:
383 if kind == tokenize.INDENT or kind == tokenize.DEDENT: 383 if kind == tokenize.INDENT or kind == tokenize.DEDENT:
384 continue 384 continue
385 385
386 if token is 'unicode_literals': 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
387 is_unicode_literals = True 390 is_unicode_literals = True
388 391
389 if kind == tokenize.STRING: 392 if kind == tokenize.STRING:
390 match = re.search(r'^(u)?(b)?(r)?((""")?.*)$', 393 match = re.search(r'^(u)?(b)?(r)?((""")?.*)$',
391 token, re.IGNORECASE | re.DOTALL) 394 token, re.IGNORECASE | re.DOTALL)
392 (is_unicode, is_bytes, is_raw, 395 (is_unicode, is_bytes, is_raw,
393 literal, has_doc_quotes) = match.groups() 396 literal, has_doc_quotes) = match.groups()
394 397
395 if first_token and re.search(r'^(?:(?:def|class)\s|$)', 398 if first_token and re.search(r'^(?:(?:def|class)\s|$)',
396 previous_logical): 399 previous_logical):
397 if not has_doc_quotes: 400 if not has_doc_quotes:
398 yield (start, 'A109 use triple double ' 401 yield (start, 'A109 use triple double '
399 'quotes for docstrings') 402 'quotes for docstrings')
400 elif is_unicode or is_bytes or is_raw: 403 elif is_unicode or is_bytes or is_raw:
401 yield (start, "A109 don't use u'', b'' " 404 yield (start, "A109 don't use u'', b'' "
402 "or r'' for doc strings") 405 "or r'' for doc strings")
403 elif start[0] == end[0]: 406 elif start[0] == end[0]:
404 if is_raw: 407 if is_raw:
405 literal = re.sub(r'\\(?!{})'.format(literal[0]), 408 literal = re.sub(r'\\(?!{})'.format(literal[0]),
406 '\\\\\\\\', literal) 409 '\\\\\\\\', literal)
407 410
408 if sys.version_info[0] >= 3: 411 if sys.version_info[0] >= 3:
409 if is_bytes: 412 if is_bytes:
410 literal = 'b' + literal 413 literal = 'b' + literal
411 elif is_unicode: 414 elif is_unicode or is_unicode_literals:
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
412 literal = 'u' + literal
413 elif not is_unicode_literals:
414 literal = 'u' + literal 415 literal = 'u' + literal
415 416
416 if ascii(eval(literal)) != literal: 417 if ascii(eval(literal)) != literal:
417 yield (start, "A110 string literal doesn't match " 418 yield (start, "A110 string literal doesn't match "
418 '{}()'.format(ascii.__name__)) 419 '{}()'.format(ascii.__name__))
419 420
420 first_token = False 421 first_token = False
421 422
422 check_quotes.name = 'abp-quotes' 423 check_quotes.name = 'abp-quotes'
423 check_quotes.version = __version__ 424 check_quotes.version = __version__
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
468 if tokens[i + 1][:2] != (tokenize.OP, ':'): 469 if tokens[i + 1][:2] != (tokenize.OP, ':'):
469 break 470 break
470 471
471 return [(pos, 'A111 redundant parenthesis for {} ' 472 return [(pos, 'A111 redundant parenthesis for {} '
472 'statement'.format(statement))] 473 'statement'.format(statement))]
473 474
474 return [] 475 return []
475 476
476 check_redundant_parenthesis.name = 'abp-redundant-parenthesis' 477 check_redundant_parenthesis.name = 'abp-redundant-parenthesis'
477 check_redundant_parenthesis.version = __version__ 478 check_redundant_parenthesis.version = __version__
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld