Skip to content

Commit

Permalink
Fix PyCQA#1275 in the case where the --add-imports option was given
Browse files Browse the repository at this point in the history
  • Loading branch information
bbugyi200 committed Jul 11, 2020
1 parent 046a62f commit f5d9983
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
5 changes: 5 additions & 0 deletions isort/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,11 @@ def _sort_imports(
import_section += line_separator.join(add_imports) + line_separator
contains_imports = True
add_imports = []

# HACK: This will occur on the blank line after a
# module docstring. We want to preserve this blank
# line.
output_stream.write(line_separator)
else:
not_imports = True
elif (
Expand Down
2 changes: 1 addition & 1 deletion tests/test_isort.py
Original file line number Diff line number Diff line change
Expand Up @@ -810,7 +810,7 @@ def test_add_imports() -> None:
test_input = '"""Module docstring"""\n' "\nclass MyClass(object):\n pass\n"
test_output = isort.code(code=test_input, add_imports=["from __future__ import print_function"])
assert test_output == (
'"""Module docstring"""\n'
'"""Module docstring"""\n\n'
"from __future__ import print_function\n"
"\n"
"\n"
Expand Down
12 changes: 7 additions & 5 deletions tests/test_regressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,23 +42,25 @@ def test_blank_lined_removed_issue_1275():
"""Ensure isort doesn't accidentally remove blank lines after doc strings and before imports.
See: https://github.com/timothycrosley/isort/issues/1275
"""
assert (
isort.code(
'''"""
input_code = '''"""
My docstring
"""
from b import thing
from a import other_thing
'''
)
== '''"""
output_code = '''"""
My docstring
"""
from a import other_thing
from b import thing
'''

assert isort.code(input_code) == output_code
assert (
isort.code(input_code, add_imports=["from c import cthing"])
== output_code + "from c import cthing\n"
)


Expand Down

0 comments on commit f5d9983

Please sign in to comment.