Skip to content

Commit

Permalink
Fix ValueError when formatting if continuation lines are incorrectly …
Browse files Browse the repository at this point in the history
…indented when using autopep8 (#829)
  • Loading branch information
lordsutch authored Jul 2, 2020
1 parent 5fa7ae9 commit 2faf957
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
11 changes: 10 additions & 1 deletion pyls/plugins/autopep8_format.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Copyright 2018 Palantir Technologies, Inc.
import logging
from autopep8 import fix_code
import pycodestyle
from autopep8 import fix_code, continued_indentation as autopep8_c_i
from pyls import hookimpl

log = logging.getLogger(__name__)
Expand Down Expand Up @@ -31,8 +32,16 @@ def _format(config, document, line_range=None):
if line_range:
options['line_range'] = list(line_range)

# Temporarily re-monkey-patch the continued_indentation checker - #771
del pycodestyle._checks['logical_line'][pycodestyle.continued_indentation]
pycodestyle.register_check(autopep8_c_i)

new_source = fix_code(document.source, options=options)

# Switch it back
del pycodestyle._checks['logical_line'][autopep8_c_i]
pycodestyle.register_check(pycodestyle.continued_indentation)

if new_source == document.source:
return []

Expand Down
27 changes: 27 additions & 0 deletions test/plugins/test_autopep8_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,25 @@ def func():

GOOD_DOC = """A = ['hello', 'world']\n"""

INDENTED_DOC = """def foo():
print('asdf',
file=None
)
bar = { 'foo': foo
}
"""

CORRECT_INDENTED_DOC = """def foo():
print('asdf',
file=None
)
bar = {'foo': foo
}
"""


def test_format(config, workspace):
doc = Document(DOC_URI, workspace, DOC)
Expand Down Expand Up @@ -42,3 +61,11 @@ def test_range_format(config, workspace):
def test_no_change(config, workspace):
doc = Document(DOC_URI, workspace, GOOD_DOC)
assert not pyls_format_document(config, doc)


def test_hanging_indentation(config, workspace):
doc = Document(DOC_URI, workspace, INDENTED_DOC)
res = pyls_format_document(config, doc)

assert len(res) == 1
assert res[0]['newText'] == CORRECT_INDENTED_DOC

0 comments on commit 2faf957

Please sign in to comment.