From 7b52b7caa5ed7a9909b8ee4867e9284f01accb5b Mon Sep 17 00:00:00 2001 From: Andreas Jaeger Date: Sun, 29 Mar 2020 17:29:47 +0200 Subject: [PATCH] Update hacking for Python3 The repo is Python 3 now, so update hacking to version 3.0 which supports Python 3. Fix problems found. Update local hacking checks for new flake8. Change-Id: If11b38493aacc7b888a6b64bf50c1a2782efd9ae --- octavia_lib/hacking/checks.py | 38 +++++++++---------- .../unit/api/drivers/test_data_models.py | 12 +++--- octavia_lib/tests/unit/hacking/test_checks.py | 14 ------- test-requirements.txt | 2 +- tox.ini | 22 ++++++++++- 5 files changed, 46 insertions(+), 42 deletions(-) diff --git a/octavia_lib/hacking/checks.py b/octavia_lib/hacking/checks.py index 26f9214..a57df5a 100644 --- a/octavia_lib/hacking/checks.py +++ b/octavia_lib/hacking/checks.py @@ -12,8 +12,6 @@ # License for the specific language governing permissions and limitations # under the License. -import re - """ Guidelines for writing new hacking checks @@ -30,6 +28,11 @@ Guidelines for writing new hacking checks """ + +import re + +from hacking import core + _all_log_levels = {'critical', 'error', 'exception', 'info', 'warning'} _all_hints = {'_LC', '_LE', '_LI', '_', '_LW'} @@ -41,7 +44,7 @@ _log_translation_hint = re.compile( assert_trueinst_re = re.compile( r"(.)*assertTrue\(isinstance\((\w|\.|\'|\"|\[|\])+, " - "(\w|\.|\'|\"|\[|\])+\)\)") + r"(\w|\.|\'|\"|\[|\])+\)\)") assert_equal_in_end_with_true_or_false_re = re.compile( r"assertEqual\((\w|[][.'\"])+ in (\w|[][.'\", ])+, (True|False)\)") assert_equal_in_start_with_true_or_false_re = re.compile( @@ -110,6 +113,7 @@ def _translation_checks_not_enforced(filename): return any(pat in filename for pat in ["/tests/", "rally-jobs/plugins/"]) +@core.flake8ext def assert_true_instance(logical_line): """Check for assertTrue(isinstance(a, b)) sentences @@ -120,6 +124,7 @@ def assert_true_instance(logical_line): "Use assertIsInstance instead.") +@core.flake8ext def assert_equal_or_not_none(logical_line): """Check for assertEqual(A, None) or assertEqual(None, A) sentences, @@ -137,6 +142,7 @@ def assert_equal_or_not_none(logical_line): yield (0, msg) +@core.flake8ext def assert_equal_true_or_false(logical_line): """Check for assertEqual(True, A) or assertEqual(False, A) sentences @@ -149,12 +155,14 @@ def assert_equal_true_or_false(logical_line): "sentences not allowed") +@core.flake8ext def no_mutable_default_args(logical_line): msg = "O324: Method's default argument shouldn't be mutable!" if mutable_default_args.match(logical_line): yield (0, msg) +@core.flake8ext def assert_equal_in(logical_line): """Check for assertEqual(A in B, True), assertEqual(True, A in B), @@ -170,6 +178,7 @@ def assert_equal_in(logical_line): "contents.") +@core.flake8ext def no_log_warn(logical_line): """Disallow 'LOG.warn(' @@ -179,6 +188,7 @@ def no_log_warn(logical_line): yield(0, "O339:Use LOG.warning() rather than LOG.warn()") +@core.flake8ext def no_translate_logs(logical_line, filename): """O341 - Don't translate logs. @@ -204,6 +214,7 @@ def no_translate_logs(logical_line, filename): yield (logical_line.index(match.group()), msg) +@core.flake8ext def check_raised_localized_exceptions(logical_line, filename): """O342 - Untranslated exception message. @@ -225,6 +236,7 @@ def check_raised_localized_exceptions(logical_line, filename): yield (logical_line.index(exception_msg), msg) +@core.flake8ext def check_no_basestring(logical_line): """O343 - basestring is not Python3-compatible. @@ -239,6 +251,7 @@ def check_no_basestring(logical_line): yield(0, msg) +@core.flake8ext def check_no_eventlet_imports(logical_line): """O345 - Usage of Python eventlet module not allowed. @@ -252,6 +265,7 @@ def check_no_eventlet_imports(logical_line): yield logical_line.index('eventlet'), msg +@core.flake8ext def check_line_continuation_no_backslash(logical_line, tokens): """O346 - Don't use backslashes for line continuation. @@ -273,6 +287,7 @@ def check_line_continuation_no_backslash(logical_line, tokens): yield backslash, msg +@core.flake8ext def check_no_logging_imports(logical_line): """O348 - Usage of Python logging module not allowed. @@ -286,6 +301,7 @@ def check_no_logging_imports(logical_line): yield logical_line.index('logging'), msg +@core.flake8ext def check_no_octavia_namespace_imports(logical_line): """O501 - Direct octavia imports not allowed. @@ -299,19 +315,3 @@ def check_no_octavia_namespace_imports(logical_line): message_override="O501 Direct octavia imports not allowed") if x is not None: yield x - - -def factory(register): - register(assert_true_instance) - register(assert_equal_or_not_none) - register(no_translate_logs) - register(assert_equal_true_or_false) - register(no_mutable_default_args) - register(assert_equal_in) - register(no_log_warn) - register(check_raised_localized_exceptions) - register(check_no_basestring) - register(check_no_eventlet_imports) - register(check_line_continuation_no_backslash) - register(check_no_logging_imports) - register(check_no_octavia_namespace_imports) diff --git a/octavia_lib/tests/unit/api/drivers/test_data_models.py b/octavia_lib/tests/unit/api/drivers/test_data_models.py index d580793..5f9780a 100644 --- a/octavia_lib/tests/unit/api/drivers/test_data_models.py +++ b/octavia_lib/tests/unit/api/drivers/test_data_models.py @@ -306,24 +306,24 @@ class TestProviderDataModels(base.TestCase): def test_equality(self): second_ref_lb = deepcopy(self.ref_lb) - self.assertTrue(self.ref_lb == second_ref_lb) + self.assertEqual(self.ref_lb, second_ref_lb) second_ref_lb.admin_state_up = True - self.assertFalse(self.ref_lb == second_ref_lb) + self.assertNotEqual(self.ref_lb, second_ref_lb) - self.assertFalse(self.ref_lb == self.loadbalancer_id) + self.assertNotEqual(self.ref_lb, self.loadbalancer_id) def test_inequality(self): second_ref_lb = deepcopy(self.ref_lb) - self.assertFalse(self.ref_lb != second_ref_lb) + self.assertEqual(self.ref_lb, second_ref_lb) second_ref_lb.admin_state_up = True - self.assertTrue(self.ref_lb != second_ref_lb) + self.assertNotEqual(self.ref_lb, second_ref_lb) - self.assertTrue(self.ref_lb != self.loadbalancer_id) + self.assertNotEqual(self.ref_lb, self.loadbalancer_id) def test_to_dict(self): ref_lb_converted_to_dict = self.ref_lb.to_dict() diff --git a/octavia_lib/tests/unit/hacking/test_checks.py b/octavia_lib/tests/unit/hacking/test_checks.py index 4c2b147..d2576d5 100644 --- a/octavia_lib/tests/unit/hacking/test_checks.py +++ b/octavia_lib/tests/unit/hacking/test_checks.py @@ -59,20 +59,6 @@ class HackingTestCase(base.BaseTestCase): def assertLineFails(self, func, *args): self.assertIsInstance(next(func(*args)), tuple) - def _get_factory_checks(self, factory): - check_fns = [] - - def _reg(check_fn): - self.assertTrue(hasattr(check_fn, '__call__')) - self.assertFalse(check_fn in check_fns) - check_fns.append(check_fn) - - factory(_reg) - return check_fns - - def test_factory(self): - self.assertTrue(len(self._get_factory_checks(checks.factory)) > 0) - def test_assert_true_instance(self): self.assertEqual(1, len(list(checks.assert_true_instance( "self.assertTrue(isinstance(e, " diff --git a/test-requirements.txt b/test-requirements.txt index 3ca6033..f417486 100644 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -2,7 +2,7 @@ # of appearance. Changing the order has an impact on the overall integration # process, which may cause wedges in the gate later. -hacking!=0.13.0,<0.14,>=0.12.0 # Apache-2.0 +hacking>=3.0,<3.1.0 # Apache-2.0 bandit>=1.1.0 # Apache-2.0 coverage>=4.0,!=4.4 # Apache-2.0 diff --git a/tox.ini b/tox.ini index 1537974..aa2bde4 100644 --- a/tox.ini +++ b/tox.ini @@ -69,7 +69,8 @@ commands = commands = oslo_debug_helper {posargs} [flake8] -ignore = +# W504 line break after binary operator +ignore = W504 show-source = True builtins = _ exclude=.venv,.git,.tox,dist,doc,*lib/python*,*egg,build @@ -82,7 +83,24 @@ enable-extensions=H106,H203,H204,H205,H904 [hacking] import_exceptions = octavia_lib.i18n -local-check-factory = octavia_lib.hacking.checks.factory + +[flake8:local-plugins] +extension = + O316 = checks:assert_true_instance + O318 = checks:assert_equal_or_not_none + O323 = checks:assert_equal_true_or_false + O324 = checks:no_mutable_default_args + O338 = checks:assert_equal_in + O339 = checks:no_log_warn + O341 = checks:no_translate_logs + O342 = checks:check_raised_localized_exceptions + O343 = checks:check_no_basestring + O345 = checks:check_no_eventlet_imports + O346 = checks:check_line_continuation_no_backslash + O348 = checks:check_no_logging_imports + O501 = checks:check_no_octavia_namespace_imports +paths = + ./octavia_lib/hacking [doc8] max-line-length = 79