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: Created May 19, 2016, 9:57 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
« no previous file with comments | « no previous file | flake8-abp/tests/A110_unicode_literals.py » ('j') | 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
Sebastian Noack 2016/05/20 00:04:34 While I didnt test this change yet I have a hard t
Vasily Kuznetsov 2016/05/20 08:46:24 Funnily enough, the test does pass in the first ve
380 381
381 for kind, token, start, end, _ in tokens: 382 for kind, token, start, end, _ in tokens:
382 if kind == tokenize.INDENT or kind == tokenize.DEDENT: 383 if kind == tokenize.INDENT or kind == tokenize.DEDENT:
383 continue 384 continue
384 385
386 if token is 'unicode_literals':
Sebastian Noack 2016/05/20 00:04:34 It's not as simple as that. Following examples wil
Jon Sonesen 2016/05/20 02:00:01 Yes I should be checking the logical line object w
387 is_unicode_literals = True
388
385 if kind == tokenize.STRING: 389 if kind == tokenize.STRING:
386 match = re.search(r'^(u)?(b)?(r)?((""")?.*)$', 390 match = re.search(r'^(u)?(b)?(r)?((""")?.*)$',
387 token, re.IGNORECASE | re.DOTALL) 391 token, re.IGNORECASE | re.DOTALL)
388 (is_unicode, is_bytes, is_raw, 392 (is_unicode, is_bytes, is_raw,
389 literal, has_doc_quotes) = match.groups() 393 literal, has_doc_quotes) = match.groups()
390 394
391 if first_token and re.search(r'^(?:(?:def|class)\s|$)', 395 if first_token and re.search(r'^(?:(?:def|class)\s|$)',
392 previous_logical): 396 previous_logical):
393 if not has_doc_quotes: 397 if not has_doc_quotes:
394 yield (start, 'A109 use triple double ' 398 yield (start, 'A109 use triple double '
395 'quotes for docstrings') 399 'quotes for docstrings')
396 elif is_unicode or is_bytes or is_raw: 400 elif is_unicode or is_bytes or is_raw:
397 yield (start, "A109 don't use u'', b'' " 401 yield (start, "A109 don't use u'', b'' "
398 "or r'' for doc strings") 402 "or r'' for doc strings")
399 elif start[0] == end[0]: 403 elif start[0] == end[0]:
400 if is_raw: 404 if is_raw:
401 literal = re.sub(r'\\(?!{})'.format(literal[0]), 405 literal = re.sub(r'\\(?!{})'.format(literal[0]),
402 '\\\\\\\\', literal) 406 '\\\\\\\\', literal)
403 407
404 if sys.version_info[0] >= 3: 408 if sys.version_info[0] >= 3:
405 if is_bytes: 409 if is_bytes:
406 literal = 'b' + literal 410 literal = 'b' + literal
407 elif is_unicode: 411 elif is_unicode:
408 literal = 'u' + literal 412 literal = 'u' + literal
413 elif not is_unicode_literals:
Sebastian Noack 2016/05/20 00:04:34 Just add it to the condition above using "or" rath
Jon Sonesen 2016/05/20 02:00:01 Will do.
414 literal = 'u' + literal
409 415
410 if ascii(eval(literal)) != literal: 416 if ascii(eval(literal)) != literal:
411 yield (start, "A110 string literal doesn't match " 417 yield (start, "A110 string literal doesn't match "
412 '{}()'.format(ascii.__name__)) 418 '{}()'.format(ascii.__name__))
413 419
414 first_token = False 420 first_token = False
415 421
416 check_quotes.name = 'abp-quotes' 422 check_quotes.name = 'abp-quotes'
417 check_quotes.version = __version__ 423 check_quotes.version = __version__
418 424
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
462 if tokens[i + 1][:2] != (tokenize.OP, ':'): 468 if tokens[i + 1][:2] != (tokenize.OP, ':'):
463 break 469 break
464 470
465 return [(pos, 'A111 redundant parenthesis for {} ' 471 return [(pos, 'A111 redundant parenthesis for {} '
466 'statement'.format(statement))] 472 'statement'.format(statement))]
467 473
468 return [] 474 return []
469 475
470 check_redundant_parenthesis.name = 'abp-redundant-parenthesis' 476 check_redundant_parenthesis.name = 'abp-redundant-parenthesis'
471 check_redundant_parenthesis.version = __version__ 477 check_redundant_parenthesis.version = __version__
OLDNEW
« no previous file with comments | « no previous file | flake8-abp/tests/A110_unicode_literals.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld