Move docstring/list checks into their own file

Move H5xx checks

Change-Id: I2aa1cc096fc24abaafefd13b109300975c1bec44
This commit is contained in:
Joe Gordon 2014-04-24 17:25:45 -07:00 committed by Gerrit Code Review
parent 13fefe4f27
commit 2c092eedff
3 changed files with 38 additions and 23 deletions

View File

@ -0,0 +1,37 @@
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import tokenize
from hacking import core
@core.flake8ext
def hacking_no_locals(logical_line, physical_line, tokens, noqa):
"""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 noqa:
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")

View File

@ -100,28 +100,6 @@ def import_normalize(line):
return line
@flake8ext
def hacking_no_locals(logical_line, physical_line, tokens, noqa):
"""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 noqa:
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")
FORMAT_RE = re.compile("%(?:"
"%|" # Ignore plain percents
"(\(\w+\))?" # mapping key

View File

@ -50,7 +50,7 @@ flake8.extension =
H403 = hacking.checks.docstrings:hacking_docstring_multiline_end
H404 = hacking.checks.docstrings:hacking_docstring_multiline_start
H405 = hacking.checks.docstrings:hacking_docstring_summary
H501 = hacking.core:hacking_no_locals
H501 = hacking.checks.dictlist:hacking_no_locals
H700 = hacking.core:hacking_localization_strings
H801 = hacking.core:OnceGitCheckCommitTitleBug
H802 = hacking.core:OnceGitCheckCommitTitleLength