summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael van Tellingen <m.vantellingen@lukkien.com>2015-03-31 22:20:41 +0200
committerMichael van Tellingen <m.vantellingen@lukkien.com>2015-03-31 22:20:41 +0200
commit754c46de42e122086b557a46e60d7cb52df61063 (patch)
treef46c5bb104eb538b5e78e347dcd05530cbfb14b3
parent74ddc39485c18cb78972fc0088f82b8af7b24c53 (diff)
downloadisort-754c46de42e122086b557a46e60d7cb52df61063.tar.gz
Implement `use_parentheses`.
Instead of using the `\` as line continuation allow wrapping the the long line within parentheses as suggested in PEP8 using a config setting
-rw-r--r--isort/isort.py7
-rw-r--r--isort/settings.py1
-rw-r--r--test_isort.py12
3 files changed, 18 insertions, 2 deletions
diff --git a/isort/isort.py b/isort/isort.py
index b0a740a0..9a9a9e15 100644
--- a/isort/isort.py
+++ b/isort/isort.py
@@ -293,8 +293,11 @@ class SortImports(object):
line = splitter.join(line_parts)
if not line:
line = next_line.pop()
- return "{0}{1} \\\n{2}".format(line, splitter,
- self._wrap(self.config['indent'] + splitter.join(next_line).lstrip()))
+
+ cont_line = self._wrap(self.config['indent'] + splitter.join(next_line).lstrip())
+ if self.config['use_parentheses']:
+ return "{0}{1} (\n{2})".format(line, splitter, cont_line)
+ return "{0}{1} \\\n{2}".format(line, splitter, cont_line)
return line
diff --git a/isort/settings.py b/isort/settings.py
index 230636ca..ff103eda 100644
--- a/isort/settings.py
+++ b/isort/settings.py
@@ -82,6 +82,7 @@ default = {'force_to_top': [],
'import_heading_firstparty': '',
'import_heading_localfolder': '',
'balanced_wrapping': False,
+ 'use_parentheses': False,
'order_by_type': True,
'atomic': False,
'lines_after_imports': -1,
diff --git a/test_isort.py b/test_isort.py
index 5ca0d67d..3da01ba0 100644
--- a/test_isort.py
+++ b/test_isort.py
@@ -413,6 +413,18 @@ def test_custom_indent():
" lib20, lib21, lib22\n")
+def test_use_parentheses():
+ test_input = (
+ "from fooooooooooooooooooooooooo.baaaaaaaaaaaaaaaaaaarrrrrrr import \\"
+ " my_custom_function as my_special_function"
+ )
+ test_output = SortImports(
+ file_contents=test_input, known_third_party=['django'],
+ line_length=79, use_parentheses=True,
+ ).output
+ assert '(' in test_output
+
+
def test_skip():
"""Ensure skipping a single import will work as expected."""
test_input = ("import myproject\n"