Skip to content

Fix ValueError when formatting if continuation lines are incorrectly indented when using autopep8 #829

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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