Skip to content

Commit 2095bdd

Browse files
authored
Allow the same special cases for B950 as E501 (#176) (#213)
* Allow the same special cases for B950 as E501 (176) * Undo version bump
1 parent 49aec18 commit 2095bdd

File tree

5 files changed

+61
-2
lines changed

5 files changed

+61
-2
lines changed

Diff for: .gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ var/
2323
*.egg-info/
2424
.installed.cfg
2525
*.egg
26+
venv/
2627

2728
# PyInstaller
2829
# Usually these files are written by a python script from a template

Diff for: README.rst

+14-1
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,15 @@ significantly violate the line length, you will receive a message that
173173
states what the actual limit is. This is inspired by Raymond Hettinger's
174174
`"Beyond PEP 8" talk <https://www.youtube.com/watch?v=wf-BqAjZb8M>`_ and
175175
highway patrol not stopping you if you drive < 5mph too fast. Disable
176-
E501 to avoid duplicate warnings.
176+
E501 to avoid duplicate warnings. Like E501, this error ignores long shebangs
177+
on the first line and urls or paths that are on their own line::
178+
179+
#! long shebang ignored
180+
181+
# https://some-super-long-domain-name.com/with/some/very/long/paths
182+
url = (
183+
"https://some-super-long-domain-name.com/with/some/very/long/paths"
184+
)
177185

178186

179187
How to enable opinionated warnings
@@ -237,6 +245,11 @@ MIT
237245
Change Log
238246
----------
239247

248+
21.12.0
249+
~~~~~~~~~~
250+
251+
* B950: Add same special cases as E501 (#213)
252+
240253
21.11.29
241254
~~~~~~~~~~
242255

Diff for: bugbear.py

+23
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,31 @@ def gen_line_based_checks(self):
6767
The following simple checks are based on the raw lines, not the AST.
6868
"""
6969
for lineno, line in enumerate(self.lines, start=1):
70+
# Special case: ignore long shebang (following pycodestyle).
71+
if lineno == 1 and line.startswith("#!"):
72+
continue
73+
7074
length = len(line) - 1
7175
if length > 1.1 * self.max_line_length:
76+
# Special case long URLS and paths to follow pycodestyle.
77+
# Would use the `pycodestyle.maximum_line_length` directly but
78+
# need to supply it arguments that are not available so chose
79+
# to replicate instead.
80+
chunks = line.split()
81+
82+
is_line_comment_url_path = len(chunks) == 2 and chunks[0] == "#"
83+
84+
just_long_url_path = len(chunks) == 1
85+
86+
num_leading_whitespaces = len(line) - len(chunks[-1])
87+
too_many_leading_white_spaces = (
88+
num_leading_whitespaces >= self.max_line_length - 7
89+
)
90+
91+
skip = is_line_comment_url_path or just_long_url_path
92+
if skip and not too_many_leading_white_spaces:
93+
continue
94+
7295
yield B950(lineno, length, vars=(length, self.max_line_length))
7396

7497
@classmethod

Diff for: tests/b950.py

+14
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,21 @@
1+
#! Ignore long shebang fooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
12
# Assumes the default allowed line length of 79
23

34
"line is fine"
45
" line is fine "
56
" line is still fine "
67
" line is no longer fine by any measures, yup"
78
"line is fine again"
9+
10+
# Ensure URL/path on it's own line is fine
11+
"https://foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo.com"
12+
"NOT OK: https://foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo.com"
13+
# https://foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo.com
14+
# NOT OK: https://foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo.com
15+
#
16+
#: Okay
17+
# This
18+
# almost_empty_line_too_long
19+
20+
# This
21+
# almost_empty_line_too_long

Diff for: tests/test_bugbear.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,15 @@ def test_b950(self):
318318
filename = Path(__file__).absolute().parent / "b950.py"
319319
bbc = BugBearChecker(filename=str(filename))
320320
errors = list(bbc.run())
321-
self.assertEqual(errors, self.errors(B950(6, 92, vars=(92, 79))))
321+
self.assertEqual(
322+
errors,
323+
self.errors(
324+
B950(7, 92, vars=(92, 79)),
325+
B950(12, 103, vars=(103, 79)),
326+
B950(14, 103, vars=(103, 79)),
327+
B950(21, 97, vars=(97, 79)),
328+
),
329+
)
322330

323331
def test_selfclean_bugbear(self):
324332
filename = Path(__file__).absolute().parent.parent / "bugbear.py"

0 commit comments

Comments
 (0)