From d3d43a355c53e44bf442e4c5c987be55a5c851a9 Mon Sep 17 00:00:00 2001 From: Takashi Kajinami Date: Wed, 27 Nov 2024 11:13:29 +0900 Subject: [PATCH] Replace own hacking checks by built-on ones Some of the checks implemented in the local plugin are already implemented in core hacking and can be replaced. Notes: The following checks are enabled by default H211: Check for assertTrue(isinstance(a, b)) sentences H212: Check for assertEqual(type(A), B) sentences H213: Check for usage of deprecated assertRaisesRegexp Also, the following checks are additionally enabled by this change. H204: Use assert(Not)Equal to check for equality H205: Use assert{Greater,Less}[Equal] Change-Id: I61f0bd669ef4c96524fc511909832093cb8623ee --- HACKING.rst | 3 --- cloudkitty/hacking/checks.py | 36 -------------------------------- cloudkitty/tests/test_hacking.py | 24 --------------------- tox.ini | 8 ++++--- 4 files changed, 5 insertions(+), 66 deletions(-) diff --git a/HACKING.rst b/HACKING.rst index 85130bd0..1acd61be 100644 --- a/HACKING.rst +++ b/HACKING.rst @@ -10,8 +10,6 @@ Cloudkitty Specific Commandments -------------------------------- - [C310] Check for improper use of logging format arguments. -- [C311] Use assertIsNone(...) instead of assertEqual(None, ...). -- [C312] Use assertTrue(...) rather than assertEqual(True, ...). - [C313] Validate that logs are not translated. - [C314] str() and unicode() cannot be used on an exception. - [C315] Translated messages cannot be concatenated. String should be @@ -23,7 +21,6 @@ Cloudkitty Specific Commandments - [C320] Do not use LOG.warn as it's deprecated. - [C321] Ensure that the _() function is explicitly imported to ensure proper translations. -- [C322] Check for usage of deprecated assertRaisesRegexp LOG Translations ---------------- diff --git a/cloudkitty/hacking/checks.py b/cloudkitty/hacking/checks.py index d11a2624..26886770 100644 --- a/cloudkitty/hacking/checks.py +++ b/cloudkitty/hacking/checks.py @@ -320,30 +320,6 @@ def no_xrange(logical_line): yield (0, "C319: Do not use xrange().") -@core.flake8ext -def validate_assertTrue(logical_line): - """Use assertTrue instead of assertEqual - - C312 - """ - if re.match(assert_True, logical_line): - msg = ("C312: Unit tests should use assertTrue(value) instead" - " of using assertEqual(True, value).") - yield (0, msg) - - -@core.flake8ext -def validate_assertIsNone(logical_line): - """Use assertIsNone instead of assertEqual - - C311 - """ - if re.match(assert_None, logical_line): - msg = ("C311: Unit tests should use assertIsNone(value) instead" - " of using assertEqual(None, value).") - yield (0, msg) - - @core.flake8ext def no_log_warn_check(logical_line): """Disallow 'LOG.warn' @@ -353,15 +329,3 @@ def no_log_warn_check(logical_line): msg = ("C320: LOG.warn is deprecated, please use LOG.warning!") if re.match(no_log_warn, logical_line): yield (0, msg) - - -@core.flake8ext -def assert_raises_regexp(logical_line): - """Check for usage of deprecated assertRaisesRegexp - - C322 - """ - res = asse_raises_regexp.search(logical_line) - if res: - yield (0, "C322: assertRaisesRegex must be used instead " - "of assertRaisesRegexp") diff --git a/cloudkitty/tests/test_hacking.py b/cloudkitty/tests/test_hacking.py index 9efb5d40..6f0f3cbd 100644 --- a/cloudkitty/tests/test_hacking.py +++ b/cloudkitty/tests/test_hacking.py @@ -285,33 +285,9 @@ class HackingTestCase(tests.TestCase): self.assertEqual(0, len(list(checks.no_xrange("range(45)")))) - def test_validate_assertTrue(self): - test_value = True - self.assertEqual(0, len(list(checks.validate_assertTrue( - "assertTrue(True)")))) - self.assertEqual(1, len(list(checks.validate_assertTrue( - "assertEqual(True, %s)" % test_value)))) - - def test_validate_assertIsNone(self): - test_value = None - self.assertEqual(0, len(list(checks.validate_assertIsNone( - "assertIsNone(None)")))) - self.assertEqual(1, len(list(checks.validate_assertIsNone( - "assertEqual(None, %s)" % test_value)))) - def test_no_log_warn_check(self): self.assertEqual(0, len(list(checks.no_log_warn_check( "LOG.warning('This should not trigger LOG.warn" "hacking check.')")))) self.assertEqual(1, len(list(checks.no_log_warn_check( "LOG.warn('We should not use LOG.wan')")))) - - def test_oslo_assert_raises_regexp(self): - code = """ - self.assertRaisesRegexp(ValueError, - "invalid literal for.*XYZ'$", - int, - 'XYZ') - """ - self._assert_has_errors(code, checks.assert_raises_regexp, - expected_errors=[(1, 0, "C322")]) diff --git a/tox.ini b/tox.ini index 7b7562a7..672e7270 100644 --- a/tox.ini +++ b/tox.ini @@ -82,6 +82,11 @@ commands = {posargs} [flake8] filename = *.py,app.wsgi exclude = .git,.venv,.tox,dist,doc,*egg,build,.ropeproject,releasenotes +# [H203]: Use assertIs(Not)None to check for None +# [H204]: Use assert(Not)Equal to check for equality +# [H205]: Use assert(Greater|Less)(Equal) for comparison +enable-extensions=H203,H204,H205 + [doc8] ignore-path = .venv,.git,.tox,.tmp,*cloudkitty/locale*,*lib/python*,cloudkitty.egg*,doc/build,releasenotes/* @@ -92,8 +97,6 @@ import_exceptions = cloudkitty.i18n [flake8:local-plugins] extension = C310 = checks:CheckLoggingFormatArgs - C311 = checks:validate_assertIsNone - C312 = checks:validate_assertTrue C313 = checks:no_translate_logs C314 = checks:CheckForStrUnicodeExc C315 = checks:CheckForTransAdd @@ -102,7 +105,6 @@ extension = C319 = checks:no_xrange C320 = checks:no_log_warn_check C321 = checks:check_explicit_underscore_import - C322 = checks:assert_raises_regexp paths = ./cloudkitty/hacking [testenv:releasenotes]