Handle formatting of args in string interpolation check

We have added a check to make sure format strings and any args are being
passed in to be formatted as needed by the logger. The check did not
take into account string formatting that may be needed in preparing the
value args. There are some cases where this is needed, and we should not
be raising an error if the string formatting is being done there.

This adds a new test string and updates the hacking check to account for
these cases so the new hacking release does not raise unnecessary
errors.

Change-Id: Ie4e3f95d24dc954deb7336835a6f9b7bf54c9674
Signed-off-by: Sean McGinnis <sean.mcginnis@gmail.com>
This commit is contained in:
Sean McGinnis 2020-10-13 12:00:16 -05:00
parent 711ca6eb60
commit 31e9c8751e
No known key found for this signature in database
GPG Key ID: CE7EE4BFAF8D70C8
2 changed files with 4 additions and 0 deletions

View File

@ -52,5 +52,8 @@ def hacking_delayed_string_interpolation(logical_line, noqa):
# Line is a log statement, strip out strings and see if % is used,
# just to make sure we don't match on a format specifier in a string.
line = re.sub(r"[\"'].+?[\"']", '', logical_line)
# There are some cases where string formatting of the arguments are
# needed, so don't include those when checking.
line = re.sub(r",.*", '', line)
if '%' in line:
yield 0, msg

View File

@ -28,6 +28,7 @@ class OthersTestCase(tests.TestCase):
(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))
def test_H904_hacking_delayed_string_interpolation(
self, err_count, line, noqa):