Browse Source

Enforce using six.text_type() over unicode()

For python 3 compatibility, this adds a hacking check to prevent the
usage of unicode().

Change-Id: I791bbf251ac0f9615a9bedc2dd295a7982d72e0a
Closes-bug: #1282893
changes/03/122203/2
Louis Taylor 8 years ago
parent
commit
70beb11924
  1. 1
      HACKING.rst
  2. 11
      glance/hacking/checks.py
  3. 11
      glance/tests/test_hacking.py

1
HACKING.rst

@ -15,3 +15,4 @@ glance Specific Commandments
- [G318] Change assertEqual(A, None) or assertEqual(None, A) by optimal assert like
assertIsNone(A)
- [G319] Validate that debug level logs are not translated
- [G320] For python 3 compatibility, use six.text_type() instead of unicode()

11
glance/hacking/checks.py

@ -40,6 +40,7 @@ asse_equal_end_with_none_re = re.compile(
r"(.)*assertEqual\((\w|\.|\'|\"|\[|\])+, None\)")
asse_equal_start_with_none_re = re.compile(
r"(.)*assertEqual\(None, (\w|\.|\'|\"|\[|\])+\)")
unicode_func_re = re.compile(r"(\s|\W|^)unicode\(")
def assert_true_instance(logical_line):
@ -91,8 +92,18 @@ def no_translate_debug_logs(logical_line, filename):
yield(0, "G319: Don't translate debug level logs")
def no_direct_use_of_unicode_function(logical_line):
"""Check for use of unicode() builtin
G320
"""
if unicode_func_re.match(logical_line):
yield(0, "G320: Use six.text_type() instead of unicode()")
def factory(register):
register(assert_true_instance)
register(assert_equal_type)
register(assert_equal_none)
register(no_translate_debug_logs)
register(no_direct_use_of_unicode_function)

11
glance/tests/test_hacking.py

@ -51,3 +51,14 @@ class HackingTestCase(utils.BaseTestCase):
self.assertEqual(0, len(list(checks.no_translate_debug_logs(
"LOG.info(_('foo'))", "glance/store/foo.py"))))
def test_no_direct_use_of_unicode_function(self):
self.assertEqual(1, len(list(checks.no_direct_use_of_unicode_function(
"unicode('the party dont start til the unicode walks in')"))))
self.assertEqual(1, len(list(checks.no_direct_use_of_unicode_function(
"""unicode('something '
'something else"""))))
self.assertEqual(0, len(list(checks.no_direct_use_of_unicode_function(
"six.text_type('party over')"))))
self.assertEqual(0, len(list(checks.no_direct_use_of_unicode_function(
"not_actually_unicode('something completely different')"))))

Loading…
Cancel
Save