From 118ef0e60bc0782e78a2e285b04a169aaa7f839e Mon Sep 17 00:00:00 2001 From: Matthew Shafer Date: Fri, 10 Sep 2021 09:57:43 -0500 Subject: [PATCH 1/5] fix spelling issue --- ni_python_styleguide/_acknowledge_existing_errors/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ni_python_styleguide/_acknowledge_existing_errors/__init__.py b/ni_python_styleguide/_acknowledge_existing_errors/__init__.py index 878e0c73..ea3efb86 100644 --- a/ni_python_styleguide/_acknowledge_existing_errors/__init__.py +++ b/ni_python_styleguide/_acknowledge_existing_errors/__init__.py @@ -93,7 +93,7 @@ def acknowledge_lint_errors(lint_errors): cached_key = f"{error.file}:{error.line + skip}" if error.code in handled_lines[cached_key]: logging.warning( - "Multiple occurances of error %s code were logged for %s:%s, only suprressing first", + "Multiple occurrences of error %s code were logged for %s:%s, only suprressing first", error.code, error.file, error.line + skip, From 431e8cdb4f4a87dd6a1ae7e867b0f1e35c7ef845 Mon Sep 17 00:00:00 2001 From: Matthew Shafer Date: Fri, 10 Sep 2021 09:58:05 -0500 Subject: [PATCH 2/5] whoops, don't suppress ignored CODES --- ni_python_styleguide/_acknowledge_existing_errors/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ni_python_styleguide/_acknowledge_existing_errors/__init__.py b/ni_python_styleguide/_acknowledge_existing_errors/__init__.py index ea3efb86..f5401741 100644 --- a/ni_python_styleguide/_acknowledge_existing_errors/__init__.py +++ b/ni_python_styleguide/_acknowledge_existing_errors/__init__.py @@ -64,7 +64,7 @@ def acknowledge_lint_errors(lint_errors): """ parsed_errors = map(_lint_errors_parser.parse, lint_errors) parsed_errors = filter(None, parsed_errors) - lint_errors_to_process = [error for error in parsed_errors if error not in EXCLUDED_ERRORS] + lint_errors_to_process = [error for error in parsed_errors if error.code not in EXCLUDED_ERRORS] lint_errors_by_file = defaultdict(list) for error in lint_errors_to_process: From 40922baef1bba0621d1675bc6b0a0c815399276b Mon Sep 17 00:00:00 2001 From: Matthew Shafer Date: Fri, 10 Sep 2021 09:58:24 -0500 Subject: [PATCH 3/5] better handle multiline statements --- .../_acknowledge_existing_errors/__init__.py | 23 +++++++++++------ .../line_continuation_tests/input.py | 25 +++++++++++++++++++ .../line_continuation_tests/output.py | 25 +++++++++++++++++++ 3 files changed, 65 insertions(+), 8 deletions(-) create mode 100644 tests/test_cli/acknowledge_existing_errors_test_cases__snapshots/line_continuation_tests/input.py create mode 100644 tests/test_cli/acknowledge_existing_errors_test_cases__snapshots/line_continuation_tests/output.py diff --git a/ni_python_styleguide/_acknowledge_existing_errors/__init__.py b/ni_python_styleguide/_acknowledge_existing_errors/__init__.py index f5401741..4f91fe24 100644 --- a/ni_python_styleguide/_acknowledge_existing_errors/__init__.py +++ b/ni_python_styleguide/_acknowledge_existing_errors/__init__.py @@ -25,19 +25,26 @@ def in_multiline_string(self, lineno): @staticmethod def _count_multiline_string_endings_in_line(line): - return line.count('"""') + line.count("'''") + return line.count('"""'), line.count("'''") def _load_lines(self): in_file = self._error_file.read_text().splitlines() - current_count = 0 + current_count = [0, 0] for line in in_file: - line_value = ( - current_count - + _InMultiLineStringChecker._count_multiline_string_endings_in_line(line) + type1, type2 = _InMultiLineStringChecker._count_multiline_string_endings_in_line(line) + current_count[0] += type1 + current_count[1] += type2 + + code_part_of_line = line + if "#" in line: + code_part_of_line = line.split("#", maxsplit=1)[0] + + # if occurrences of multiline string markers is odd, this must be in a multiline + # or, if line continuation token is on the ending, assume in a multiline statement + self._values.append( + any([part % 2 == 1 for part in current_count]) + or code_part_of_line.strip().endswith("\\") ) - # if occurances of multiline string markers is odd, this must be in a multiline - self._values.append(line_value % 2 == 1) - current_count = line_value def _add_noqa_to_line(lineno, code_lines, error_code, explanation): diff --git a/tests/test_cli/acknowledge_existing_errors_test_cases__snapshots/line_continuation_tests/input.py b/tests/test_cli/acknowledge_existing_errors_test_cases__snapshots/line_continuation_tests/input.py new file mode 100644 index 00000000..adba0444 --- /dev/null +++ b/tests/test_cli/acknowledge_existing_errors_test_cases__snapshots/line_continuation_tests/input.py @@ -0,0 +1,25 @@ +some_string_with_line_continuation = f"{installPath}\\foo\\bar\\baz.exe \ + --check --this-is-cool --bacon --ipsum" + +# black, please leave these ridiculous line continuations in place for testing. +# fmt: off +if 1 < 2 == True and \ + 3 < 4 and \ + 5 < 6: + print("There is order in this world") +else: + raise Exception("There's funny going on here.") +# fmt: on + + +someStringWithProblems = """ +problems, I have # even if +putting the other quote in would be weird +but like I said: '''I got problems :| +# even some like this ''' +many they be, +suppresion should be after +""" + +loremIpsum = "dolor" # comments however are not affected \ +# even if they have backslashes diff --git a/tests/test_cli/acknowledge_existing_errors_test_cases__snapshots/line_continuation_tests/output.py b/tests/test_cli/acknowledge_existing_errors_test_cases__snapshots/line_continuation_tests/output.py new file mode 100644 index 00000000..cc34c74d --- /dev/null +++ b/tests/test_cli/acknowledge_existing_errors_test_cases__snapshots/line_continuation_tests/output.py @@ -0,0 +1,25 @@ +some_string_with_line_continuation = f"{installPath}\\foo\\bar\\baz.exe \ + --check --this-is-cool --bacon --ipsum" # noqa D100: Missing docstring in public module (auto-generated noqa) # noqa F821: undefined name 'installPath' (auto-generated noqa) + +# black, please leave these ridiculous line continuations in place for testing. +# fmt: off +if 1 < 2 == True and \ + 3 < 4 and \ + 5 < 6: # noqa E712: comparison to True should be 'if cond is True:' or 'if cond:' (auto-generated noqa) + print("There is order in this world") +else: + raise Exception("There's funny going on here.") +# fmt: on + + +someStringWithProblems = """ +problems, I have # even if +putting the other quote in would be weird +but like I said: '''I got problems :| +# even some like this ''' +many they be, +suppresion should be after +""" # noqa N816: variable 'someStringWithProblems' in global scope should not be mixedCase (auto-generated noqa) + +loremIpsum = "dolor" # comments however are not affected \ # noqa N816: variable 'loremIpsum' in global scope should not be mixedCase (auto-generated noqa) +# even if they have backslashes From 3346e05518a8451dd93cc63a2d8ff23d07f56478 Mon Sep 17 00:00:00 2001 From: Matthew Shafer Date: Fri, 10 Sep 2021 10:51:25 -0500 Subject: [PATCH 4/5] simplify appending suppresions to existing line --- .../_acknowledge_existing_errors/__init__.py | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/ni_python_styleguide/_acknowledge_existing_errors/__init__.py b/ni_python_styleguide/_acknowledge_existing_errors/__init__.py index 4f91fe24..aac5b849 100644 --- a/ni_python_styleguide/_acknowledge_existing_errors/__init__.py +++ b/ni_python_styleguide/_acknowledge_existing_errors/__init__.py @@ -52,12 +52,7 @@ def _add_noqa_to_line(lineno, code_lines, error_code, explanation): old_line_ending = "\n" if line.endswith("\n") else "" line = line.rstrip("\n") - existing_suppression = re.search(r"noqa (?P[\w\d]+\: [\w\W]+?) -", line) - if existing_suppression: - before = existing_suppression.groupdict()["existing_suppresions"] - if error_code not in before: - line = line.replace(before, before + f", {error_code}: {explanation}") - else: + if f"noqa {error_code}" not in line: line += f" # noqa {error_code}: {explanation} (auto-generated noqa)" code_lines[lineno] = line + old_line_ending From 8928232ea0ed8eb8ae8c58d076d078f98cc303a8 Mon Sep 17 00:00:00 2001 From: Matthew Shafer Date: Fri, 10 Sep 2021 10:59:05 -0500 Subject: [PATCH 5/5] and we no longer need re --- ni_python_styleguide/_acknowledge_existing_errors/__init__.py | 1 - 1 file changed, 1 deletion(-) diff --git a/ni_python_styleguide/_acknowledge_existing_errors/__init__.py b/ni_python_styleguide/_acknowledge_existing_errors/__init__.py index aac5b849..517b86f6 100644 --- a/ni_python_styleguide/_acknowledge_existing_errors/__init__.py +++ b/ni_python_styleguide/_acknowledge_existing_errors/__init__.py @@ -1,6 +1,5 @@ import logging import pathlib -import re from collections import defaultdict from ni_python_styleguide._acknowledge_existing_errors import _lint_errors_parser