Add a check for delayed string interpolation
This is stolen from the Neutron checks, since this seems useful for all OpenStack projects. See the oslo i18n guideline: http://docs.openstack.org/developer/oslo.i18n/guidelines.html Change-Id: Ibeaa82b295e011d87db2a0c44186a106f8d4254d
This commit is contained in:
parent
912fabbf1a
commit
a038160785
|
@ -32,6 +32,7 @@ General
|
|||
it easier to find out who the author of the comment was.
|
||||
- [H105] Don't use author tags. We use version control instead.
|
||||
- [H106] Don't put vim configuration in source files (off by default).
|
||||
- [H904] Delay string interpolations at logging calls.
|
||||
- Do not shadow a built-in or reserved word. Shadowing built -in or reserved
|
||||
words makes the code harder to understand. Example::
|
||||
|
||||
|
|
|
@ -10,9 +10,16 @@
|
|||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
import re
|
||||
|
||||
from hacking import core
|
||||
|
||||
|
||||
log_string_interpolation = re.compile(r".*LOG\.(?:error|warn|warning|info"
|
||||
r"|critical|exception|debug)"
|
||||
r"\([^,]*%[^,]*[,)]")
|
||||
|
||||
|
||||
@core.flake8ext
|
||||
def hacking_no_cr(physical_line):
|
||||
r"""Check that we only use newlines not carriage returns.
|
||||
|
@ -25,3 +32,23 @@ def hacking_no_cr(physical_line):
|
|||
pos = physical_line.find('\r')
|
||||
if pos != -1 and pos == (len(physical_line) - 2):
|
||||
return (pos, "H903: Windows style line endings not allowed in code")
|
||||
|
||||
|
||||
@core.flake8ext
|
||||
@core.off_by_default
|
||||
def hacking_delayed_string_interpolation(logical_line, noqa):
|
||||
r"""String interpolation should be delayed at logging calls.
|
||||
|
||||
H904: LOG.debug('Example: %s' % 'bad')
|
||||
Okay: LOG.debug('Example: %s', 'good')
|
||||
"""
|
||||
msg = ("H904: 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 log_string_interpolation.match(logical_line):
|
||||
yield 0, msg
|
||||
|
|
|
@ -39,7 +39,7 @@ class HackingTestCase(hacking.tests.TestCase):
|
|||
def test_pep8(self):
|
||||
|
||||
# NOTE(jecarey): Add tests marked as off_by_default to enable testing
|
||||
turn_on = set(['H106'])
|
||||
turn_on = set(['H106', 'H904'])
|
||||
if self.options.select:
|
||||
turn_on.update(self.options.select)
|
||||
self.options.select = tuple(turn_on)
|
||||
|
|
Loading…
Reference in New Issue