Fix the pep8 commands failed
* Fix the pep8 commands failed and Enable H904 checks in tox.ini. * Hacking M331 still has Bug and remove it from HACKING, use H904 instead. Change-Id: Iaf692bc139b309376917832aaa749cc141c11275
This commit is contained in:
parent
9f04fc52ed
commit
f64f6c14ed
@ -14,4 +14,3 @@ Mistral Specific Commandments
|
|||||||
- [M328] Python 3: do not use dict.iteritems.
|
- [M328] Python 3: do not use dict.iteritems.
|
||||||
- [M329] Python 3: do not use dict.iterkeys.
|
- [M329] Python 3: do not use dict.iterkeys.
|
||||||
- [M330] Python 3: do not use dict.itervalues.
|
- [M330] Python 3: do not use dict.itervalues.
|
||||||
- [M331] String interpolation should be delayed at logging calls.
|
|
||||||
|
@ -30,9 +30,6 @@ import six
|
|||||||
oslo_namespace_imports_dot = re.compile(r"import[\s]+oslo[.][^\s]+")
|
oslo_namespace_imports_dot = re.compile(r"import[\s]+oslo[.][^\s]+")
|
||||||
oslo_namespace_imports_from_dot = re.compile(r"from[\s]+oslo[.]")
|
oslo_namespace_imports_from_dot = re.compile(r"from[\s]+oslo[.]")
|
||||||
oslo_namespace_imports_from_root = re.compile(r"from[\s]+oslo[\s]+import[\s]+")
|
oslo_namespace_imports_from_root = re.compile(r"from[\s]+oslo[\s]+import[\s]+")
|
||||||
log_string_interpolation = re.compile(r".*LOG\.(?:error|warn|warning|info"
|
|
||||||
r"|critical|exception|debug)"
|
|
||||||
r"\([^,]*%[^,]*[,)]")
|
|
||||||
|
|
||||||
|
|
||||||
def no_assert_equal_true_false(logical_line):
|
def no_assert_equal_true_false(logical_line):
|
||||||
@ -108,27 +105,6 @@ def check_python3_no_itervalues(logical_line):
|
|||||||
yield(0, msg)
|
yield(0, msg)
|
||||||
|
|
||||||
|
|
||||||
def check_delayed_string_interpolation(logical_line, filename, noqa):
|
|
||||||
"""M331: String interpolation should be delayed at logging calls.
|
|
||||||
|
|
||||||
M331: LOG.debug('Example: %s' % 'bad')
|
|
||||||
Okay: LOG.debug('Example: %s', 'good')
|
|
||||||
"""
|
|
||||||
msg = ("M331 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 'mistral/tests/' in filename:
|
|
||||||
return
|
|
||||||
|
|
||||||
if log_string_interpolation.match(logical_line):
|
|
||||||
yield(logical_line.index('%'), msg)
|
|
||||||
|
|
||||||
|
|
||||||
class BaseASTChecker(ast.NodeVisitor):
|
class BaseASTChecker(ast.NodeVisitor):
|
||||||
"""Provides a simple framework for writing AST-based checks.
|
"""Provides a simple framework for writing AST-based checks.
|
||||||
|
|
||||||
@ -297,4 +273,3 @@ def factory(register):
|
|||||||
register(check_python3_no_iterkeys)
|
register(check_python3_no_iterkeys)
|
||||||
register(check_python3_no_itervalues)
|
register(check_python3_no_itervalues)
|
||||||
register(check_python3_xrange)
|
register(check_python3_xrange)
|
||||||
register(check_delayed_string_interpolation)
|
|
||||||
|
@ -142,7 +142,7 @@ class BaseTest(base.BaseTestCase):
|
|||||||
found = len(filtered_items)
|
found = len(filtered_items)
|
||||||
|
|
||||||
if found != count:
|
if found != count:
|
||||||
LOG.info("[failed test ctx] items=%s, expected_props=%s" % (str(
|
LOG.info("[failed test ctx] items=%s, expected_props=%s", (str(
|
||||||
items), props))
|
items), props))
|
||||||
self.fail("Wrong number of items found [props=%s, "
|
self.fail("Wrong number of items found [props=%s, "
|
||||||
"expected=%s, found=%s]" % (props, count, found))
|
"expected=%s, found=%s]" % (props, count, found))
|
||||||
|
@ -121,59 +121,6 @@ class BaseLoggingCheckTest(base.BaseTest):
|
|||||||
self.assertEqual(0, len(list(checks.check_python3_no_itervalues(
|
self.assertEqual(0, len(list(checks.check_python3_no_itervalues(
|
||||||
"six.itervalues(ob))"))))
|
"six.itervalues(ob))"))))
|
||||||
|
|
||||||
def test_check_delayed_string_interpolation(self):
|
|
||||||
checker = checks.check_delayed_string_interpolation
|
|
||||||
code = """
|
|
||||||
msg_w = _LW('Test string (%s)')
|
|
||||||
msg_i = _LI('Test string (%s)')
|
|
||||||
value = 'test'
|
|
||||||
|
|
||||||
LOG.error(_LE("Test string (%s)") % value)
|
|
||||||
LOG.warning(msg_w % 'test%string')
|
|
||||||
LOG.info(msg_i %
|
|
||||||
"test%string%info")
|
|
||||||
LOG.critical(
|
|
||||||
_LC('Test string (%s)') % value,
|
|
||||||
instance=instance)
|
|
||||||
LOG.exception(_LE(" 'Test quotation %s' \"Test\"") % 'test')
|
|
||||||
LOG.debug(' "Test quotation %s" \'Test\'' % "test")
|
|
||||||
LOG.debug('Tesing %(test)s' %
|
|
||||||
{'test': ','.join(
|
|
||||||
['%s=%s' % (name, value)
|
|
||||||
for name, value in test.items()])})
|
|
||||||
"""
|
|
||||||
|
|
||||||
expected_errors = [(5, 34, 'M331'), (6, 18, 'M331'), (7, 15, 'M331'),
|
|
||||||
(10, 28, 'M331'), (12, 49, 'M331'),
|
|
||||||
(13, 40, 'M331'), (14, 28, 'M331')]
|
|
||||||
self._assert_has_errors(code, checker, expected_errors=expected_errors)
|
|
||||||
self._assert_has_no_errors(
|
|
||||||
code, checker, filename='mistral/tests/unit/hacking/test_checks.py'
|
|
||||||
)
|
|
||||||
|
|
||||||
code = """
|
|
||||||
msg_w = _LW('Test string (%s)')
|
|
||||||
msg_i = _LI('Test string (%s)')
|
|
||||||
value = 'test'
|
|
||||||
|
|
||||||
LOG.error(_LE("Test string (%s)"), value)
|
|
||||||
LOG.error(_LE("Test string (%s)") % value) # noqa
|
|
||||||
LOG.warn(_LW('Test string (%s)'),
|
|
||||||
value)
|
|
||||||
LOG.info(msg_i,
|
|
||||||
"test%string%info")
|
|
||||||
LOG.critical(
|
|
||||||
_LC('Test string (%s)'), value,
|
|
||||||
instance=instance)
|
|
||||||
LOG.exception(_LE(" 'Test quotation %s' \"Test\""), 'test')
|
|
||||||
LOG.debug(' "Test quotation %s" \'Test\'', "test")
|
|
||||||
LOG.debug('Tesing %(test)s',
|
|
||||||
{'test': ','.join(
|
|
||||||
['%s=%s' % (name, value)
|
|
||||||
for name, value in test.items()])})
|
|
||||||
"""
|
|
||||||
self._assert_has_no_errors(code, checker)
|
|
||||||
|
|
||||||
|
|
||||||
class TestLoggingWithWarn(BaseLoggingCheckTest):
|
class TestLoggingWithWarn(BaseLoggingCheckTest):
|
||||||
|
|
||||||
|
3
tox.ini
3
tox.ini
@ -77,7 +77,8 @@ show-source = true
|
|||||||
builtins = _
|
builtins = _
|
||||||
# [H106] Don't put vim configuration in source files.
|
# [H106] Don't put vim configuration in source files.
|
||||||
# [H203] Use assertIs(Not)None to check for None.
|
# [H203] Use assertIs(Not)None to check for None.
|
||||||
enable-extensions = H106,H203
|
# [H904] Delay string interpolations at logging calls.
|
||||||
|
enable-extensions = H106,H203,H904
|
||||||
exclude=.venv,.git,.tox,dist,doc,*lib/python*,*egg,tools,scripts
|
exclude=.venv,.git,.tox,dist,doc,*lib/python*,*egg,tools,scripts
|
||||||
|
|
||||||
[hacking]
|
[hacking]
|
||||||
|
Loading…
Reference in New Issue
Block a user