Skip to content

Commit 5d24e16

Browse files
committed
Allow long lines in multiline strings if they cannot be wrapped (issue #224).
1 parent 66d353d commit 5d24e16

2 files changed

Lines changed: 20 additions & 1 deletion

File tree

pep8.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ def missing_newline(physical_line):
200200
return len(physical_line), "W292 no newline at end of file"
201201

202202

203-
def maximum_line_length(physical_line, max_line_length):
203+
def maximum_line_length(physical_line, max_line_length, multiline):
204204
"""
205205
Limit all lines to a maximum of 79 characters.
206206
@@ -216,6 +216,10 @@ def maximum_line_length(physical_line, max_line_length):
216216
line = physical_line.rstrip()
217217
length = len(line)
218218
if length > max_line_length and not noqa(line):
219+
# Sometimes, long lines in docstrings are hard to avoid -- like,
220+
# a long URL that can't be wrapped because it has no whitespace.
221+
if multiline and re.match(r'^\s*\S+$', line):
222+
return
219223
if hasattr(line, 'decode'): # Python 2
220224
# The line could contain multi-byte characters
221225
try:
@@ -1187,6 +1191,7 @@ def __init__(self, filename=None, lines=None,
11871191
self._logical_checks = options.logical_checks
11881192
self._ast_checks = options.ast_checks
11891193
self.max_line_length = options.max_line_length
1194+
self.multiline = False # in a multiline string?
11901195
self.hang_closing = options.hang_closing
11911196
self.verbose = options.verbose
11921197
self.filename = filename
@@ -1358,10 +1363,12 @@ def maybe_check_physical(self, token):
13581363
# *not* check the last line: its newline is outside of the
13591364
# multiline string, so we consider it a regular physical line
13601365
# (it will be checked when we see the newline token).
1366+
self.multiline = True
13611367
self.line_number = token[2][0]
13621368
for line in token[1].split('\n')[:-1]:
13631369
self.check_physical(line + '\n')
13641370
self.line_number += 1
1371+
self.multiline = False
13651372
elif token[0] in (tokenize.NEWLINE, tokenize.NL):
13661373
self.check_physical(token[4])
13671374

testsuite/E50.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@
4545
('''
4646
''' + ' \
4747
')
48+
#: E501 E225 E226
49+
very_long_identifiers=and_terrible_whitespace_habits(are_no_excuse+for_long_lines)
4850
#
4951
#: E501
5052
'''multiline string
@@ -53,3 +55,13 @@
5355
#: E501
5456
'''same thing, but this time without a terminal newline in the string
5557
long long long long long long long long long long long long long long long long line'''
58+
#
59+
# issue 224 (unavoidable long lines in docstrings)
60+
#: Okay
61+
"""
62+
I'm some great documentation. Because I'm some great documentation, I'm
63+
going to give you a reference to some valuable information about some API
64+
that I'm calling:
65+
66+
http://msdn.microsoft.com/en-us/library/windows/desktop/aa363858(v=vs.85).aspx
67+
"""

0 commit comments

Comments
 (0)