diff --git a/hacking/checks/dictlist.py b/hacking/checks/dictlist.py new file mode 100644 index 00000000..b9338e5b --- /dev/null +++ b/hacking/checks/dictlist.py @@ -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") diff --git a/hacking/core.py b/hacking/core.py index 3f35b1c9..12582150 100644 --- a/hacking/core.py +++ b/hacking/core.py @@ -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 diff --git a/setup.cfg b/setup.cfg index c44bd14d..09c4b3c5 100644 --- a/setup.cfg +++ b/setup.cfg @@ -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