From b082d06cbccd06bd49d9aa2a35ea922190e42cc5 Mon Sep 17 00:00:00 2001 From: Stephen Finucane Date: Tue, 5 Apr 2022 11:24:49 +0100 Subject: [PATCH] hacking: Prevent use of six Spotted this in a review recently. We don't want people using six anymore. Change-Id: Ie107a95bc06390ab519d3b3af9b07103a9a14316 Signed-off-by: Stephen Finucane --- nova/hacking/checks.py | 16 ++++++++++++++++ nova/tests/unit/test_hacking.py | 10 ++++++++++ tox.ini | 1 + 3 files changed, 27 insertions(+) diff --git a/nova/hacking/checks.py b/nova/hacking/checks.py index abe6c0ab17af..26c7ba375db7 100644 --- a/nova/hacking/checks.py +++ b/nova/hacking/checks.py @@ -140,6 +140,7 @@ mock_class_as_new_value_in_patching_re = re.compile( rwlock_re = re.compile( r"(?P(oslo_concurrency\.)?(lockutils|fasteners))" r"\.ReaderWriterLock\(.*\)") +six_re = re.compile(r"^(import six(\..*)?|from six(\..*)? import .*)$") class BaseASTChecker(ast.NodeVisitor): @@ -1030,3 +1031,18 @@ def check_lockutils_rwlocks(logical_line): 0, msg % {'module': match.group('module_part')} ) + + +@core.flake8ext +def check_six(logical_line): + """Check for use of six + + nova is now Python 3-only so we don't want six. However, people might use + it out of habit and it will likely work since six is a transitive + dependency. + + N370 + """ + match = re.match(six_re, logical_line) + if match: + yield (0, "N370: Don't use or import six") diff --git a/nova/tests/unit/test_hacking.py b/nova/tests/unit/test_hacking.py index 03b76922170f..e5a6efb0ad0e 100644 --- a/nova/tests/unit/test_hacking.py +++ b/nova/tests/unit/test_hacking.py @@ -1020,3 +1020,13 @@ class HackingTestCase(test.NoDBTestCase): nova_utils.ReaderWriterLock() """ self._assert_has_no_errors(code, checks.check_lockutils_rwlocks) + + def test_check_six(self): + code = """ + import six + from six import moves + from six.moves import range + import six.moves.urllib.parse as urlparse + """ + errors = [(x + 1, 0, 'N370') for x in range(4)] + self._assert_has_errors(code, checks.check_six, expected_errors=errors) diff --git a/tox.ini b/tox.ini index 2875f3374adb..41d06916668d 100644 --- a/tox.ini +++ b/tox.ini @@ -375,6 +375,7 @@ extension = N367 = checks:do_not_alias_mock_class N368 = checks:do_not_use_mock_class_as_new_mock_value N369 = checks:check_lockutils_rwlocks + N370 = checks:check_six paths = ./nova/hacking