Add no locals() for string formatting test
Using locals() for formatting strings is not as clear as explicit dictionaries and can produce hidden errors during refactoring. locals() are already being partially removed in nova and cinder. Related to bug 1171936 Change-Id: I6ef790ebf50d640af62c535952c7a98053869e43
This commit is contained in:
parent
8cb3057b8f
commit
42494c3fb9
@ -152,6 +152,8 @@ Example::
|
||||
},
|
||||
}
|
||||
|
||||
Do not use ``locals()`` for formatting strings, it is not clear as using
|
||||
explicit dictionaries and can hide errors during refactoring.
|
||||
|
||||
Calling Methods
|
||||
---------------
|
||||
|
@ -586,6 +586,28 @@ def hacking_docstring_multiline_start(physical_line, previous_logical, tokens):
|
||||
"should start with a summary")
|
||||
|
||||
|
||||
@flake8ext
|
||||
def hacking_no_locals(logical_line, physical_line, tokens):
|
||||
"""Do not use locals() for string formatting.
|
||||
|
||||
Okay: 'locals()'
|
||||
Okay: 'locals'
|
||||
Okay: locals()
|
||||
Okay: print(locals())
|
||||
H501: print("%(something)" % locals())
|
||||
Okay: print("%(something)" % locals()) # noqa
|
||||
"""
|
||||
if pep8.noqa(physical_line):
|
||||
return
|
||||
for_formatting = False
|
||||
for token_type, text, start, _, _ in tokens:
|
||||
if text == "%" and token_type == tokenize.OP:
|
||||
for_formatting = True
|
||||
if (for_formatting and token_type == tokenize.NAME and text ==
|
||||
"locals" and "locals()" in logical_line):
|
||||
yield (start[1], "H501: Do not use locals() for string formatting")
|
||||
|
||||
|
||||
@flake8ext
|
||||
def hacking_no_cr(physical_line):
|
||||
r"""Check that we only use newlines not carriage returns.
|
||||
|
@ -41,6 +41,7 @@ flake8.extension =
|
||||
H402 = hacking.core:hacking_docstring_one_line
|
||||
H403 = hacking.core:hacking_docstring_multiline_end
|
||||
H404 = hacking.core:hacking_docstring_multiline_start
|
||||
H501 = hacking.core:hacking_no_locals
|
||||
H601 = hacking.core:hacking_no_cr
|
||||
H700 = hacking.core:hacking_localization_strings
|
||||
H801 = hacking.core:OnceGitCheckCommitTitleBug
|
||||
|
Loading…
Reference in New Issue
Block a user