From d1dae85ac7ee1d2471542e9b85c690ecf92fe446 Mon Sep 17 00:00:00 2001 From: Andreas Jaeger Date: Mon, 30 Mar 2020 08:09:44 +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: Ibe28b6b6bf3927e80816f0c5f1cb972fb6e2374b --- murano/engine/system/workflowclient.py | 2 +- murano/hacking/checks.py | 14 +++++++------- murano/policy/model_policy_enforcer.py | 2 +- .../unit/engine/system/test_workflowclient.py | 2 +- murano/tests/unit/test_hacking.py | 8 -------- test-requirements.txt | 2 +- tox.ini | 8 +++++++- 7 files changed, 18 insertions(+), 20 deletions(-) diff --git a/murano/engine/system/workflowclient.py b/murano/engine/system/workflowclient.py index b300ec28b..bc9f4fbc5 100644 --- a/murano/engine/system/workflowclient.py +++ b/murano/engine/system/workflowclient.py @@ -19,7 +19,7 @@ import json import eventlet try: from mistralclient.api import client as mistralcli -except ImportError as mistral_import_error: +except ImportError: mistralcli = None from oslo_config import cfg from oslo_log import log as logging diff --git a/murano/hacking/checks.py b/murano/hacking/checks.py index 2603c246a..f64909d90 100644 --- a/murano/hacking/checks.py +++ b/murano/hacking/checks.py @@ -13,7 +13,6 @@ # License for the specific language governing permissions and limitations # under the License. -import re """ Guidelines for writing new hacking checks @@ -28,6 +27,10 @@ Guidelines for writing new hacking checks - Add test cases for each new rule to /tests/unit/test_hacking.py """ +import re + +from hacking import core + mutable_default_args = re.compile(r"^\s*def .+\((.+=\{\}|.+=\[\])") assert_equal_end_with_none_re = re.compile( r"(.)*assertEqual\((\w|\.|\'|\"|\[|\])+, None\)") @@ -35,6 +38,7 @@ assert_equal_start_with_none_re = re.compile( r"(.)*assertEqual\(None, (\w|\.|\'|\"|\[|\])+\)") +@core.flake8ext def assert_equal_none(logical_line): """Check for assertEqual(A, None) or assertEqual(None, A) sentences @@ -48,20 +52,16 @@ def assert_equal_none(logical_line): yield (0, msg) +@core.flake8ext def no_mutable_default_args(logical_line): msg = "M322: Method's default argument shouldn't be mutable!" if mutable_default_args.match(logical_line): yield (0, msg) +@core.flake8ext def check_no_basestring(logical_line): if re.search(r"\bbasestring\b", logical_line): msg = ("M326: basestring is not Python3-compatible, use " "six.string_types instead.") yield(0, msg) - - -def factory(register): - register(assert_equal_none) - register(no_mutable_default_args) - register(check_no_basestring) diff --git a/murano/policy/model_policy_enforcer.py b/murano/policy/model_policy_enforcer.py index c29538d2d..7aff66844 100644 --- a/murano/policy/model_policy_enforcer.py +++ b/murano/policy/model_policy_enforcer.py @@ -18,7 +18,7 @@ import re try: # integration with congress is optional import congressclient.v1.client as congress_client -except ImportError as congress_client_import_error: +except ImportError: congress_client = None from oslo_log import log as logging diff --git a/murano/tests/unit/engine/system/test_workflowclient.py b/murano/tests/unit/engine/system/test_workflowclient.py index 27b6d6727..e0306219f 100644 --- a/murano/tests/unit/engine/system/test_workflowclient.py +++ b/murano/tests/unit/engine/system/test_workflowclient.py @@ -18,7 +18,7 @@ import random try: from mistralclient.api import client as mistralcli -except ImportError as mistral_import_error: +except ImportError: mistralcli = None import mock diff --git a/murano/tests/unit/test_hacking.py b/murano/tests/unit/test_hacking.py index 1abe9cee1..4756c938a 100644 --- a/murano/tests/unit/test_hacking.py +++ b/murano/tests/unit/test_hacking.py @@ -104,11 +104,3 @@ class HackingTestCase(base.MuranoTestCase): self.assertEqual(0, len(list(checks.check_no_basestring( "isinstance('foo', six.string_types)")))) - - def test_factory(self): - mock_register = mock.MagicMock() - checks.factory(mock_register) - expected = [ - mock.call(checks.no_mutable_default_args), - mock.call(checks.check_no_basestring)] - mock_register.assert_has_calls(expected) diff --git a/test-requirements.txt b/test-requirements.txt index 80d6fdbae..12a710245 100644 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -1,7 +1,7 @@ # The order of packages is significant, because pip processes them in the order # of appearance. Changing the order has an impact on the overall integration # process, which may cause wedges in the gate later. -hacking>=1.1.0,<1.2.0 # Apache-2.0 +hacking>=3.0,<4.0.0 # Apache-2.0 coverage!=4.4,>=4.0 # Apache-2.0 fixtures>=3.0.0 # Apache-2.0/BSD diff --git a/tox.ini b/tox.ini index b6e93a9b8..7480a7af2 100644 --- a/tox.ini +++ b/tox.ini @@ -99,10 +99,16 @@ ignore = W605,W504,W503,E123,H405 exclude=.venv,.git,.tox,dist,doc,*lib/python*,*egg,build [hacking] -local-check-factory = murano.hacking.checks.factory import_exceptions = oslo.db.sqlalchemy.test_base, murano.common.i18n +[flake8:local-plugins] +extension = + M318 = checks:assert_equal_none + M322 = checks:no_mutable_default_args + M326 = checks:check_no_basestring +paths = ./murano/hacking + [testenv:bindep] # Do not install any requirements. We want this to be fast and work even if # system dependencies are missing, since it's used to tell you what system