summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorClaudiu Popa <cpopa@cloudbasesolutions.com>2014-11-17 17:02:19 +0200
committerClaudiu Popa <cpopa@cloudbasesolutions.com>2014-11-17 17:02:19 +0200
commit95a68a38c0b3a99ccdae089480ce4977d7f0c9dc (patch)
tree9f7d8067accb5a1d87512e8e875b54220531eebb
parent712cc928a1915b253cd8f551b9c8095983559f02 (diff)
downloadpylint-95a68a38c0b3a99ccdae089480ce4977d7f0c9dc.tar.gz
Add 'old-octal-literal' to Python 3 porting checker.
This warning is emitted when encountering octals with the old syntax, in the form '0number'.
-rw-r--r--ChangeLog3
-rw-r--r--checkers/python3.py22
-rw-r--r--test/unittest_checker_python3.py7
3 files changed, 29 insertions, 3 deletions
diff --git a/ChangeLog b/ChangeLog
index a7f3901..f42ff32 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -179,6 +179,9 @@ ChangeLog for Pylint
mode. This mode will emit warnings and errors for constructs
invalid or removed in Python 3, helping the users of Pylint
to port their projects.
+
+ * Add 'old-octal-literal' to Python 3 porting checker, emitted when
+ encountering octals with the old syntax.
diff --git a/checkers/python3.py b/checkers/python3.py
index 21bc923..5c66f76 100644
--- a/checkers/python3.py
+++ b/checkers/python3.py
@@ -14,6 +14,7 @@
"""Check Python 2 code for Python 2/3 source-compatible issues."""
from __future__ import absolute_import
+import re
import tokenize
import astroid
@@ -21,6 +22,12 @@ from pylint import checkers, interfaces
from pylint.utils import WarningScope
from pylint.checkers import utils
+
+_OLD_OCTAL = re.compile("\d{2}")
+
+def _is_old_octal(literal):
+ return _OLD_OCTAL.match(literal)
+
def _check_dict_node(node):
inferred = node.infer()
inferred_types = set()
@@ -384,13 +391,22 @@ class Python3TokenChecker(checkers.BaseTokenChecker):
'of "!=". This is removed in Python 3.',
{'maxversion': (3, 0),
'old_names': [('W0331', 'old-ne-operator')]}),
+ 'E1608': ('Use of old octal literal',
+ 'old-octal-literal',
+ 'Usen when encountering the old octal syntax, '
+ 'removed in Python 3. To use the new syntax, '
+ 'prepend 0o on the number.',
+ {'maxversion': (3, 0)}),
}
def process_tokens(self, tokens):
for idx, (tok_type, token, start, _, _) in enumerate(tokens):
- if tok_type == tokenize.NUMBER and token.lower().endswith('l'):
- # This has a different semantic than lowercase-l-suffix.
- self.add_message('long-suffix', line=start[0])
+ if tok_type == tokenize.NUMBER:
+ if token.lower().endswith('l'):
+ # This has a different semantic than lowercase-l-suffix.
+ self.add_message('long-suffix', line=start[0])
+ elif _is_old_octal(token):
+ self.add_message('old-octal-literal', line=start[0])
if tokens[idx][1] == '<>':
self.add_message('old-ne-operator', line=tokens[idx][2][0])
diff --git a/test/unittest_checker_python3.py b/test/unittest_checker_python3.py
index cf408f8..b48a76d 100644
--- a/test/unittest_checker_python3.py
+++ b/test/unittest_checker_python3.py
@@ -309,6 +309,13 @@ class Python3TokenCheckerTest(testutils.CheckerTestCase):
with self.assertAddsMessages(message):
self.checker.process_tokens(tokens)
+ @python2_only
+ def test_old_octal_literal(self):
+ tokens = testutils.tokenize_str("045")
+ message = testutils.Message('old-octal-literal', line=1)
+ with self.assertAddsMessages(message):
+ self.checker.process_tokens(tokens)
+
if __name__ == '__main__':
unittest.main()