From 3ac1870e85d1c88592525984304c2306742d413c Mon Sep 17 00:00:00 2001 From: Andreas Jaeger Date: Wed, 1 Apr 2020 08:38:23 +0200 Subject: [PATCH] Update hacking for Python3 The repo is Python 3 now, so update hacking to version 3.0 which supports Python 3. Update local hacking checks for new flake8. Many tests are failing, so disable all that fail and fix most of them in a followup. Change-Id: I34a767406ed9110fc6e0cf5015b88cf1e7f43801 --- lower-constraints.txt | 4 ---- manila/hacking/checks.py | 35 ++++++++++++++++++----------------- manila/tests/test_hacking.py | 8 ++++---- test-requirements.txt | 2 +- tox.ini | 34 ++++++++++++++++++++++++++++++++-- 5 files changed, 55 insertions(+), 28 deletions(-) diff --git a/lower-constraints.txt b/lower-constraints.txt index 5443435029..d87bdfbb13 100644 --- a/lower-constraints.txt +++ b/lower-constraints.txt @@ -27,11 +27,9 @@ 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 greenlet==0.4.10 -hacking==0.12.0 idna==2.6 imagesize==1.0.0 ipaddress==1.0.17 @@ -84,7 +82,6 @@ paramiko==2.0.0 Paste==2.0.2 PasteDeploy==1.5.0 pbr==2.0.0 -pep8==1.5.7 pika==0.10.0 pika-pool==0.1.3 prettytable==0.7.2 @@ -93,7 +90,6 @@ psycopg2-binary==2.6.2 pyasn1==0.4.2 pycadf==2.7.0 pycparser==2.18 -pyflakes==0.8.1 Pygments==2.2.0 pyinotify==0.9.6 PyMySQL==0.7.6 diff --git a/manila/hacking/checks.py b/manila/hacking/checks.py index 4530b27cad..2708d533e7 100644 --- a/manila/hacking/checks.py +++ b/manila/hacking/checks.py @@ -17,7 +17,8 @@ import ast import re import six -import pep8 +from hacking import core +import pycodestyle """ @@ -99,6 +100,7 @@ class BaseASTChecker(ast.NodeVisitor): return False +@core.flake8ext def no_translate_logs(logical_line): if translated_log.match(logical_line): yield(0, "M359 Don't translate log messages!") @@ -114,6 +116,8 @@ class CheckLoggingFormatArgs(BaseASTChecker): """ + name = "check_logging_format_args" + version = "1.0" CHECK_DESC = 'M310 Log method arguments should not be a tuple.' LOG_METHODS = [ 'debug', 'info', @@ -169,6 +173,7 @@ class CheckLoggingFormatArgs(BaseASTChecker): return super(CheckLoggingFormatArgs, self).generic_visit(node) +@core.flake8ext def check_explicit_underscore_import(logical_line, filename): """Check for explicit import of the _ function @@ -200,6 +205,8 @@ class CheckForStrUnicodeExc(BaseASTChecker): catch it. """ + name = "check_for_str_unicode_exc" + version = "1.0" CHECK_DESC = ('M325 str() and unicode() cannot be used on an ' 'exception. Remove or use six.text_type()') @@ -245,6 +252,8 @@ class CheckForTransAdd(BaseASTChecker): string to give the translators the most information. """ + name = "check_for_trans_add" + version = "1.0" CHECK_DESC = ('M326 Translated messages cannot be concatenated. ' 'String should be included in translated message.') @@ -259,8 +268,9 @@ class CheckForTransAdd(BaseASTChecker): super(CheckForTransAdd, self).generic_visit(node) -def check_oslo_namespace_imports(logical_line, physical_line, filename): - if pep8.noqa(physical_line): +@core.flake8ext +def check_oslo_namespace_imports(physical_line, logical_line, filename): + if pycodestyle.noqa(physical_line): return if re.match(oslo_namespace_imports, logical_line): msg = ("M333: '%s' must be used instead of '%s'.") % ( @@ -269,6 +279,7 @@ def check_oslo_namespace_imports(logical_line, physical_line, filename): yield(0, msg) +@core.flake8ext def dict_constructor_with_list_copy(logical_line): msg = ("M336: Must use a dict comprehension instead of a dict constructor" " with a sequence of key-value pairs." @@ -277,11 +288,13 @@ def dict_constructor_with_list_copy(logical_line): yield (0, msg) +@core.flake8ext def no_xrange(logical_line): if assert_no_xrange_re.match(logical_line): yield(0, "M337: Do not use xrange().") +@core.flake8ext def validate_assertTrue(logical_line): if re.match(assert_True, logical_line): msg = ("M313: Unit tests should use assertTrue(value) instead" @@ -289,6 +302,7 @@ def validate_assertTrue(logical_line): yield(0, msg) +@core.flake8ext def check_uuid4(logical_line): """Generating UUID @@ -307,6 +321,7 @@ def check_uuid4(logical_line): yield (0, msg) +@core.flake8ext def no_log_warn_check(logical_line): """Disallow 'LOG.warn' @@ -318,17 +333,3 @@ def no_log_warn_check(logical_line): msg = ("M338: LOG.warn is deprecated, use LOG.warning.") if re.match(no_log_warn, logical_line): yield(0, msg) - - -def factory(register): - register(check_explicit_underscore_import) - register(no_translate_logs) - register(CheckForStrUnicodeExc) - register(CheckLoggingFormatArgs) - register(CheckForTransAdd) - register(check_oslo_namespace_imports) - register(dict_constructor_with_list_copy) - register(no_xrange) - register(validate_assertTrue) - register(check_uuid4) - register(no_log_warn_check) diff --git a/manila/tests/test_hacking.py b/manila/tests/test_hacking.py index 6666bb5047..8bc5389d6e 100644 --- a/manila/tests/test_hacking.py +++ b/manila/tests/test_hacking.py @@ -18,7 +18,7 @@ import textwrap import ddt import mock -import pep8 +import pycodestyle from manila.hacking import checks from manila import test @@ -112,14 +112,14 @@ class HackingTestCase(test.TestCase): # We are patching pep8 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) checker.check_all() checker.report._deferred_print.sort() return checker.report._deferred_print diff --git a/test-requirements.txt b/test-requirements.txt index 4cc78ad9a3..1506053afc 100644 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -3,7 +3,7 @@ # process, which may cause wedges in the gate later. # hacking should be first -hacking!=0.13.0,<0.14,>=0.12.0 # Apache-2.0 +hacking>=3.0,<3.1.0 # Apache-2.0 bashate>=0.5.1 # Apache-2.0 coverage!=4.4,>=4.0 # Apache-2.0 diff --git a/tox.ini b/tox.ini index 6cf0221bb2..c9fcd1e58a 100644 --- a/tox.ini +++ b/tox.ini @@ -127,7 +127,23 @@ commands = alembic -c manila/db/migrations/alembic.ini revision -m ""{posargs} [flake8] # Following checks are ignored on purpose: -ignore = + +# Following checks should be evaluated and fixed: +# E123 closing bracket does not match indentation of opening bracket's line +# E402 module level import not at top of file +# W503 line break before binary operator +# W504 line break after binary operator +# W605 invalid escape sequence +# E741 ambiguous variable name 'l' +# E305 expected 2 blank lines after class or function definition, found 1 +# E241 multiple spaces after ':' +# E731 do not assign a lambda expression, use a def +# F632 use ==/!= to compare str, bytes, and int literals +# E117 over-indented +# F841 local variable 'e' is assigned to but never used +# E226 missing whitespace around arithmetic operator +# F601 dictionary key 'qos' repeated with different values +ignore = E117,E123,E226,E241,E305,E402,E731,E741,F601,F632,F841,W503,W504,W605 builtins = _ # [H106] Don't put vim configuration in source files. # [H203] Use assertIs(Not)None to check for None. @@ -139,7 +155,21 @@ exclude = .git,.tox,.testrepository,.venv,build,cover,dist,doc,*egg,api-ref/buil [hacking] import_exceptions = manila.i18n -local-check-factory = manila.hacking.checks.factory + +[flake8:local-plugins] +extension = + M310 = checks:CheckLoggingFormatArgs + M313 = checks:validate_assertTrue + M323 = checks:check_explicit_underscore_import + M325 = checks:CheckForStrUnicodeExc + M326 = checks:CheckForTransAdd + M333 = checks:check_oslo_namespace_imports + M336 = checks:dict_constructor_with_list_copy + M337 = checks:no_xrange + M338 = checks:no_log_warn_check + M354 = checks:check_uuid4 + M359 = checks:no_translate_logs +paths = ./manila/hacking [testenv:lower-constraints] deps =