summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTimothy Edmund Crosley <timothy.crosley@gmail.com>2021-07-08 00:31:49 -0700
committerGitHub <noreply@github.com>2021-07-08 00:31:49 -0700
commit1c8df11e2476aa2ec06e7597b3f5d845ecbcd2a2 (patch)
tree9070eeda7aba27b51211752716e0b6cab4749023
parentf44b5a55b0b8f09c0983bca8b425ecdd6aaf74a3 (diff)
parentc36414d25453fb9f2a14d26930283c263fc19aa0 (diff)
downloadisort-1c8df11e2476aa2ec06e7597b3f5d845ecbcd2a2.tar.gz
Merge pull request #1775 from PyCQA/issue/1769
Issue/1769
-rw-r--r--CHANGELOG.md1
-rw-r--r--isort/core.py10
-rw-r--r--tests/unit/test_regressions.py30
3 files changed, 37 insertions, 4 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 4ba31082..dc51d227 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -6,6 +6,7 @@ Find out more about isort's release policy [here](https://pycqa.github.io/isort/
### 5.9.2 TBD
- Improved behavior of `isort --check --atomic` against Cython files.
+ - Fixed #1769: Future imports added below assignments when no other imports present.
- Fixed #1772: skip-gitignore will check files not in the git repository.
- Fixed #1762: in some cases when skip-gitignore is set, isort fails to skip any files.
diff --git a/isort/core.py b/isort/core.py
index 7b2910da..4b24e45c 100644
--- a/isort/core.py
+++ b/isort/core.py
@@ -55,6 +55,7 @@ def process(
next_import_section: str = ""
next_cimports: bool = False
in_quote: str = ""
+ was_in_quote: bool = False
first_comment_index_start: int = -1
first_comment_index_end: int = -1
contains_imports: bool = False
@@ -332,14 +333,15 @@ def process(
and (stripped_line or end_of_file)
and not config.append_only
and not in_top_comment
- and not in_quote
+ and not was_in_quote
and not import_section
and not line.lstrip().startswith(COMMENT_INDICATORS)
- and not line.rstrip().endswith(DOCSTRING_INDICATORS)
+ and not (line.rstrip().endswith(DOCSTRING_INDICATORS) and "=" not in line)
):
- import_section = line_separator.join(add_imports) + line_separator
+ add_line_separator = line_separator or "\n"
+ import_section = add_line_separator.join(add_imports) + add_line_separator
if end_of_file and index != 0:
- output_stream.write(line_separator)
+ output_stream.write(add_line_separator)
contains_imports = True
add_imports = []
diff --git a/tests/unit/test_regressions.py b/tests/unit/test_regressions.py
index 388aa8d3..449f83d5 100644
--- a/tests/unit/test_regressions.py
+++ b/tests/unit/test_regressions.py
@@ -1798,3 +1798,33 @@ from some_very_long_filename_to_import_from_that_causes_a_too_long_import_row im
profile="black",
force_single_line=True,
)
+
+
+def test_isort_should_only_add_imports_to_valid_location_issue_1769():
+ assert (
+ isort.code(
+ '''v = """
+""".split(
+ "\n"
+)
+''',
+ add_imports=["from __future__ import annotations"],
+ )
+ == '''from __future__ import annotations
+
+v = """
+""".split(
+ "\n"
+)
+'''
+ )
+ assert (
+ isort.code(
+ '''v=""""""''',
+ add_imports=["from __future__ import annotations"],
+ )
+ == '''from __future__ import annotations
+
+v=""""""
+'''
+ )