From ebd565cee04ba23a2eef54e2bedf6fa7be1bfce8 Mon Sep 17 00:00:00 2001 From: chen-li Date: Wed, 27 Jan 2016 17:14:37 +0800 Subject: [PATCH] Use new-style Python classes There are some classes in the code that didn't inherited from nothing and this is an old-style classes. A "New Class" is the recommended way to create a class in modern Python.A "New Class" should always inherit from `object` or another new-style class. Hacking rule added as well. Change-Id: I2c72f20695e35e05435d2526d4a804196c2ab2d3 --- tests/hacking/README.rst | 1 + tests/hacking/checks.py | 15 +++++++++++++++ tests/unit/test_hacking.py | 9 +++++++++ 3 files changed, 25 insertions(+) diff --git a/tests/hacking/README.rst b/tests/hacking/README.rst index d1a4fa73..e99dbee4 100644 --- a/tests/hacking/README.rst +++ b/tests/hacking/README.rst @@ -28,6 +28,7 @@ Rally Specific Commandments * [N352] - Ensure that string formatting only uses a mapping if multiple mapping keys are used. * [N353] - Ensure that unicode() function is not uset because of absence in py3 * [N354] - Ensure that ``:raises: Exception`` is not used +* [N355] - Ensure that we use only "new-style" Python classes * [N360-N370] - Reserved for rules related to CLI * [N360] - Ensure that CLI modules do not use ``rally.common.db`` * [N361] - Ensure that CLI modules do not use ``rally.common.objects`` diff --git a/tests/hacking/checks.py b/tests/hacking/checks.py index 01c9677d..f67cb268 100644 --- a/tests/hacking/checks.py +++ b/tests/hacking/checks.py @@ -63,6 +63,7 @@ re_raises = re.compile( r"\s:raise[^s] *.*$|\s:raises *:.*$|\s:raises *[^:]+$") re_db_import = re.compile(r"^from rally.common import db") re_objects_import = re.compile(r"^from rally.common import objects") +re_old_type_class = re.compile(r"^\s*class \w+(\(\))?:") def skip_ignored_lines(func): @@ -475,6 +476,19 @@ def check_raises(physical_line, filename): "in docstrings.") +@skip_ignored_lines +def check_old_type_class(logical_line, physical_line, filename): + """Use new-style Python classes + + N355 + """ + + if re_old_type_class.search(logical_line): + yield (0, "N355 This class does not inherit from anything and thus " + "will be an old-style class by default. Try to inherit from " + "``object`` or another new-style class.") + + @skip_ignored_lines def check_db_imports_in_cli(logical_line, physical_line, filename): """Ensure that CLI modules do not use ``rally.common.db`` @@ -522,3 +536,4 @@ def factory(register): register(check_raises) register(check_db_imports_in_cli) register(check_objects_imports_in_cli) + register(check_old_type_class) diff --git a/tests/unit/test_hacking.py b/tests/unit/test_hacking.py index c6079883..d8dec906 100644 --- a/tests/unit/test_hacking.py +++ b/tests/unit/test_hacking.py @@ -367,3 +367,12 @@ class HackingTestCase(test.TestCase): checkres = checks.check_objects_imports_in_cli( line, line, "./filename") self.assertRaises(StopIteration, next, checkres) + + @ddt.data( + "class Oldstype():", + "class Oldstyle:" + ) + def test_check_old_type_class(self, line): + checkres = checks.check_old_type_class(line, line, "fakefile") + self.assertIsNotNone(next(checkres)) + self.assertEqual([], list(checkres))