hacking: Add check for deprecated exception types

We also move everything to a new module with no external imports so
flake8 can import it without needing to install all the dependencies of
openstacksdk.

Change-Id: I8e610bc196f530223b27a6fbb8e8ca11b6420b82
Signed-off-by: Stephen Finucane <stephenfin@redhat.com>
This commit is contained in:
Stephen Finucane 2024-02-02 10:37:43 +00:00
parent 81d60c7874
commit 5de45a329d
4 changed files with 55 additions and 8 deletions

View File

@ -12,6 +12,7 @@
# License for the specific language governing permissions and limitations
# under the License.
import os
import re
from hacking import core
@ -26,7 +27,7 @@ Guidelines for writing new hacking checks
- Keep the test method code in the source file ordered based
on the O3xx value.
- List the new rule in the top level HACKING.rst file
- Add test cases for each new rule to nova/tests/unit/test_hacking.py
- Add test cases for each new rule to openstack/tests/unit/test_hacking.py
"""
@ -41,3 +42,23 @@ def assert_no_setupclass(logical_line):
"""
if SETUPCLASS_RE.match(logical_line):
yield (0, "O300: setUpClass not allowed")
@core.flake8ext
def assert_no_deprecated_exceptions(logical_line, filename):
"""Check for use of deprecated cloud-layer exceptions
0310
"""
if filename.endswith(os.path.join('openstack', 'cloud', 'exc.py')):
return
for exception in (
'OpenStackCloudTimeout',
'OpenStackCloudHTTPError',
'OpenStackCloudBadRequest',
'OpenStackCloudURINotFound',
'OpenStackCloudResourceNotFound',
):
if re.search(fr'\b{exception}\b', logical_line):
yield (0, 'O310: Use of deprecated Exception class')

View File

@ -37,7 +37,7 @@ class OpenStackCloudUnavailableFeature(OpenStackCloudException):
pass
# Backwards compat
# Backwards compat. These are deprecated and should not be used in new code.
OpenStackCloudTimeout = exceptions.ResourceTimeout
OpenStackCloudHTTPError = exceptions.HttpException
OpenStackCloudBadRequest = exceptions.BadRequestException

View File

@ -12,7 +12,7 @@
# License for the specific language governing permissions and limitations
# under the License.
from openstack import _hacking
from openstack._hacking import checks
from openstack.tests.unit import base
@ -52,20 +52,45 @@ class HackingTestCase(base.TestCase):
def test_assert_no_setupclass(self):
self.assertEqual(
len(list(_hacking.assert_no_setupclass("def setUpClass(cls)"))), 1
len(list(checks.assert_no_setupclass("def setUpClass(cls)"))), 1
)
self.assertEqual(
len(list(_hacking.assert_no_setupclass("# setUpClass is evil"))), 0
len(list(checks.assert_no_setupclass("# setUpClass is evil"))), 0
)
self.assertEqual(
len(
list(
_hacking.assert_no_setupclass(
checks.assert_no_setupclass(
"def setUpClassyDrinkingLocation(cls)"
)
)
),
0,
)
def test_assert_no_deprecated_exceptions(self):
self.assertEqual(
len(
list(
checks.assert_no_deprecated_exceptions(
"raise exc.OpenStackCloudTimeout",
"openstack/cloud/compute.py",
)
)
),
1,
)
self.assertEqual(
len(
list(
checks.assert_no_deprecated_exceptions(
"raise exc.OpenStackCloudTimeout",
"openstack/cloud/exc.py",
)
)
),
0,
)

View File

@ -159,8 +159,9 @@ exclude = .venv,.git,.tox,dist,doc,*lib/python*,*egg,build,openstack/_services_m
[flake8:local-plugins]
extension =
O300 = _hacking:assert_no_setupclass
paths = ./openstack
O300 = checks:assert_no_setupclass
O310 = checks:assert_no_deprecated_exceptions
paths = ./openstack/_hacking
[doc8]
extensions = .rst, .yaml