Skip to content

Commit

Permalink
B950: Ignores 'noqa' and 'type: ignore' comments (#162) (#358)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
dejvidq committed Mar 12, 2023
1 parent 37bd2a0 commit 5c85ef4
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 10 additions & 4 deletions bugbear.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
10 changes: 10 additions & 0 deletions tests/b950.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
6 changes: 6 additions & 0 deletions tests/test_bugbear.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
),
)

Expand All @@ -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)),
),
)

Expand All @@ -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)),
),
)

Expand Down

0 comments on commit 5c85ef4

Please sign in to comment.