Switch to new hacking 0.12

The release includes its own check for delayed string interpolation for
log messages, so we can now remove our own check for the same thing. The
check is off-by-default, so we need to explicitly enable it. Sadly,
select= directive in tox.ini is broken [1] so we need to enable the
check with flake8 argument instead.

[1] https://github.com/PyCQA/pycodestyle/issues/390

Change-Id: Idc6b8e5b1cb594e130d4cc0cbcfffd362f9ab86a
This commit is contained in:
Ihar Hrachyshka 2016-11-07 18:48:23 +00:00
parent 3596a387c1
commit 4e25ba840a
5 changed files with 3 additions and 61 deletions

View File

@ -28,7 +28,6 @@ Neutron Specific Commandments
- [N334] Use unittest2 uniformly across Neutron.
- [N340] Check usage of <module>.i18n (and neutron.i18n)
- [N341] Check usage of _ from python builtins
- [N342] String interpolation should be delayed at logging calls.
- [N343] Production code must not import from neutron.tests.*
- [N344] Python 3: Do not use filter(lambda obj: test(obj), data). Replace it
with [obj for obj in data if test(obj)].

View File

@ -64,9 +64,6 @@ def _regex_for_level(level, hint):
}
log_string_interpolation = re.compile(r".*LOG\.(?:error|warn|warning|info"
r"|critical|exception|debug)"
r"\([^,]*%[^,]*[,)]")
log_translation_hint = re.compile(
'|'.join('(?:%s)' % _regex_for_level(level, hint)
for level, hint in six.iteritems(_all_log_levels)))
@ -364,28 +361,6 @@ def check_unittest_imports(logical_line):
yield (0, msg)
@flake8ext
def check_delayed_string_interpolation(logical_line, filename, noqa):
"""N342 String interpolation should be delayed at logging calls.
N342: LOG.debug('Example: %s' % 'bad')
Okay: LOG.debug('Example: %s', 'good')
"""
msg = ("N342 String interpolation should be delayed to be "
"handled by the logging code, rather than being done "
"at the point of the logging call. "
"Use ',' instead of '%'.")
if noqa:
return
if 'neutron/tests/' in filename:
return
if log_string_interpolation.match(logical_line):
yield(0, msg)
@flake8ext
def check_no_imports_from_tests(logical_line, filename, noqa):
"""N343 Production code must not import from neutron.tests.*
@ -432,6 +407,5 @@ def factory(register):
register(check_oslo_i18n_wrapper)
register(check_builtins_gettext)
register(check_unittest_imports)
register(check_delayed_string_interpolation)
register(check_no_imports_from_tests)
register(check_python3_no_filter)

View File

@ -301,39 +301,6 @@ class HackingTestCase(base.BaseTestCase):
self.assertLineFails(f, 'from unittest.TestSuite')
self.assertLineFails(f, 'import unittest')
def test_check_delayed_string_interpolation(self):
dummy_noqa = CREATE_DUMMY_MATCH_OBJECT.search('a')
# In 'logical_line', Contents of strings replaced with
# "xxx" of same length.
fail_code1 = 'LOG.error(_LE("xxxxxxxxxxxxxxx") % value)'
fail_code2 = "LOG.warning(msg % 'xxxxx')"
self.assertEqual(
1, len(list(checks.check_delayed_string_interpolation(fail_code1,
"neutron/common/rpc.py", None))))
self.assertEqual(
1, len(list(checks.check_delayed_string_interpolation(fail_code2,
"neutron/common/rpc.py", None))))
pass_code1 = 'LOG.error(_LE("xxxxxxxxxxxxxxxxxx"), value)'
pass_code2 = "LOG.warning(msg, 'xxxxx')"
self.assertEqual(
0, len(list(checks.check_delayed_string_interpolation(pass_code1,
"neutron/common/rpc.py", None))))
self.assertEqual(
0, len(list(checks.check_delayed_string_interpolation(pass_code2,
"neutron/common/rpc.py", None))))
# check a file in neutron/tests
self.assertEqual(
0, len(list(checks.check_delayed_string_interpolation(fail_code1,
"neutron/tests/test_assert.py",
None))))
# check code including 'noqa'
self.assertEqual(
0, len(list(checks.check_delayed_string_interpolation(fail_code1,
"neutron/common/rpc.py", dummy_noqa))))
def test_check_log_warn_deprecated(self):
bad = "LOG.warn(_LW('i am zlatan!'))"
self.assertEqual(

View File

@ -1,7 +1,7 @@
# The order of packages is significant, because pip processes them in the order
# of appearance. Changing the order has an impact on the overall integration
# process, which may cause wedges in the gate later.
hacking<0.12,>=0.11.0 # Apache-2.0
hacking<0.13,>=0.12.0 # Apache-2.0
coverage>=4.0 # Apache-2.0
fixtures>=3.0.0 # Apache-2.0/BSD

View File

@ -138,6 +138,8 @@ commands = sphinx-build -W -b html doc/source doc/build/html
# H404 multi line docstring should start with a summary
# H405 multi line docstring summary not separated with an empty line
ignore = E125,E126,E128,E129,E265,H404,H405
# H904: Delay string interpolations at logging calls
enable-extensions=H904
show-source = true
exclude = ./.*,build,dist