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: removed 'r' prefix check Created May 26, 2016, 4:12 p.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
Index: flake8-abp/flake8_abp.py
===================================================================
--- a/flake8-abp/flake8_abp.py
+++ b/flake8-abp/flake8_abp.py
@@ -39,16 +39,18 @@
}
ESSENTIAL_BUILTINS = set(dir(builtins)) - {'apply', 'buffer', 'coerce',
'intern', 'file'}
LEAVE_BLOCK = (ast.Return, ast.Raise, ast.Continue, ast.Break)
VOLATILE = object()
+is_unicode_literals = False
+
def evaluate(node):
try:
return eval(compile(ast.Expression(node), '', 'eval'), {})
except Exception:
return VOLATILE
@@ -372,44 +374,59 @@
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):
first_token = True
+ global is_unicode_literals
+
+ # check if this is beginning of file
+ if tokens[0][3][0] == 1:
+ is_unicode_literals = False
+
+ # check if in unicode_literals mode
+ token_strings = [t[1] for t in tokens]
+ if token_strings[:3] == ['from', '__future__', 'import']:
+ if 'unicode_literals' in token_strings:
+ is_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'^(u)?(b)?(r)?((""")?.*)$',
token, re.IGNORECASE | re.DOTALL)
(is_unicode, is_bytes, is_raw,
literal, has_doc_quotes) = match.groups()
if first_token and re.search(r'^(?:(?:def|class)\s|$)',
previous_logical):
if not has_doc_quotes:
yield (start, 'A109 use triple double '
'quotes for docstrings')
- elif is_unicode or is_bytes or is_raw:
- yield (start, "A109 don't use u'', b'' "
- "or r'' for doc strings")
+ if is_unicode and not is_unicode_literals:
Vasily Kuznetsov 2016/05/26 19:29:44 Actually if is_unicode_literals is True, this erro
+ yield (start, 'A112 use "from __future__ import'
+ 'unicode_literals" instead of '
+ 'prefixing literals with "u"')
elif start[0] == end[0]:
if is_raw:
literal = re.sub(r'\\(?!{})'.format(literal[0]),
'\\\\\\\\', literal)
-
+ if is_unicode and not is_unicode_literals:
+ yield (start, 'A112 use "from __future__ import'
+ 'unicode_literals" instead of '
+ 'prefixing literals with "u"')
if sys.version_info[0] >= 3:
if is_bytes:
literal = 'b' + literal
- elif is_unicode:
+ elif not is_bytes:
literal = 'u' + literal
if ascii(eval(literal)) != literal:
yield (start, "A110 string literal doesn't match "
'{}()'.format(ascii.__name__))
first_token = False
« flake8-abp/README.md ('K') | « flake8-abp/README.md ('k') | flake8-abp/tests/A109.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld