summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorZuul <zuul@review.opendev.org>2022-12-20 16:25:03 +0000
committerGerrit Code Review <review@openstack.org>2022-12-20 16:25:03 +0000
commit72281b9f93139f5069035ef586867addb1f909f4 (patch)
tree17b133398c1cae519c7778ce5af4e1ff1d3d7a9c
parent7f7aabd26e1a65092581418e7f562f9af1db2231 (diff)
parent628e1c152cc7c5e89d5261fc6f25ec72ac24c1df (diff)
downloadnova-72281b9f93139f5069035ef586867addb1f909f4.tar.gz
Merge "Add a hacking rule for the setDaemon method"
-rw-r--r--HACKING.rst1
-rw-r--r--nova/hacking/checks.py21
-rw-r--r--nova/tests/unit/test_hacking.py21
-rw-r--r--tox.ini1
4 files changed, 44 insertions, 0 deletions
diff --git a/HACKING.rst b/HACKING.rst
index a2f67d993b..c5a1ba4ae3 100644
--- a/HACKING.rst
+++ b/HACKING.rst
@@ -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
-------------------
diff --git a/nova/hacking/checks.py b/nova/hacking/checks.py
index cd393e7b33..704538250f 100644
--- a/nova/hacking/checks.py
+++ b/nova/hacking/checks.py
@@ -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.")
diff --git a/nova/tests/unit/test_hacking.py b/nova/tests/unit/test_hacking.py
index 10b2a79db4..41cbada99f 100644
--- a/nova/tests/unit/test_hacking.py
+++ b/nova/tests/unit/test_hacking.py
@@ -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)
diff --git a/tox.ini b/tox.ini
index 60315b507c..1ae5e32364 100644
--- a/tox.ini
+++ b/tox.ini
@@ -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