Skip to content

Commit fd5cc44

Browse files
committed
Allow long lines in comments too, with similar rules; related to issue #224
1 parent 61a1f6d commit fd5cc44

4 files changed

Lines changed: 56 additions & 9 deletions

File tree

CHANGES.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ Changelog
1111

1212
* Fix regression with multiple brackets. (Issue #214)
1313

14+
* Allow long lines in multiline strings and comments if they cannot
15+
be wrapped. (Issue #224).
16+
17+
* Optionally disable physical line checks inside multiline strings,
18+
using ``# noqa``. (Issue #242)
19+
1420

1521
1.4.6 (2013-07-02)
1622
------------------

pep8.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,14 @@ def maximum_line_length(physical_line, max_line_length, multiline):
218218
if length > max_line_length and not noqa(line):
219219
# Sometimes, long lines in docstrings are hard to avoid -- like,
220220
# a long URL that can't be wrapped because it has no whitespace.
221-
if multiline and re.match(r'^\s*\S+$', line):
221+
# And similar case exists with URL in comments.
222+
#
223+
# A check is added to still report the error when the 72 first
224+
# chars are whitespaces (79 - 7).
225+
chunks = line.split()
226+
if ((len(chunks) == 1 and multiline) or
227+
(len(chunks) == 2 and chunks[0] == '#')) and \
228+
len(line) - len(chunks[-1]) < max_line_length - 7:
222229
return
223230
if hasattr(line, 'decode'): # Python 2
224231
# The line could contain multi-byte characters
@@ -1351,8 +1358,8 @@ def generate_tokens(self):
13511358
tokengen = tokenize.generate_tokens(self.readline)
13521359
try:
13531360
for token in tokengen:
1354-
yield token
13551361
self.maybe_check_physical(token)
1362+
yield token
13561363
except (SyntaxError, tokenize.TokenError):
13571364
self.report_invalid_syntax()
13581365

@@ -1365,7 +1372,7 @@ def maybe_check_physical(self, token):
13651372
if token[0] in (tokenize.NEWLINE, tokenize.NL):
13661373
# Obviously, a newline token ends a single physical line.
13671374
self.check_physical(token[4])
1368-
elif token[0] == tokenize.STRING and token[1].count('\n'):
1375+
elif token[0] == tokenize.STRING and '\n' in token[1]:
13691376
# Less obviously, a string that contains newlines is a
13701377
# multiline string, either triple-quoted or with internal
13711378
# newlines backslash-escaped. Check every physical line in the

testsuite/E50.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,37 @@
6868
#: E501
6969
"""
7070
longnospaceslongnospaceslongnospaceslongnospaceslongnospaceslongnospaceslongnospaceslongnospaces"""
71+
#: Okay
72+
"""
73+
This
74+
almost_empty_line
75+
"""
76+
#: E501
77+
"""
78+
This
79+
almost_empty_line
80+
"""
81+
#: E501
82+
# A basic comment
83+
# with a long long long long long long long long long long long long long long long long line
84+
85+
#
86+
#: Okay
87+
# I'm some great comment. Because I'm so great, I'm going to give you a
88+
# reference to some valuable information about some API that I'm calling:
89+
#
90+
# http://msdn.microsoft.com/en-us/library/windows/desktop/aa363858(v=vs.85).aspx
91+
92+
import this
93+
94+
# longnospaceslongnospaceslongnospaceslongnospaceslongnospaceslongnospaceslongnospaceslongnospaces
95+
96+
#
97+
#: Okay
98+
# This
99+
# almost_empty_line
100+
101+
#
102+
#: E501
103+
# This
104+
# almost_empty_line

testsuite/utf-8.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ def __init__(self, width, height,
3131
# 78 narrow chars (Na) + 1 wide char (W)
3232
# 01 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8情
3333

34-
# 2 narrow chars (Na) + 40 wide chars (W)
35-
# 情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情
34+
# 3 narrow chars (Na) + 40 wide chars (W)
35+
# 情 情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情
3636

37-
# 2 narrow chars (Na) + 77 wide chars (W)
38-
# 情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情
37+
# 3 narrow chars (Na) + 76 wide chars (W)
38+
# 情 情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情
3939

4040
#
4141
#: E501
@@ -47,6 +47,6 @@ def __init__(self, width, height,
4747
# 01 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8情情
4848
#
4949
#: E501
50-
# 2 narrow chars (Na) + 78 wide chars (W)
51-
# 情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情
50+
# 3 narrow chars (Na) + 77 wide chars (W)
51+
# 情 情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情情
5252
#

0 commit comments

Comments
 (0)