Add a hacking rule for the setDaemon method

Add the following hacking rule.

* N372: Don't use the setDaemon method.
        Use the daemon attribute instead.

Change-Id: Idb45421205f76d2d3b0576bd0504d261ed249edd
Related-Bug: 1987191
Signed-off-by: Takashi Natsume <takanattie@gmail.com>
This commit is contained in:
Takashi Natsume 2022-08-26 00:53:00 +09:00
parent 2eb358cdce
commit 628e1c152c
4 changed files with 44 additions and 0 deletions

View File

@ -76,6 +76,7 @@ Nova Specific Commandments
with eventlet patched code. Use nova.utils.ReaderWriterLock() instead.
- [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.
Creating Unit Tests
-------------------

View File

@ -141,6 +141,8 @@ rwlock_re = re.compile(
r"(?P<module_part>(oslo_concurrency\.)?(lockutils|fasteners))"
r"\.ReaderWriterLock\(.*\)")
six_re = re.compile(r"^(import six(\..*)?|from six(\..*)? import .*)$")
# Regex for catching the setDaemon method
set_daemon_re = re.compile(r"\.setDaemon\(")
class BaseASTChecker(ast.NodeVisitor):
@ -1078,3 +1080,22 @@ def import_stock_mock(logical_line):
"N371: You must explicitly import python's mock: "
"``from unittest import mock``"
)
@core.flake8ext
def check_set_daemon(logical_line):
"""Check for use of the setDaemon method of the threading.Thread class
The setDaemon method of the threading.Thread class has been deprecated
since Python 3.10. Use the daemon attribute instead.
See
https://docs.python.org/3.10/library/threading.html#threading.Thread.setDaemon
for details.
N372
"""
res = set_daemon_re.search(logical_line)
if res:
yield (0, "N372: Don't use the setDaemon method. "
"Use the daemon attribute instead.")

View File

@ -1043,3 +1043,24 @@ class HackingTestCase(test.NoDBTestCase):
import unittest.mock
"""
self._assert_has_no_errors(code, checks.import_stock_mock)
def test_check_set_daemon(self):
code = """
self.setDaemon(True)
worker.setDaemon(True)
self._event_thread.setDaemon(True)
mythread.setDaemon(False)
self.thread.setDaemon(1)
"""
errors = [(x + 1, 0, 'N372') for x in range(5)]
self._assert_has_errors(
code, checks.check_set_daemon, expected_errors=errors)
code = """
self.setDaemon = True
worker.setDaemonFlag(True)
self._event_thread.resetDaemon(True)
self.set.Daemon(True)
self.thread.setdaemon(True)
"""
self._assert_has_no_errors(code, checks.check_set_daemon)

View File

@ -351,6 +351,7 @@ extension =
N369 = checks:check_lockutils_rwlocks
N370 = checks:check_six
N371 = checks:import_stock_mock
N372 = checks:check_set_daemon
paths =
./nova/hacking