Repository: timothycrosley/isort
Pull request #530 (2017-05-28 18:58:55)

Feature/fix issue 471


Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 320f409a..06a45529 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -2,11 +2,13 @@ Changelog
 =========
 
 ### 4.2.8 - In progress
+IMPORTANT NOTE: This will be the last release with Python 2.6 support, subsequent releases will be 2.7+ only
 - Fixed issue #299: long lines occasionally not wrapped
 - Fixed issue #432: No longer add import inside class when class starts at top of file after encoding comment
 - Fixed issue #440: Added missing `--use-parentheses` option to command line tool and documentation
 - Fixed issue #496: import* imports now get successfully identified and reformatted instead of deleted
 - Fixed issue #491: Non ending parentheses withing single line comments no longer cause formatting issues
+- Fixed issue #471: Imports that wrap the maximum line length and contain comments on the last line are no longer rendered incorrectly
 Breaking Changes:
     - Fixed issue #511: All command line options with an underscore, have had the underscore replaced with a dash for consistency. This effects: multi-line, add-import, remove-import, force-adds, --force-single-line-imports, and length-sort.
     - Replaced the `--enforce-whitespace` option with `--ignore-whitespace` to restore original behavior of strict whitespace by default
diff --git a/isort/isort.py b/isort/isort.py
index 2953ff26..3a1dfcb2 100644
--- a/isort/isort.py
+++ b/isort/isort.py
@@ -345,14 +345,18 @@ class SortImports(object):
 
                     cont_line = self._wrap(self.config['indent'] + splitter.join(next_line).lstrip())
                     if self.config['use_parentheses']:
-                        return "{0}{1} (\n{2}{3}{4})".format(
+                        output = "{0}{1} (\n{2}{3}{4})".format(
                             line, splitter, cont_line,
                             "," if self.config['include_trailing_comma'] else "",
                             "\n" if wrap_mode in (
                                 settings.WrapModes.VERTICAL_HANGING_INDENT,
                                 settings.WrapModes.VERTICAL_GRID_GROUPED,
-                            ) else ""
-                        )
+                            ) else "")
+                        lines = output.split('\n')
+                        if '  #' in lines[-1] and lines[-1].endswith(')'):
+                            line, comment = lines[-1].split('  #', 1)
+                            lines[-1] = line + ')  #' + comment[:-1]
+                        return '\n'.join(lines)
                     return "{0}{1} \\\n{2}".format(line, splitter, cont_line)
         elif len(line) > self.config['line_length'] and wrap_mode == settings.WrapModes.NOQA:
             if "# NOQA" not in line:
diff --git a/isort/settings.py b/isort/settings.py
index 69af4765..d2f66480 100644
--- a/isort/settings.py
+++ b/isort/settings.py
@@ -154,7 +154,7 @@ def _update_with_config_file(file_path, sections, computed_settings):
 
         max_line_length = settings.pop('max_line_length', '').strip()
         if max_line_length:
-            computed_settings['line_length'] = int(max_line_length)
+            computed_settings['line_length'] = float('inf') if max_line_length == 'off' else int(max_line_length)
 
     for key, value in itemsview(settings):
         access_key = key.replace('not_', '').lower()


Test patch:
diff --git a/test_isort.py b/test_isort.py
index b1f169b7..9126b722 100644
--- a/test_isort.py
+++ b/test_isort.py
@@ -2056,3 +2056,14 @@ def test_ignore_whitespace(capsys):
     SortImports(file_contents=test_input, check=True, ignore_whitespace=True)
     out, err = capsys.readouterr()
     assert out == ''
+
+
+def test_import_wraps_with_comment_issue_471():
+    """Test to insure issue #471 is resolved"""
+    test_input = ('from very_long_module_name import SuperLongClassName  #@UnusedImport'
+                  ' -- long string of comments which wrap over')
+    expected_output = ('from very_long_module_name import (\n'
+                       '    SuperLongClassName)  # @UnusedImport -- long string of comments which wrap over\n')
+    assert  SortImports(file_contents=test_input, line_length=50, multi_line_output=1,
+                        use_parentheses=True).output == expected_output
+