Use six.string_types instead of basestring

There is no global variable "basestring" in Python 3.
"six.string_types" should be used to support both Python 2 and Python 3

Change-Id: I556fef0e98d00ab819e3dea961666b403b927b06
Closes-bug:#1403077
This commit is contained in:
li,chen 2014-12-23 10:54:02 +08:00
parent 3ba9ca2833
commit 352df6d6af
4 changed files with 30 additions and 8 deletions

View File

@ -23,3 +23,4 @@ Rally Specific Commandments
* [N324] - Ensure that ``assertEqual(A in/not in B, True/False)`` and ``assertEqual(True/False, A in/not in B)`` are not used with collection contents
* [N33x] - Reserved for rules related to Python 3 compatibility
* [N330] - Ensure that ``dict.iteritems()`` is not used
* [N331] - Ensure that ``basestring`` is not used

View File

@ -46,6 +46,7 @@ re_assert_equal_in_end_with_true_or_false = re.compile(
re_assert_equal_in_start_with_true_or_false = re.compile(
r"assertEqual\((True|False), (\w|[][.'\"])+( not)? in (\w|[][.'\", ])+\)")
re_iteritems_method = re.compile(r"\.iteritems\(\)")
re_basestring_method = re.compile(r"(^|[\s,(\[=])basestring([\s,)\]]|$)")
def _parse_assert_mock_str(line):
@ -232,13 +233,25 @@ def check_iteritems_method(logical_line):
N330
"""
res = re_iteritems_method.search(logical_line)
if res:
yield (0, "N330: Use six.iteritems(dict) or dict.items() rather than "
"dict.iteritems() to iterate a collection.")
def check_basestring_method(logical_line):
"""Check if basestring is properly called for compatibility with Python 3
There is no global variable "basestring" in Python 3.The correct form
is six.string_types, instead of basestring.
N331
"""
res = re_basestring_method.search(logical_line)
if res:
yield (0, "N331: Use six.string_types rather than basestring.")
def factory(register):
register(check_assert_methods_from_mock)
register(check_import_of_logging)
@ -250,3 +263,4 @@ def factory(register):
register(assert_true_or_false_with_in)
register(assert_equal_in)
register(check_iteritems_method)
register(check_basestring_method)

View File

@ -344,7 +344,7 @@ class FakeServerManager(FakeManager):
pass
def delete(self, resource):
if not isinstance(resource, basestring):
if not isinstance(resource, six.string_types):
resource = resource.id
cached = self.get(resource)
@ -383,7 +383,7 @@ class FakeImageManager(FakeManager):
return self._create(name=name)
def delete(self, resource):
if not isinstance(resource, basestring):
if not isinstance(resource, six.string_types):
resource = resource.id
cached = self.get(resource)
@ -440,7 +440,7 @@ class FakeKeypairManager(FakeManager):
return self._cache(kp)
def delete(self, resource):
if not isinstance(resource, basestring):
if not isinstance(resource, six.string_types):
resource = resource.id
cached = self.get(resource)
@ -458,7 +458,7 @@ class FakeStackManager(FakeManager):
return self._cache(stack)
def delete(self, resource):
if not isinstance(resource, basestring):
if not isinstance(resource, six.string_types):
resource = resource.id
cached = self.get(resource)
@ -476,7 +476,7 @@ class FakeDomainManager(FakeManager):
return self._cache(domain)
def delete(self, resource):
if not isinstance(resource, basestring):
if not isinstance(resource, six.string_types):
resource = resource.id
cached = self.get(resource)
@ -533,7 +533,7 @@ class FakeSecurityGroupManager(FakeManager):
raise nova_exceptions.NotFound('Security Group not found')
def delete(self, resource):
if not isinstance(resource, basestring):
if not isinstance(resource, six.string_types):
resource = resource.id
cached = self.get(resource)
@ -765,7 +765,7 @@ class FakeDbInstanceManager(FakeManager):
return self.__db_instances.values()
def delete(self, resource):
if not isinstance(resource, basestring):
if not isinstance(resource, six.string_types):
resource = resource.id
cached = self.get(resource)

View File

@ -128,6 +128,13 @@ class HackingTestCase(test.TestCase):
self.assertEqual(len(list(checks.check_iteritems_method(
"dict.items()"))), 0)
def test_check_basestring_method(self):
self.assertEqual(len(list(checks.check_basestring_method(
"basestring"))), 1)
self.assertEqual(len(list(checks.check_basestring_method(
"six.string_types"))), 0)
def test_assert_equal_none(self):
self.assertEqual(len(list(checks.assert_equal_none(
"self.assertEqual(A, None)"))), 1)