Merge "Add check to detect LOG.warn"

This commit is contained in:
Zuul
2025-12-04 17:18:18 +00:00
committed by Gerrit Code Review
4 changed files with 39 additions and 13 deletions

View File

@@ -50,6 +50,7 @@ General
mylist = Foo().list() # OKAY, does not shadow built-in mylist = Foo().list() # OKAY, does not shadow built-in
- [H905] Log.warn is deprecated. Enforce use of LOG.warning.
Imports Imports
------- -------

View File

@@ -101,7 +101,10 @@ Some of the available checks are disabled by default. These checks are:
- [H205] Use assert(Greater|Less)(Equal) for comparison. - [H205] Use assert(Greater|Less)(Equal) for comparison.
- [H210] Require 'autospec', 'spec', or 'spec_set' in - [H210] Require 'autospec', 'spec', or 'spec_set' in
mock.patch/mock.patch.object calls 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. - [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. To enable these checks, edit the ``flake8`` section of the ``tox.ini`` file.
For example to enable H106 and H203: For example to enable H106 and H203:

View File

@@ -57,3 +57,15 @@ def hacking_delayed_string_interpolation(logical_line, noqa):
line = re.sub(r",.*", '', line) line = re.sub(r",.*", '', line)
if '%' in line or '.format(' in line: if '%' in line or '.format(' in line:
yield 0, msg 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!")

View File

@@ -23,22 +23,22 @@ class OthersTestCase(tests.TestCase):
@ddt.unpack @ddt.unpack
@ddt.data( @ddt.data(
(1, 'LOG.debug("Test %s" % foo)', None), (False, 'LOG.debug("Test %s" % foo)', None),
(0, 'LOG.info("Test %s", foo)', None), (True, 'LOG.info("Test %s", foo)', None),
(1, 'LOG.info("Test {}".format(foo))', None), (False, 'LOG.info("Test {}".format(foo))', None),
(0, 'LOG.error("Test %s" % foo)', '# noqa'), (True, 'LOG.error("Test %s" % foo)', '# noqa'),
(1, 'LOG.debug("Test %s" % "foo")', None), (False, 'LOG.debug("Test %s" % "foo")', None),
(0, 'LOG.debug("Test %s", "foo")', None), (True, 'LOG.debug("Test %s", "foo")', None),
(0, 'LOG.warning("Test %s", ",".join("%s:%s" % (a, b)))', None), (True, 'LOG.warning("Test %s", ",".join("%s:%s" % (a, b)))', None),
(0, "LOG.warning('Testing some stuff')", None)) (True, "LOG.warning('Testing some stuff')", None))
def test_H904_hacking_delayed_string_interpolation( def test_H904_hacking_delayed_string_interpolation(
self, err_count, line, noqa): self, passes, line, noqa):
if err_count > 0: if passes:
self.assertCheckFails(other.hacking_delayed_string_interpolation,
line, noqa)
else:
self.assertCheckPasses(other.hacking_delayed_string_interpolation, self.assertCheckPasses(other.hacking_delayed_string_interpolation,
line, noqa) line, noqa)
else:
self.assertCheckFails(other.hacking_delayed_string_interpolation,
line, noqa)
@ddt.unpack @ddt.unpack
@ddt.data( @ddt.data(
@@ -49,3 +49,13 @@ class OthersTestCase(tests.TestCase):
self.assertCheckPasses(other.hacking_no_cr, line) self.assertCheckPasses(other.hacking_no_cr, line)
else: else:
self.assertCheckFails(other.hacking_no_cr, line) 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)