Merge "Adding dict.items() concatenate validation"

This commit is contained in:
Jenkins 2015-01-15 13:44:42 +00:00 committed by Gerrit Code Review
commit 1ef6fcd472
3 changed files with 46 additions and 0 deletions

View File

@ -30,4 +30,5 @@ Rally Specific Commandments
* [N335] - Ensure that ``xrange`` is not used
* [N336] - Ensure that ``string.lowercase`` and ``string.uppercase`` are not used
* [N337] - Ensure that ``next()`` method on iterator objects is not used
* [N338] - Ensure that ``+`` operand is not used to concatenate dict.items()
* [N340] - Ensure that we are importing always ``from rally import objects``

View File

@ -53,6 +53,8 @@ re_xrange_method = re.compile(r"(^|[\s=])xrange\(")
re_string_lower_upper_case_method = re.compile(
r"(^|[(,\s=])string\.(lower|upper)case([)\[,\s]|$)")
re_next_on_iterator_method = re.compile(r"\.next\(\)")
re_concatenate_dict = re.compile(
r".*\.items\(\)(\s*\+\s*.*\.items\(\))+.*")
def _parse_assert_mock_str(line):
@ -345,6 +347,21 @@ def check_next_on_iterator_method(logical_line):
yield (0, "N337: Use next(iterator) rather than iterator.next().")
def check_concatenate_dict_with_plus_operand(logical_line):
"""Check if a dict is being sum with + operator
Python 3 dict.items() return a dict_items object instead of a list, and
this object, doesn't support + operator. Need to use the update method
instead in order to concatenate two dictionaries.
N338
"""
res = re_concatenate_dict.search(logical_line)
if res:
yield (0, "N338: Use update() method instead of '+'' operand to "
"concatenate dictionaries")
def check_no_direct_rally_objects_import(logical_line, filename):
"""Check if rally.objects are properly imported.
@ -381,3 +398,4 @@ def factory(register):
register(check_string_lower_upper_case_method)
register(check_next_on_iterator_method)
register(check_no_direct_rally_objects_import)
register(check_concatenate_dict_with_plus_operand)

View File

@ -128,6 +128,33 @@ class HackingTestCase(test.TestCase):
self.assertEqual(len(list(checks.check_iteritems_method(
"dict.items()"))), 0)
def test_check_concatenate_dict_with_plus_operand(self):
self.assertEqual(len(list(
checks.check_concatenate_dict_with_plus_operand(
"dict1.items() + dict2.items()"))), 1)
self.assertEqual(len(list(
checks.check_concatenate_dict_with_plus_operand(
"dict(self.endpoint.to_dict().items() + "
"endpoint.items())"))), 1)
self.assertEqual(len(list(
checks.check_concatenate_dict_with_plus_operand(
"dict(self.endpoint.to_dict().items() + "
"endpoint.items() + something.items())"))), 1)
self.assertEqual(len(list(
checks.check_concatenate_dict_with_plus_operand(
"dict1.item() + dict2.item()"))), 0)
self.assertEqual(len(list(
checks.check_concatenate_dict_with_plus_operand(
"obj.getitems() + obj.getitems()"))), 0)
self.assertEqual(len(list(
checks.check_concatenate_dict_with_plus_operand(
"obj.getitems() + dict2.items()"))), 0)
def test_check_basestring_method(self):
self.assertEqual(len(list(checks.check_basestring_method(
"basestring"))), 1)