summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTorsten Marek <shlomme@gmail.com>2014-04-18 18:44:53 +0200
committerTorsten Marek <shlomme@gmail.com>2014-04-18 18:44:53 +0200
commit2e152035e8e2c56e2461d3f694ebee3713698767 (patch)
tree68b52d9b09ce704bf09a2b9412751abcf14b81be
parent14c199bb7ee187e67b3dac216e71695c5090604e (diff)
downloadpylint-2e152035e8e2c56e2461d3f694ebee3713698767.tar.gz
Do not warn about \u escapes in string literals when Unicode literals
are used for Python 2.*. Fixes BitBucket issue #151.
-rw-r--r--ChangeLog3
-rw-r--r--checkers/strings.py9
-rw-r--r--test/input/func_unicode_literal_py_30.py7
-rw-r--r--test/messages/func_unicode_literal_py_30.txt1
4 files changed, 17 insertions, 3 deletions
diff --git a/ChangeLog b/ChangeLog
index d57133d..371b0ba 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -11,6 +11,9 @@ ChangeLog for Pylint
* Added a new warning for closing over variables that are
defined in loops. Fixes Bitbucket issue #176.
+ * Do not warn about \u escapes in string literals when Unicode literals
+ are used for Python 2.*. Fixes BitBucket issue #151.
+
* Extend the checking for unbalanced-tuple-unpacking and
unpacking-non-sequence to instance attribute unpacking as well.
diff --git a/checkers/strings.py b/checkers/strings.py
index 663d61d..04cf1bc 100644
--- a/checkers/strings.py
+++ b/checkers/strings.py
@@ -23,7 +23,7 @@ import tokenize
import astroid
-from pylint.interfaces import ITokenChecker, IAstroidChecker
+from pylint.interfaces import ITokenChecker, IAstroidChecker, IRawChecker
from pylint.checkers import BaseChecker, BaseTokenChecker
from pylint.checkers import utils
from pylint.checkers.utils import check_messages
@@ -195,7 +195,7 @@ class StringMethodsChecker(BaseChecker):
class StringConstantChecker(BaseTokenChecker):
"""Check string literals"""
- __implements__ = (ITokenChecker,)
+ __implements__ = (ITokenChecker, IRawChecker)
name = 'string_constant'
msgs = {
'W1401': ('Anomalous backslash in string: \'%s\'. '
@@ -221,6 +221,9 @@ class StringConstantChecker(BaseTokenChecker):
# Unicode strings.
UNICODE_ESCAPE_CHARACTERS = 'uUN'
+ def process_module(self, module):
+ self._unicode_literals = 'unicode_literals' in module.future_imports
+
def process_tokens(self, tokens):
for (tok_type, token, (start_row, start_col), _, _) in tokens:
if tok_type == tokenize.STRING:
@@ -279,7 +282,7 @@ class StringConstantChecker(BaseTokenChecker):
if next_char in self.UNICODE_ESCAPE_CHARACTERS:
if 'u' in prefix:
pass
- elif _PY3K and 'b' not in prefix:
+ elif (_PY3K or self._unicode_literals) and 'b' not in prefix:
pass # unicode by default
else:
self.add_message('anomalous-unicode-escape-in-string',
diff --git a/test/input/func_unicode_literal_py_30.py b/test/input/func_unicode_literal_py_30.py
new file mode 100644
index 0000000..fa47902
--- /dev/null
+++ b/test/input/func_unicode_literal_py_30.py
@@ -0,0 +1,7 @@
+"""Unicode literals in Python 2.*"""
+from __future__ import unicode_literals
+
+__revision__ = 0
+
+BAD_STRING = b'\u1234'
+GOOD_STRING = '\u1234'
diff --git a/test/messages/func_unicode_literal_py_30.txt b/test/messages/func_unicode_literal_py_30.txt
new file mode 100644
index 0000000..25efa99
--- /dev/null
+++ b/test/messages/func_unicode_literal_py_30.txt
@@ -0,0 +1 @@
+W: 6: Anomalous Unicode escape in byte string: '\u'. String constant might be missing an r or u prefix.