Skip to content

Commit

Permalink
Fix second part of #1275, when add_imports is used
Browse files Browse the repository at this point in the history
  • Loading branch information
timothycrosley committed Jul 15, 2020
1 parent fafe61d commit fd87001
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
3 changes: 3 additions & 0 deletions isort/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,9 @@ def _sort_imports(
indent = line[: -len(line.lstrip())]
elif not (stripped_line or contains_imports):
if add_imports and not indent:
if not import_section:
output_stream.write(line)
line = ""
import_section += line_separator.join(add_imports) + line_separator
contains_imports = True
add_imports = []
Expand Down
20 changes: 20 additions & 0 deletions tests/test_regressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,26 @@ def test_blank_lined_removed_issue_1275():
My docstring
"""
from a import other_thing
from b import thing
'''
)

assert (
isort.code(
'''"""
My docstring
"""
from b import thing
from a import other_thing
''',
add_imports=["from b import thing"],
)
== '''"""
My docstring
"""
from a import other_thing
from b import thing
'''
Expand Down

0 comments on commit fd87001

Please sign in to comment.