Skip to content

Commit

Permalink
Merge pull request #1451 from PyCQA/issue/1445/1444/add-import-placem…
Browse files Browse the repository at this point in the history
…ent-errors

Issue/1445/1444/add import placement errors
  • Loading branch information
timothycrosley committed Sep 3, 2020
2 parents c8cb55f + b94f4d2 commit 6a34b64
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 7 deletions.
14 changes: 7 additions & 7 deletions isort/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ def process(
code_sorting_indent: str = ""
cimports: bool = False
made_changes: bool = False
stripped_line: str = ""
end_of_file: bool = False

if config.float_to_top:
new_input = ""
Expand Down Expand Up @@ -112,6 +114,7 @@ def process(
return False

not_imports = True
end_of_file = True
line = ""
if not line_separator:
line_separator = "\n"
Expand Down Expand Up @@ -211,13 +214,7 @@ def process(
import_section += line
indent = line[: -len(line.lstrip())]
elif not (stripped_line or contains_imports):
if add_imports and not indent and not config.append_only:
if not import_section:
output_stream.write(line)
line = ""
contains_imports = True
else:
not_imports = True
not_imports = True
elif (
not stripped_line
or stripped_line.startswith("#")
Expand Down Expand Up @@ -275,13 +272,16 @@ def process(
raw_import_section: str = import_section
if (
add_imports
and (stripped_line or end_of_file)
and not config.append_only
and not in_top_comment
and not in_quote
and not import_section
and not line.lstrip().startswith(COMMENT_INDICATORS)
):
import_section = line_separator.join(add_imports) + line_separator
if end_of_file and index != 0:
output_stream.write(line_separator)
contains_imports = True
add_imports = []

Expand Down
55 changes: 55 additions & 0 deletions tests/unit/test_regressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -659,3 +659,58 @@ def test_isort_doesnt_misplace_comments_issue_1431():
)
"""
assert isort.code(input_text, profile="black") == input_text


def test_isort_doesnt_misplace_add_import_issue_1445():
"""Test to ensure isort won't misplace an added import depending on docstring position
See: https://github.com/PyCQA/isort/issues/1445
"""
assert (
isort.code(
'''#!/usr/bin/env python
"""module docstring"""
''',
add_imports=["import os"],
)
== '''#!/usr/bin/env python
"""module docstring"""
import os
'''
)

assert isort.check_code(
'''#!/usr/bin/env python
"""module docstring"""
import os
''',
add_imports=["import os"],
show_diff=True,
)


def test_isort_doesnt_mangle_code_when_adding_imports_issue_1444():
"""isort should NEVER mangle code. This particularly nasty and easy to reproduce bug,
caused isort to produce invalid code just by adding a single import statement depending
on comment placement.
See: https://github.com/PyCQA/isort/issues/1444
"""
assert (
isort.code(
'''
"""module docstring"""
''',
add_imports=["import os"],
)
== '''
"""module docstring"""
import os
'''
)

0 comments on commit 6a34b64

Please sign in to comment.