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

Unified Diff: flake8-abp/flake8_abp.py

Issue 29342824: Issue 4044 - Added handling for __future__ unicode_literals import to check_quotes() (Closed)
Patch Set: changed logical if else to match suggestion from sebastian Created June 2, 2016, 1:18 a.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « flake8-abp/README.md ('k') | flake8-abp/tests/A109.py » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: flake8-abp/flake8_abp.py
===================================================================
--- a/flake8-abp/flake8_abp.py
+++ b/flake8-abp/flake8_abp.py
@@ -371,50 +371,65 @@
def check_non_default_encoding(physical_line, line_number):
if line_number <= 2 and re.search(r'^\s*#.*coding[:=]', physical_line):
return (0, 'A303 non-default file encoding')
check_non_default_encoding.name = 'abp-non-default-encoding'
check_non_default_encoding.version = __version__
-def check_quotes(logical_line, tokens, previous_logical):
+def check_quotes(logical_line, tokens, previous_logical, checker_state):
first_token = True
+ # check if in unicode_literals mode
Sebastian Noack 2016/06/02 11:41:01 I feel that comment is kinda redundant. IMO it doe
+ token_strings = [t[1] for t in tokens]
+ future_import = token_strings[:3] == ['from', '__future__', 'import']
+
+ if future_import and 'unicode_literals' in token_strings:
+ checker_state['has_unicode_literals'] = True
+
for kind, token, start, end, _ in tokens:
if kind == tokenize.INDENT or kind == tokenize.DEDENT:
continue
if kind == tokenize.STRING:
match = re.search(r'^([rub]*)([\'"]{1,3})(.*)\2$',
token, re.IGNORECASE | re.DOTALL)
prefixes, quote, text = match.groups()
prefixes = prefixes.lower()
+ if 'u' in prefixes:
+ yield (start, 'A112 use "from __future__ import '
+ 'unicode_literals" instead of '
+ 'prefixing literals with "u"')
+
if first_token and re.search(r'^(?:(?:def|class)\s|$)',
previous_logical):
if quote != '"""':
yield (start, 'A109 use triple double '
'quotes for docstrings')
- elif prefixes:
- yield (start, "A109 don't use u'', b'' "
- "or r'' for doc strings")
- elif start[0] == end[0]:
- if 'r' in prefixes:
- if quote != "'" and not (quote == '"' and "'" in text):
- yield (start, 'A110 use single quotes for raw string')
+ elif start[0] != end[0]:
+ pass
+ elif 'r' in prefixes:
+ if quote != "'" and not (quote == '"' and "'" in text):
+ yield (start, 'A110 use single quotes for raw string')
+ else:
+ prefix = ''
+ if sys.version_info[0] >= 3:
+ if 'b' in prefixes:
+ prefix = 'b'
else:
- prefix = 'b' if sys.version_info[0] >= 3 else 'u'
- if prefix not in prefixes:
- prefix = ''
+ u_literals = checker_state.get('has_unicode_literals')
+ if 'u' in prefixes or u_literals and 'b' not in prefixes:
+ prefix = 'u'
- literal = '{0}{1}{2}{1}'.format(prefix, quote, text)
- if ascii(eval(literal)) != literal:
- yield (start, "A110 string literal doesn't match "
- '{}()'.format(ascii.__name__))
+ literal = '{0}{1}{2}{1}'.format(prefix, quote, text)
+ if ascii(eval(literal)) != literal:
+ yield (start, "A110 string literal doesn't match "
+ '{}()'.format(ascii.__name__))
first_token = False
check_quotes.name = 'abp-quotes'
check_quotes.version = __version__
def check_redundant_parenthesis(logical_line, tokens):
« no previous file with comments | « flake8-abp/README.md ('k') | flake8-abp/tests/A109.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld