 Issue 29345337:
  Noissue - Fix edge cases with raw strings in flake8-abp  (Closed)
    
  
    Issue 29345337:
  Noissue - Fix edge cases with raw strings in flake8-abp  (Closed) 
  | Left: | ||
| Right: | 
| OLD | NEW | 
|---|---|
| 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 366 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 377 | 377 | 
| 378 | 378 | 
| 379 def check_quotes(logical_line, tokens, previous_logical): | 379 def check_quotes(logical_line, tokens, previous_logical): | 
| 380 first_token = True | 380 first_token = True | 
| 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 kind == tokenize.STRING: | 386 if kind == tokenize.STRING: | 
| 387 match = re.search(r'^(u)?(b)?(r)?((""")?.*)$', | 387 match = re.search(r'^([rub]*)([\'"]{1,3})(.*)\2$', | 
| 
Sebastian Noack
2016/05/30 09:57:42
On Python 3, you can write br'' as well as rb''. T
 | |
| 388 token, re.IGNORECASE | re.DOTALL) | 388 token, re.IGNORECASE | re.DOTALL) | 
| 389 (is_unicode, is_bytes, is_raw, | 389 prefixes, quote, text = match.groups() | 
| 390 literal, has_doc_quotes) = match.groups() | 390 prefixes = prefixes.lower() | 
| 391 | 391 | 
| 392 if first_token and re.search(r'^(?:(?:def|class)\s|$)', | 392 if first_token and re.search(r'^(?:(?:def|class)\s|$)', | 
| 393 previous_logical): | 393 previous_logical): | 
| 394 if not has_doc_quotes: | 394 if quote != '"""': | 
| 395 yield (start, 'A109 use triple double ' | 395 yield (start, 'A109 use triple double ' | 
| 396 'quotes for docstrings') | 396 'quotes for docstrings') | 
| 397 elif is_unicode or is_bytes or is_raw: | 397 elif prefixes: | 
| 398 yield (start, "A109 don't use u'', b'' " | 398 yield (start, "A109 don't use u'', b'' " | 
| 399 "or r'' for doc strings") | 399 "or r'' for doc strings") | 
| 400 elif start[0] == end[0]: | 400 elif start[0] == end[0]: | 
| 401 if is_raw: | 401 if 'r' in prefixes: | 
| 402 literal = re.sub(r'\\(?!{})'.format(literal[0]), | 402 if quote != "'" and not (quote == '"' and "'" in text): | 
| 
Sebastian Noack
2016/05/30 09:57:42
The problem with old logic was that there are case
 
Vasily Kuznetsov
2016/05/30 10:08:19
I think it would be good to add a test for this ca
 
Sebastian Noack
2016/05/30 10:18:04
Done.
 | |
| 403 '\\\\\\\\', literal) | 403 yield (start, 'A110 use single quotes for raw string') | 
| 404 else: | |
| 405 prefix = 'b' if sys.version_info[0] >= 3 else 'u' | |
| 406 if prefix not in prefixes: | |
| 407 prefix = '' | |
| 404 | 408 | 
| 405 if sys.version_info[0] >= 3: | 409 literal = '{0}{1}{2}{1}'.format(prefix, quote, text) | 
| 406 if is_bytes: | 410 if ascii(eval(literal)) != literal: | 
| 407 literal = 'b' + literal | 411 yield (start, "A110 string literal doesn't match " | 
| 408 elif is_unicode: | 412 '{}()'.format(ascii.__name__)) | 
| 409 literal = 'u' + literal | |
| 410 | |
| 411 if ascii(eval(literal)) != literal: | |
| 412 yield (start, "A110 string literal doesn't match " | |
| 413 '{}()'.format(ascii.__name__)) | |
| 414 | 413 | 
| 415 first_token = False | 414 first_token = False | 
| 416 | 415 | 
| 417 check_quotes.name = 'abp-quotes' | 416 check_quotes.name = 'abp-quotes' | 
| 418 check_quotes.version = __version__ | 417 check_quotes.version = __version__ | 
| 419 | 418 | 
| 420 | 419 | 
| 421 def check_redundant_parenthesis(logical_line, tokens): | 420 def check_redundant_parenthesis(logical_line, tokens): | 
| 422 start_line = tokens[0][2][0] | 421 start_line = tokens[0][2][0] | 
| 423 level = 0 | 422 level = 0 | 
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 463 if tokens[i + 1][:2] != (tokenize.OP, ':'): | 462 if tokens[i + 1][:2] != (tokenize.OP, ':'): | 
| 464 break | 463 break | 
| 465 | 464 | 
| 466 return [(pos, 'A111 redundant parenthesis for {} ' | 465 return [(pos, 'A111 redundant parenthesis for {} ' | 
| 467 'statement'.format(statement))] | 466 'statement'.format(statement))] | 
| 468 | 467 | 
| 469 return [] | 468 return [] | 
| 470 | 469 | 
| 471 check_redundant_parenthesis.name = 'abp-redundant-parenthesis' | 470 check_redundant_parenthesis.name = 'abp-redundant-parenthesis' | 
| 472 check_redundant_parenthesis.version = __version__ | 471 check_redundant_parenthesis.version = __version__ | 
| OLD | NEW |