Skip to content

Commit 1c8df11

Browse files
Merge pull request #1775 from PyCQA/issue/1769
Issue/1769
2 parents f44b5a5 + c36414d commit 1c8df11

File tree

3 files changed

+37
-4
lines changed

3 files changed

+37
-4
lines changed

Diff for: CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Find out more about isort's release policy [here](https://pycqa.github.io/isort/
66

77
### 5.9.2 TBD
88
- Improved behavior of `isort --check --atomic` against Cython files.
9+
- Fixed #1769: Future imports added below assignments when no other imports present.
910
- Fixed #1772: skip-gitignore will check files not in the git repository.
1011
- Fixed #1762: in some cases when skip-gitignore is set, isort fails to skip any files.
1112

Diff for: isort/core.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ def process(
5555
next_import_section: str = ""
5656
next_cimports: bool = False
5757
in_quote: str = ""
58+
was_in_quote: bool = False
5859
first_comment_index_start: int = -1
5960
first_comment_index_end: int = -1
6061
contains_imports: bool = False
@@ -332,14 +333,15 @@ def process(
332333
and (stripped_line or end_of_file)
333334
and not config.append_only
334335
and not in_top_comment
335-
and not in_quote
336+
and not was_in_quote
336337
and not import_section
337338
and not line.lstrip().startswith(COMMENT_INDICATORS)
338-
and not line.rstrip().endswith(DOCSTRING_INDICATORS)
339+
and not (line.rstrip().endswith(DOCSTRING_INDICATORS) and "=" not in line)
339340
):
340-
import_section = line_separator.join(add_imports) + line_separator
341+
add_line_separator = line_separator or "\n"
342+
import_section = add_line_separator.join(add_imports) + add_line_separator
341343
if end_of_file and index != 0:
342-
output_stream.write(line_separator)
344+
output_stream.write(add_line_separator)
343345
contains_imports = True
344346
add_imports = []
345347

Diff for: tests/unit/test_regressions.py

+30
Original file line numberDiff line numberDiff line change
@@ -1798,3 +1798,33 @@ def test_isort_should_keep_multiple_noqa_comments_force_single_line_mode_issue_1
17981798
profile="black",
17991799
force_single_line=True,
18001800
)
1801+
1802+
1803+
def test_isort_should_only_add_imports_to_valid_location_issue_1769():
1804+
assert (
1805+
isort.code(
1806+
'''v = """
1807+
""".split(
1808+
"\n"
1809+
)
1810+
''',
1811+
add_imports=["from __future__ import annotations"],
1812+
)
1813+
== '''from __future__ import annotations
1814+
1815+
v = """
1816+
""".split(
1817+
"\n"
1818+
)
1819+
'''
1820+
)
1821+
assert (
1822+
isort.code(
1823+
'''v=""""""''',
1824+
add_imports=["from __future__ import annotations"],
1825+
)
1826+
== '''from __future__ import annotations
1827+
1828+
v=""""""
1829+
'''
1830+
)

0 commit comments

Comments
 (0)