From 4205f39a0082a291cd4cfb2d89f5d9227b36f690 Mon Sep 17 00:00:00 2001 From: Deeksha Date: Sun, 13 Mar 2016 01:54:31 +0530 Subject: [PATCH] Add hacking check to ensure not use xrange() Added hacking check to ensure not to use xrange. Change-Id: I28731e16cf0636f004bf96795c85eecbdf2f8fbd Closes-Bug: #1538118 --- HACKING.rst | 1 + magnum/hacking/checks.py | 12 ++++++++++++ magnum/tests/unit/test_hacking.py | 10 ++++++++++ 3 files changed, 23 insertions(+) diff --git a/HACKING.rst b/HACKING.rst index d165dd0c69..9a88b7900d 100644 --- a/HACKING.rst +++ b/HACKING.rst @@ -22,3 +22,4 @@ Magnum Specific Commandments - [M336] Must use a dict comprehension instead of a dict constructor with a sequence of key-value pairs. - [M338] Use assertIn/NotIn(A, B) rather than assertEqual(A in B, True/False). +- [M339] Don't use xrange() diff --git a/magnum/hacking/checks.py b/magnum/hacking/checks.py index 375f7b7711..8238a6e797 100644 --- a/magnum/hacking/checks.py +++ b/magnum/hacking/checks.py @@ -49,6 +49,8 @@ assert_true_isinstance_re = re.compile( r"(.)*assertTrue\(isinstance\((\w|\.|\'|\"|\[|\])+, " "(\w|\.|\'|\"|\[|\])+\)\)") dict_constructor_with_list_copy_re = re.compile(r".*\bdict\((\[)?(\(|\[)") +assert_xrange_re = re.compile( + r"\s*xrange\s*\(") def assert_equal_none(logical_line): @@ -112,6 +114,15 @@ def assert_equal_in(logical_line): "contents.") +def no_xrange(logical_line): + """Disallow 'xrange()' + + M339 + """ + if assert_xrange_re.match(logical_line): + yield(0, "M339: Do not use xrange().") + + def use_timeutils_utcnow(logical_line, filename): # tools are OK to use the standard datetime module if "/tools/" in filename: @@ -142,3 +153,4 @@ def factory(register): register(assert_equal_in) register(use_timeutils_utcnow) register(dict_constructor_with_list_copy) + register(no_xrange) diff --git a/magnum/tests/unit/test_hacking.py b/magnum/tests/unit/test_hacking.py index 9836f285ee..76eb07b9b9 100644 --- a/magnum/tests/unit/test_hacking.py +++ b/magnum/tests/unit/test_hacking.py @@ -179,6 +179,16 @@ class HackingTestCase(base.TestCase): code = "self.assertTrue()" self._assert_has_no_errors(code, check) + def test_no_xrange(self): + errors = [(1, 0, "M339")] + check = checks.no_xrange + + code = "xrange(45)" + self._assert_has_errors(code, check, errors) + + code = "range(45)" + self._assert_has_no_errors(code, check) + def test_use_timeunitls_utcow(self): errors = [(1, 0, "M310")] check = checks.use_timeutils_utcnow