From f33c2130511f09a6014f45c12345aa158ab9fb6d Mon Sep 17 00:00:00 2001 From: Andreas Jaeger Date: Wed, 1 Apr 2020 20:42:52 +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 by updated hacking version. Update local hacking checks for new flake8. Remove hacking and friends from lower-constraints, they are not needed in installations. Change-Id: I6e6638c07b5d24c90e9deae277e98149f383eb3d --- lower-constraints.txt | 4 ---- test-requirements.txt | 2 +- tox.ini | 9 ++++++++- trove/hacking/checks.py | 19 +++++++++---------- trove/tests/unittests/hacking/test_check.py | 17 ++++++----------- 5 files changed, 24 insertions(+), 27 deletions(-) diff --git a/lower-constraints.txt b/lower-constraints.txt index 51caa2741c..81b58e1ece 100644 --- a/lower-constraints.txt +++ b/lower-constraints.txt @@ -33,13 +33,11 @@ eventlet==0.18.2 extras==1.0.0 fasteners==0.14.1 fixtures==3.0.0 -flake8==2.5.5 future==0.16.0 futurist==1.6.0 gitdb2==2.0.3 GitPython==2.1.8 greenlet==0.4.13 -hacking==0.12.0 httplib2==0.9.1 idna==2.6 imagesize==1.0.0 @@ -96,7 +94,6 @@ passlib==1.7.0 Paste==2.0.2 PasteDeploy==1.5.0 pbr==2.0.0 -pep8==1.7.1 pexpect==3.1 pika-pool==0.1.3 pika==0.10.0 @@ -106,7 +103,6 @@ psycopg2==2.6.2 ptyprocess==0.5.2 pycadf==2.7.0 pycparser==2.18 -pyflakes==1.0.0 Pygments==2.2.0 pyinotify==0.9.6 pylint==1.9.2 # GPLv2 diff --git a/test-requirements.txt b/test-requirements.txt index 4080244b90..c8dbd865a1 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 already pins down pep8, pyflakes and flake8 -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.4,>=4.0 # Apache-2.0 nose>=1.3.7 # LGPL diff --git a/tox.ini b/tox.ini index 9971f08c2a..f808205569 100644 --- a/tox.ini +++ b/tox.ini @@ -65,7 +65,14 @@ filename=*.py,trove-*,app.wsgi [hacking] import_exceptions = trove.common.i18n -local-check-factory = trove.hacking.checks.factory + +[flake8:local-plugins] +extension = + T103= checks:check_raised_localized_exceptions + T104 = checks:check_no_basestring + T105 = checks:no_translate_logs + N335 = checks:assert_raises_regexp +paths = ./trove/hacking [testenv:api-ref] # This environment is called from CI scripts to test and publish diff --git a/trove/hacking/checks.py b/trove/hacking/checks.py index 26d30ec9ea..851ddd6645 100644 --- a/trove/hacking/checks.py +++ b/trove/hacking/checks.py @@ -12,7 +12,9 @@ import re -import pep8 +import pycodestyle + +from hacking import core _all_log_levels = ( 'critical', @@ -34,6 +36,7 @@ def _translation_is_not_expected(filename): return any(pat in filename for pat in ["/tests/"]) +@core.flake8ext def check_raised_localized_exceptions(logical_line, filename): """T103 - Untranslated exception message. :param logical_line: The logical line to check. @@ -55,6 +58,7 @@ def check_raised_localized_exceptions(logical_line, filename): yield (logical_line.index(exception_msg), msg) +@core.flake8ext def check_no_basestring(logical_line): """T104 - Don't use basestring, use six.string_types instead basestring is not supported by py3, using six.string_types to ensure @@ -66,7 +70,8 @@ def check_no_basestring(logical_line): yield(0, msg) -def no_translate_logs(logical_line, physical_line, filename): +@core.flake8ext +def no_translate_logs(physical_line, logical_line, filename): """T105 - Log messages shouldn't be translated from the Pike release. :param logical_line: The logical line to check. @@ -79,7 +84,7 @@ def no_translate_logs(logical_line, physical_line, filename): if _translation_is_not_expected(filename): return - if pep8.noqa(physical_line): + if pycodestyle.noqa(physical_line): return msg = "T105: Log message shouldn't be translated." @@ -90,6 +95,7 @@ def no_translate_logs(logical_line, physical_line, filename): asse_raises_regexp = re.compile(r"assertRaisesRegexp\(") +@core.flake8ext def assert_raises_regexp(logical_line): """Check for usage of deprecated assertRaisesRegexp @@ -99,10 +105,3 @@ def assert_raises_regexp(logical_line): if res: yield (0, "N335: assertRaisesRegex must be used instead " "of assertRaisesRegexp") - - -def factory(register): - register(check_raised_localized_exceptions) - register(check_no_basestring) - register(no_translate_logs) - register(assert_raises_regexp) diff --git a/trove/tests/unittests/hacking/test_check.py b/trove/tests/unittests/hacking/test_check.py index d2874b44c0..3f6c40211a 100644 --- a/trove/tests/unittests/hacking/test_check.py +++ b/trove/tests/unittests/hacking/test_check.py @@ -11,7 +11,7 @@ # under the License. import mock -import pep8 +import pycodestyle import textwrap from trove.hacking import checks as tc @@ -28,11 +28,6 @@ class HackingTestCase(trove_testtools.TestCase): def assertLineFails(self, func, *args): self.assertIsInstance(next(func(*args)), tuple) - def test_factory(self): - def check_callable(fn): - self.assertTrue(hasattr(fn, '__call__')) - self.assertIsNone(tc.factory(check_callable)) - def test_log_translations(self): all_log_levels = ( 'critical', @@ -89,20 +84,20 @@ class HackingTestCase(trove_testtools.TestCase): 0, len(list(tc.check_no_basestring("this basestring is good)")))) - # We are patching pep8 so that only the check under test is actually + # We are patching pycodestyle so that only the check under test is actually # installed. - @mock.patch('pep8._checks', + @mock.patch('pycodestyle._checks', {'physical_line': {}, 'logical_line': {}, 'tree': {}}) def _run_check(self, code, checker, filename=None): - pep8.register_check(checker) + pycodestyle.register_check(checker) lines = textwrap.dedent(code).strip().splitlines(True) - checker = pep8.Checker(filename=filename, lines=lines) + checker = pycodestyle.Checker(filename=filename, lines=lines) # NOTE(sdague): the standard reporter has printing to stdout # as a normal part of check_all, which bleeds through to the # test output stream in an unhelpful way. This blocks that printing. - with mock.patch('pep8.StandardReport.get_file_results'): + with mock.patch('pycodestyle.StandardReport.get_file_results'): checker.check_all() checker.report._deferred_print.sort() return checker.report._deferred_print