File tree 3 files changed +37
-4
lines changed
3 files changed +37
-4
lines changed Original file line number Diff line number Diff line change @@ -6,6 +6,7 @@ Find out more about isort's release policy [here](https://pycqa.github.io/isort/
6
6
7
7
### 5.9.2 TBD
8
8
- Improved behavior of ` isort --check --atomic ` against Cython files.
9
+ - Fixed #1769 : Future imports added below assignments when no other imports present.
9
10
- Fixed #1772 : skip-gitignore will check files not in the git repository.
10
11
- Fixed #1762 : in some cases when skip-gitignore is set, isort fails to skip any files.
11
12
Original file line number Diff line number Diff line change @@ -55,6 +55,7 @@ def process(
55
55
next_import_section : str = ""
56
56
next_cimports : bool = False
57
57
in_quote : str = ""
58
+ was_in_quote : bool = False
58
59
first_comment_index_start : int = - 1
59
60
first_comment_index_end : int = - 1
60
61
contains_imports : bool = False
@@ -332,14 +333,15 @@ def process(
332
333
and (stripped_line or end_of_file )
333
334
and not config .append_only
334
335
and not in_top_comment
335
- and not in_quote
336
+ and not was_in_quote
336
337
and not import_section
337
338
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 )
339
340
):
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
341
343
if end_of_file and index != 0 :
342
- output_stream .write (line_separator )
344
+ output_stream .write (add_line_separator )
343
345
contains_imports = True
344
346
add_imports = []
345
347
Original file line number Diff line number Diff line change @@ -1798,3 +1798,33 @@ def test_isort_should_keep_multiple_noqa_comments_force_single_line_mode_issue_1
1798
1798
profile = "black" ,
1799
1799
force_single_line = True ,
1800
1800
)
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
+ )
You can’t perform that action at this time.
0 commit comments