From 0161ddc8facc0898ec13ab1ee077903337912f50 Mon Sep 17 00:00:00 2001 From: Takashi Kajinami Date: Sat, 23 Nov 2024 06:20:53 +0900 Subject: [PATCH] Add check to detect LOG.warn The warn method was deprecated in favor of the warning method. This check is quite commonly implemented in multiple OpenStack repos so would be a good common check we can maintain in hacking itself. Change-Id: I52acf591a5da782646757ad56a48513867be17b1 Signed-off-by: Takashi Kajinami --- HACKING.rst | 1 + README.rst | 3 +++ hacking/checks/other.py | 12 ++++++++++ hacking/tests/checks/test_other.py | 36 +++++++++++++++++++----------- 4 files changed, 39 insertions(+), 13 deletions(-) diff --git a/HACKING.rst b/HACKING.rst index 46530e6e..e1d8362e 100644 --- a/HACKING.rst +++ b/HACKING.rst @@ -47,6 +47,7 @@ General mylist = Foo().list() # OKAY, does not shadow built-in +- [H905] Log.warn is deprecated. Enforce use of LOG.warning. Imports ------- diff --git a/README.rst b/README.rst index 7fd90790..94a58fe7 100644 --- a/README.rst +++ b/README.rst @@ -101,7 +101,10 @@ Some of the available checks are disabled by default. These checks are: - [H205] Use assert(Greater|Less)(Equal) for comparison. - [H210] Require 'autospec', 'spec', or 'spec_set' in mock.patch/mock.patch.object calls +- [H216] Make sure unittest.mock is used instead of the third party mock + library - [H904] Delay string interpolations at logging calls. +- [H905] Log.warn is deprecated. Enforce use of LOG.warning. To enable these checks, edit the ``flake8`` section of the ``tox.ini`` file. For example to enable H106 and H203: diff --git a/hacking/checks/other.py b/hacking/checks/other.py index 39fe6f56..9a5f1ebc 100644 --- a/hacking/checks/other.py +++ b/hacking/checks/other.py @@ -57,3 +57,15 @@ def hacking_delayed_string_interpolation(logical_line, noqa): line = re.sub(r",.*", '', line) if '%' in line or '.format(' in line: yield 0, msg + + +@core.flake8ext +def hacking_no_log_warn(logical_line): + """Disallow 'LOG.warn(' + + Use LOG.warning() instead of Deprecated LOG.warn(). + https://docs.python.org/3/library/logging.html#logging.warning + """ + + if "LOG.warn(" in logical_line: + yield (0, "H905: LOG.warn is deprecated, please use LOG.warning!") diff --git a/hacking/tests/checks/test_other.py b/hacking/tests/checks/test_other.py index a9fab5e8..f43bfe2d 100644 --- a/hacking/tests/checks/test_other.py +++ b/hacking/tests/checks/test_other.py @@ -23,22 +23,22 @@ class OthersTestCase(tests.TestCase): @ddt.unpack @ddt.data( - (1, 'LOG.debug("Test %s" % foo)', None), - (0, 'LOG.info("Test %s", foo)', None), - (1, 'LOG.info("Test {}".format(foo))', None), - (0, 'LOG.error("Test %s" % foo)', '# noqa'), - (1, 'LOG.debug("Test %s" % "foo")', None), - (0, 'LOG.debug("Test %s", "foo")', None), - (0, 'LOG.warning("Test %s", ",".join("%s:%s" % (a, b)))', None), - (0, "LOG.warning('Testing some stuff')", None)) + (False, 'LOG.debug("Test %s" % foo)', None), + (True, 'LOG.info("Test %s", foo)', None), + (False, 'LOG.info("Test {}".format(foo))', None), + (True, 'LOG.error("Test %s" % foo)', '# noqa'), + (False, 'LOG.debug("Test %s" % "foo")', None), + (True, 'LOG.debug("Test %s", "foo")', None), + (True, 'LOG.warning("Test %s", ",".join("%s:%s" % (a, b)))', None), + (True, "LOG.warning('Testing some stuff')", None)) def test_H904_hacking_delayed_string_interpolation( - self, err_count, line, noqa): - if err_count > 0: - self.assertCheckFails(other.hacking_delayed_string_interpolation, - line, noqa) - else: + self, passes, line, noqa): + if passes: self.assertCheckPasses(other.hacking_delayed_string_interpolation, line, noqa) + else: + self.assertCheckFails(other.hacking_delayed_string_interpolation, + line, noqa) @ddt.unpack @ddt.data( @@ -49,3 +49,13 @@ class OthersTestCase(tests.TestCase): self.assertCheckPasses(other.hacking_no_cr, line) else: self.assertCheckFails(other.hacking_no_cr, line) + + @ddt.unpack + @ddt.data( + (False, 'LOG.warn("LOG.warn is deprecated")'), + (True, 'LOG.warning("LOG.warn is deprecated")')) + def test_H905_no_log_warn(self, passes, line): + if passes: + self.assertCheckPasses(other.hacking_no_log_warn, line) + else: + self.assertCheckFails(other.hacking_no_log_warn, line)