[hacking] N373 do not use eventlet primitives

Add a new hacking check to prevent adding Eventlet concurrency
primitives where the stdlib one is equivalent.

Right now only eventlet/greenthread.sleep is checked but as we progress
with the transformation we will add more primitives.

Change-Id: I1310bb7a8c8133364060eb8814226cab52219171
This commit is contained in:
Balazs Gibizer 2025-04-15 17:54:55 +02:00
parent 3946a94538
commit a0dc4e8516
4 changed files with 39 additions and 0 deletions

View File

@ -77,6 +77,8 @@ Nova Specific Commandments
- [N370] Don't use or import six
- [N371] You must explicitly import python's mock: ``from unittest import mock``
- [N372] Don't use the setDaemon method. Use the daemon attribute instead.
- [N373] Don't use eventlet specific concurrency primitives. Use the one
from stdlib instead. E.g. eventlet.sleep => time.sleep
Creating Unit Tests
-------------------

View File

@ -143,6 +143,8 @@ rwlock_re = re.compile(
six_re = re.compile(r"^(import six(\..*)?|from six(\..*)? import .*)$")
# Regex for catching the setDaemon method
set_daemon_re = re.compile(r"\.setDaemon\(")
eventlet_stdlib_primitives_re = re.compile(
r".*(eventlet|greenthread)\.sleep\(.*")
class BaseASTChecker(ast.NodeVisitor):
@ -1099,3 +1101,20 @@ def check_set_daemon(logical_line):
if res:
yield (0, "N372: Don't use the setDaemon method. "
"Use the daemon attribute instead.")
@core.flake8ext
def check_eventlet_primitives(logical_line, filename):
"""Check for use of any eventlet primitives where the stdlib equivalent
should be used
N373
"""
msg = (
"N373: Use the stdlib concurrency primitive instead of the Eventelt "
"specific one")
match = re.match(eventlet_stdlib_primitives_re, logical_line)
if match:
yield (0, msg)

View File

@ -1064,3 +1064,20 @@ class HackingTestCase(test.NoDBTestCase):
self.thread.setdaemon(True)
"""
self._assert_has_no_errors(code, checks.check_set_daemon)
def test_check_eventlet_primitives(self):
code = """
eventlet.sleep(0)
eventlet.sleep(1)
greenthread.sleep(0)
greenthread.sleep(1)
"""
errors = [(x + 1, 0, 'N373') for x in range(4)]
self._assert_has_errors(
code, checks.check_eventlet_primitives, expected_errors=errors)
code = """
time.sleep(0)
time.sleep(1)
"""
self._assert_has_no_errors(code, checks.check_eventlet_primitives)

View File

@ -358,6 +358,7 @@ extension =
N370 = checks:check_six
N371 = checks:import_stock_mock
N372 = checks:check_set_daemon
N373 = checks:check_eventlet_primitives
paths =
./nova/hacking