Skip to content

Commit

Permalink
Merge pull request #1501 from PyCQA/issue/1499/float-to-top-single-li…
Browse files Browse the repository at this point in the history
…ne-multiline-comment

Issue/1499/float to top single line multiline comment
  • Loading branch information
timothycrosley committed Sep 26, 2020
2 parents 4771e43 + ac5f198 commit b808f50
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -12,6 +12,7 @@ Find out more about isort's release policy [here](https://pycqa.github.io/isort/
- Fixed #1461: Quiet config option not respected by file API in some circumstances.
- Fixed #1482: pylama integration is not working correctly out-of-the-box.
- Fixed #1492: --check does not work with stdin source.
- Fixed #1499: isort gets confused by single line, multi-line style comments when using float-to-top.

### 5.5.3 [Hotfix] September 20, 2020
- Fixed #1488: in rare cases isort can mangle `yield from` or `raise from` statements.
Expand Down
8 changes: 6 additions & 2 deletions isort/parse.py
Expand Up @@ -200,12 +200,16 @@ def file_contents(contents: str, config: Config = DEFAULT_CONFIG) -> ParsedConte
if skipping_line:
out_lines.append(line)
continue
elif (

lstripped_line = line.lstrip()
if (
config.float_to_top
and import_index == -1
and line
and not in_quote
and not line.strip().startswith("#")
and not lstripped_line.startswith("#")
and not lstripped_line.startswith("'''")
and not lstripped_line.startswith('"""')
):
import_index = index - 1
while import_index and not in_lines[import_index - 1]:
Expand Down
77 changes: 77 additions & 0 deletions tests/unit/test_regressions.py
Expand Up @@ -1081,3 +1081,80 @@ def generator_function():
from \\
"""
assert isort.check_code(raise_from_at_file_end_ignored, show_diff=True)


def test_isort_float_to_top_correctly_identifies_single_line_comments_1499():
"""Test to ensure isort correctly handles the case where float to top is used
to push imports to the top and the top comment is a multiline type but only
one line.
See: https://github.com/PyCQA/isort/issues/1499
"""
assert (
isort.code(
'''#!/bin/bash
"""My comment"""
def foo():
pass
import a
def bar():
pass
''',
float_to_top=True,
)
== (
'''#!/bin/bash
"""My comment"""
import a
def foo():
pass
def bar():
pass
'''
)
)
assert (
isort.code(
"""#!/bin/bash
'''My comment'''
def foo():
pass
import a
def bar():
pass
""",
float_to_top=True,
)
== (
"""#!/bin/bash
'''My comment'''
import a
def foo():
pass
def bar():
pass
"""
)
)

assert isort.check_code(
"""#!/bin/bash
'''My comment'''
import a
x = 1
""",
float_to_top=True,
show_diff=True,
)

0 comments on commit b808f50

Please sign in to comment.