Enforce style check for xrange()

The following check is added in this commit:
* hacking checks for xrange()

Change-Id: If7bd8759445e5ca8cd5f4e74d91dcd02fa267904
Partial-Implements: blueprint mistral-hacking
This commit is contained in:
xpress 2017-02-02 17:54:40 +05:30 committed by Sharat Sharma
parent b635dcc79e
commit 15d8ac5092
3 changed files with 15 additions and 0 deletions

View File

@ -8,3 +8,5 @@ Mistral Specific Commandments
- [M318] Change assertEqual(A, None) or assertEqual(None, A) by optimal assert
like assertIsNone(A)
- [M327] Do not use xrange(). xrange() is not compatible with Python 3. Use
range() or six.moves.range() instead.

View File

@ -67,6 +67,12 @@ def check_oslo_namespace_imports(logical_line):
yield(0, msg)
def check_python3_xrange(logical_line):
if re.search(r"\bxrange\s*\(", logical_line):
yield(0, "M327: Do not use xrange(). 'xrange()' is not compatible "
"with Python 3. Use range() or six.moves.range() instead.")
class BaseASTChecker(ast.NodeVisitor):
"""Provides a simple framework for writing AST-based checks.

View File

@ -69,6 +69,13 @@ class BaseLoggingCheckTest(base.BaseTest):
self.assertEqual(
len(list(checks.assert_equal_none("self.assertIsNone()"))), 0)
def test_check_python3_xrange(self):
func = checks.check_python3_xrange
self.assertEqual(1, len(list(func('for i in xrange(10)'))))
self.assertEqual(1, len(list(func('for i in xrange (10)'))))
self.assertEqual(0, len(list(func('for i in range(10)'))))
self.assertEqual(0, len(list(func('for i in six.moves.range(10)'))))
class TestLoggingWithWarn(BaseLoggingCheckTest):