Skip to content
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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,
)