diff options
-rw-r--r-- | ChangeLog | 3 | ||||
-rw-r--r-- | checkers/python3.py | 22 | ||||
-rw-r--r-- | test/unittest_checker_python3.py | 7 |
3 files changed, 29 insertions, 3 deletions
@@ -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 21bc9230b..5c66f7682 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 cf408f806..b48a76d8b 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() |