Skip to content

Commit 2faf957

Browse files
authored
Fix ValueError when formatting if continuation lines are incorrectly indented when using autopep8 (#829)
1 parent 5fa7ae9 commit 2faf957

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

pyls/plugins/autopep8_format.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Copyright 2018 Palantir Technologies, Inc.
22
import logging
3-
from autopep8 import fix_code
3+
import pycodestyle
4+
from autopep8 import fix_code, continued_indentation as autopep8_c_i
45
from pyls import hookimpl
56

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

35+
# Temporarily re-monkey-patch the continued_indentation checker - #771
36+
del pycodestyle._checks['logical_line'][pycodestyle.continued_indentation]
37+
pycodestyle.register_check(autopep8_c_i)
38+
3439
new_source = fix_code(document.source, options=options)
3540

41+
# Switch it back
42+
del pycodestyle._checks['logical_line'][autopep8_c_i]
43+
pycodestyle.register_check(pycodestyle.continued_indentation)
44+
3645
if new_source == document.source:
3746
return []
3847

test/plugins/test_autopep8_format.py

+27
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,25 @@ def func():
1515

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

18+
INDENTED_DOC = """def foo():
19+
print('asdf',
20+
file=None
21+
)
22+
23+
bar = { 'foo': foo
24+
}
25+
"""
26+
27+
CORRECT_INDENTED_DOC = """def foo():
28+
print('asdf',
29+
file=None
30+
)
31+
32+
33+
bar = {'foo': foo
34+
}
35+
"""
36+
1837

1938
def test_format(config, workspace):
2039
doc = Document(DOC_URI, workspace, DOC)
@@ -42,3 +61,11 @@ def test_range_format(config, workspace):
4261
def test_no_change(config, workspace):
4362
doc = Document(DOC_URI, workspace, GOOD_DOC)
4463
assert not pyls_format_document(config, doc)
64+
65+
66+
def test_hanging_indentation(config, workspace):
67+
doc = Document(DOC_URI, workspace, INDENTED_DOC)
68+
res = pyls_format_document(config, doc)
69+
70+
assert len(res) == 1
71+
assert res[0]['newText'] == CORRECT_INDENTED_DOC

0 commit comments

Comments
 (0)