From 5c85ef43264487869bf5e6a828a0b5305557c6b3 Mon Sep 17 00:00:00 2001 From: Dawid <15433565+dejvidq@users.noreply.github.com> Date: Sun, 12 Mar 2023 22:23:30 +0100 Subject: [PATCH] B950: Ignores 'noqa' and 'type: ignore' comments (#162) (#358) * B950: Ignores 'noqa' and 'type: ignore' comments (#162) * Refactored removing 'noqa' and 'type: ignore' comments, added more tests * Fixed unit tests and refactored, so the second comment replace will run only if first changed something --- README.rst | 2 +- bugbear.py | 14 ++++++++++---- tests/b950.py | 10 ++++++++++ tests/test_bugbear.py | 6 ++++++ 4 files changed, 27 insertions(+), 5 deletions(-) diff --git a/README.rst b/README.rst index 21960f5..1df792d 100644 --- a/README.rst +++ b/README.rst @@ -230,7 +230,7 @@ This is meant to be enabled by developers writing visitors using the ``ast`` mod **B950**: Line too long. This is a pragmatic equivalent of ``pycodestyle``'s ``E501``: it considers "max-line-length" but only triggers -when the value has been exceeded by **more than 10%**. You will no +when the value has been exceeded by **more than 10%**. ``noqa`` and ``type: ignore`` comments are ignored. You will no longer be forced to reformat code due to the closing parenthesis being one character too far to satisfy the linter. At the same time, if you do significantly violate the line length, you will receive a message that diff --git a/bugbear.py b/bugbear.py index fdb93e4..3d47c41 100644 --- a/bugbear.py +++ b/bugbear.py @@ -68,24 +68,30 @@ def gen_line_based_checks(self): The following simple checks are based on the raw lines, not the AST. """ + noqa_type_ignore_regex = re.compile(r"#\s*(noqa|type:\s*ignore)[^#\r\n]*$") for lineno, line in enumerate(self.lines, start=1): # Special case: ignore long shebang (following pycodestyle). if lineno == 1 and line.startswith("#!"): continue - length = len(line) - 1 - if length > 1.1 * self.max_line_length and line.strip(): + # At first, removing noqa and type: ignore trailing comments" + no_comment_line = noqa_type_ignore_regex.sub("", line) + if no_comment_line != line: + no_comment_line = noqa_type_ignore_regex.sub("", no_comment_line) + + length = len(no_comment_line) - 1 + if length > 1.1 * self.max_line_length and no_comment_line.strip(): # Special case long URLS and paths to follow pycodestyle. # Would use the `pycodestyle.maximum_line_length` directly but # need to supply it arguments that are not available so chose # to replicate instead. - chunks = line.split() + chunks = no_comment_line.split() is_line_comment_url_path = len(chunks) == 2 and chunks[0] == "#" just_long_url_path = len(chunks) == 1 - num_leading_whitespaces = len(line) - len(chunks[-1]) + num_leading_whitespaces = len(no_comment_line) - len(chunks[-1]) too_many_leading_white_spaces = ( num_leading_whitespaces >= self.max_line_length - 7 ) diff --git a/tests/b950.py b/tests/b950.py index 0dbded1..606ebaa 100644 --- a/tests/b950.py +++ b/tests/b950.py @@ -24,3 +24,13 @@ """ """ +"https://foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo.com # noqa" +"https://foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo.com # type: ignore" +"https://foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo.com # noqa: F401" +"https://foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo.com#noqa:F401, B950" +"https://foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo.com # type: ignore[some-code]" +"https://foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo.com # type: ignore[some-code]" +"https://foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo.com # type: ignore[some-code] # noqa: F401" +"https://foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo.com # noqa: F401 # type:ignore[some-code]" +"NOT OK: https://foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo.com # noqa" +"NOT OK: https://foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo.com # type: ignore" diff --git a/tests/test_bugbear.py b/tests/test_bugbear.py index 946ef7a..8964c5e 100644 --- a/tests/test_bugbear.py +++ b/tests/test_bugbear.py @@ -690,6 +690,8 @@ def test_b950(self): B950(12, 103, vars=(103, 79)), B950(14, 103, vars=(103, 79)), B950(21, 97, vars=(97, 79)), + B950(35, 104, vars=(104, 79)), + B950(36, 104, vars=(104, 79)), ), ) @@ -706,6 +708,8 @@ def test_b9_select(self): B950(12, 103, vars=(103, 79)), B950(14, 103, vars=(103, 79)), B950(21, 97, vars=(97, 79)), + B950(35, 104, vars=(104, 79)), + B950(36, 104, vars=(104, 79)), ), ) @@ -724,6 +728,8 @@ def test_b9_extend_select(self): B950(12, 103, vars=(103, 79)), B950(14, 103, vars=(103, 79)), B950(21, 97, vars=(97, 79)), + B950(35, 104, vars=(104, 79)), + B950(36, 104, vars=(104, 79)), ), )