Skip to content

Commit

Permalink
Merge pull request #493 from nicolasbonifas/fix_length_of_empty_line
Browse files Browse the repository at this point in the history
Fix exception raised when fixing long line near the end of a file. Ad…
  • Loading branch information
hhatto committed Jul 28, 2019
2 parents aa34318 + 0249cb3 commit 3f2a8e5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
2 changes: 2 additions & 0 deletions autopep8.py
Original file line number Diff line number Diff line change
Expand Up @@ -1514,6 +1514,8 @@ def get_fixed_long_line(target, previous_line, original,

def longest_line_length(code):
"""Return length of longest line."""
if len(code) == 0:
return 0
return max(len(line) for line in code.splitlines())


Expand Down
18 changes: 18 additions & 0 deletions test/test_autopep8.py
Original file line number Diff line number Diff line change
Expand Up @@ -852,6 +852,10 @@ def test_shorten_line_candidates_are_valid(self):
re.sub(r'\s', '', text),
re.sub(r'\s', '', candidate))

def test_get_fixed_long_line_empty(self):
line = ''
self.assertEqual(line, autopep8.get_fixed_long_line(line, line, line))


class SystemTests(unittest.TestCase):

Expand Down Expand Up @@ -4877,6 +4881,20 @@ def test_range_with_broken_syntax(self):
with autopep8_context(line, options=['--line-range', '1', '1']) as result:
self.assertEqual(line, result)

def test_long_import_line(self):
line = """\
s
from t import a, \
bbbbbbbbbbbbbbbbbbbbbbbbbbbbb, ccccccccccccccccccccccccccccccc, ddddddddddddddddddddddddddddddddddd
"""
fixed = """\
from t import a, \
bbbbbbbbbbbbbbbbbbbbbbbbbbbbb, ccccccccccccccccccccccccccccccc, ddddddddddddddddddddddddddddddddddd
s
"""
with autopep8_context(line) as result:
self.assertEqual(fixed, result)

def test_exchange_multiple_imports_with_def(self):
line = """\
def f(n):
Expand Down

0 comments on commit 3f2a8e5

Please sign in to comment.