Add hacking for code style checks

The following style checks are added:
 * do not use dict.iteritems
 * do not use dict.iterkeys
 * do not use dict.itervalues

Partial_implements: blueprint mistral-hacking

Change-Id: I25d846c80652d7746c3b958239797ce524ec235f
This commit is contained in:
Sharat Sharma 2017-02-17 17:03:27 +05:30
parent 6e5cbd3b5a
commit e986cb8542
4 changed files with 52 additions and 1 deletions

View File

@ -10,3 +10,6 @@ Mistral Specific Commandments
like assertIsNone(A)
- [M327] Do not use xrange(). xrange() is not compatible with Python 3. Use
range() or six.moves.range() instead.
- [M328] Python 3: do not use dict.iteritems.
- [M329] Python 3: do not use dict.iterkeys.
- [M330] Python 3: do not use dict.itervalues.

View File

@ -73,6 +73,27 @@ def check_python3_xrange(logical_line):
"with Python 3. Use range() or six.moves.range() instead.")
def check_python3_no_iteritems(logical_line):
msg = ("M328: Use six.iteritems() instead of dict.iteritems().")
if re.search(r".*\.iteritems\(\)", logical_line):
yield(0, msg)
def check_python3_no_iterkeys(logical_line):
msg = ("M329: Use six.iterkeys() instead of dict.iterkeys().")
if re.search(r".*\.iterkeys\(\)", logical_line):
yield(0, msg)
def check_python3_no_itervalues(logical_line):
msg = ("M330: Use six.itervalues() instead of dict.itervalues().")
if re.search(r".*\.itervalues\(\)", logical_line):
yield(0, msg)
class BaseASTChecker(ast.NodeVisitor):
"""Provides a simple framework for writing AST-based checks.
@ -236,3 +257,7 @@ def factory(register):
register(assert_equal_none)
register(check_oslo_namespace_imports)
register(CheckForLoggingIssues)
register(check_python3_no_iteritems)
register(check_python3_no_iterkeys)
register(check_python3_no_itervalues)
register(check_python3_xrange)

View File

@ -76,6 +76,27 @@ class BaseLoggingCheckTest(base.BaseTest):
self.assertEqual(0, len(list(func('for i in range(10)'))))
self.assertEqual(0, len(list(func('for i in six.moves.range(10)'))))
def test_dict_iteritems(self):
self.assertEqual(1, len(list(checks.check_python3_no_iteritems(
"obj.iteritems()"))))
self.assertEqual(0, len(list(checks.check_python3_no_iteritems(
"six.iteritems(ob))"))))
def test_dict_iterkeys(self):
self.assertEqual(1, len(list(checks.check_python3_no_iterkeys(
"obj.iterkeys()"))))
self.assertEqual(0, len(list(checks.check_python3_no_iterkeys(
"six.iterkeys(ob))"))))
def test_dict_itervalues(self):
self.assertEqual(1, len(list(checks.check_python3_no_itervalues(
"obj.itervalues()"))))
self.assertEqual(0, len(list(checks.check_python3_no_itervalues(
"six.itervalues(ob))"))))
class TestLoggingWithWarn(BaseLoggingCheckTest):

View File

@ -12,6 +12,8 @@
# License for the specific language governing permissions and limitations
# under the License.
import six
from oslo_concurrency.fixture import lockutils
from tempest import test
@ -77,7 +79,7 @@ class TaskTypesTestsV2(base.TestCase):
self.assertEqual(200, resp.status)
bt = body['tasks']
ll = [[v for k, v in d.iteritems() if 'type' in k] for d in bt]
ll = [[v for k, v in six.iteritems(d) if 'type' in k] for d in bt]
types_list = [item for sublist in ll for item in sublist]
self.assertIn(