diff options
author | Zuul <zuul@review.opendev.org> | 2022-12-20 16:25:03 +0000 |
---|---|---|
committer | Gerrit Code Review <review@openstack.org> | 2022-12-20 16:25:03 +0000 |
commit | 72281b9f93139f5069035ef586867addb1f909f4 (patch) | |
tree | 17b133398c1cae519c7778ce5af4e1ff1d3d7a9c /nova | |
parent | 7f7aabd26e1a65092581418e7f562f9af1db2231 (diff) | |
parent | 628e1c152cc7c5e89d5261fc6f25ec72ac24c1df (diff) | |
download | nova-72281b9f93139f5069035ef586867addb1f909f4.tar.gz |
Merge "Add a hacking rule for the setDaemon method"
Diffstat (limited to 'nova')
-rw-r--r-- | nova/hacking/checks.py | 21 | ||||
-rw-r--r-- | nova/tests/unit/test_hacking.py | 21 |
2 files changed, 42 insertions, 0 deletions
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) |