Skip to content

Commit 0383c36

Browse files
Fix indented identification isort
1 parent 15502c8 commit 0383c36

File tree

4 files changed

+70
-21
lines changed

4 files changed

+70
-21
lines changed

isort/identify.py

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -47,17 +47,17 @@ def imports(
4747
in_quote = ""
4848

4949
indexed_input = enumerate(input_stream)
50-
for index, line in indexed_input:
50+
for index, raw_line in indexed_input:
5151
(skipping_line, in_quote) = skip_line(
52-
line, in_quote=in_quote, index=index, section_comments=config.section_comments
52+
raw_line, in_quote=in_quote, index=index, section_comments=config.section_comments
5353
)
5454

55-
if top_only and not in_quote and line.startswith(STATEMENT_DECLARATIONS):
55+
if top_only and not in_quote and raw_line.startswith(STATEMENT_DECLARATIONS):
5656
break
5757
if skipping_line:
5858
continue
5959

60-
stripped_line = line.strip().split("#")[0]
60+
stripped_line = raw_line.strip().split("#")[0]
6161
if stripped_line.startswith("raise") or stripped_line.startswith("yield"):
6262
if stripped_line == "yield":
6363
while not stripped_line or stripped_line == "yield":
@@ -76,16 +76,16 @@ def imports(
7676
stripped_line = next_line.strip().split("#")[0]
7777
continue
7878

79-
line, *end_of_line_comment = line.split("#", 1)
79+
line, *end_of_line_comment = raw_line.split("#", 1)
8080
statements = [line.strip() for line in line.split(";")]
8181
if end_of_line_comment:
8282
statements[-1] = f"{statements[-1]}#{end_of_line_comment[0]}"
8383

8484
for statement in statements:
8585
line, _raw_line = _normalize_line(statement)
86-
if line.lstrip().startswith(("import ", "cimport ")):
86+
if line.startswith(("import ", "cimport ")):
8787
type_of_import = "straight"
88-
elif line.lstrip().startswith("from "):
88+
elif line.startswith("from "):
8989
type_of_import = "from"
9090
else:
9191
continue
@@ -101,7 +101,7 @@ def imports(
101101
identified_import = partial(
102102
Import,
103103
index + 1, # line numbers use 1 based indexing
104-
line.startswith(" ") or line.startswith("\n"),
104+
raw_line.startswith((" ", "\t")),
105105
cimport=cimports,
106106
file_path=file_path,
107107
)
@@ -177,18 +177,20 @@ def imports(
177177
direct_imports.remove("as")
178178
just_imports[1:] = direct_imports
179179
if attribute == alias and config.remove_redundant_aliases:
180-
pass
180+
yield identified_import(top_level_module, attribute)
181181
else:
182182
yield identified_import(top_level_module, attribute, alias=alias)
183183

184184
else:
185185
module = just_imports[as_index - 1]
186186
alias = just_imports[as_index + 1]
187-
direct_imports.remove(alias)
188-
direct_imports.remove("as")
189-
just_imports[1:] = direct_imports
190-
if not (module == alias and config.remove_redundant_aliases):
191-
yield identified_import(module, alias)
187+
just_imports.remove(alias)
188+
just_imports.remove("as")
189+
just_imports.remove(module)
190+
if module == alias and config.remove_redundant_aliases:
191+
yield identified_import(module)
192+
else:
193+
yield identified_import(module, alias=alias)
192194

193195
if just_imports:
194196
if type_of_import == "from":

tests/unit/test_identify.py

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from io import StringIO
22
from typing import List
33

4-
from isort import identify
4+
from isort import Config, identify
55

66

77
def imports_in_code(code: str, **kwargs) -> List[identify.Import]:
@@ -198,3 +198,51 @@ def generator_function():
198198
)
199199
== 2
200200
)
201+
202+
203+
def test_complex_examples():
204+
assert (
205+
len(
206+
imports_in_code(
207+
"""
208+
import a, b, c; import n
209+
210+
x = (
211+
1,
212+
2,
213+
3
214+
)
215+
216+
import x
217+
from os \\
218+
import path
219+
from os (
220+
import path
221+
)
222+
from os import ( \\"""
223+
)
224+
)
225+
== 7
226+
)
227+
assert not imports_in_code("from os import \\")
228+
229+
230+
def test_aliases():
231+
assert imports_in_code("import os as os")[0].alias == "os"
232+
assert not imports_in_code(
233+
"import os as os",
234+
config=Config(
235+
remove_redundant_aliases=True,
236+
),
237+
)[0].alias
238+
239+
assert imports_in_code("from os import path as path")[0].alias == "path"
240+
assert not imports_in_code(
241+
"from os import path as path", config=Config(remove_redundant_aliases=True)
242+
)[0].alias
243+
244+
245+
def test_indented():
246+
assert not imports_in_code("import os")[0].indented
247+
assert imports_in_code(" import os")[0].indented
248+
assert imports_in_code("\timport os")[0].indented

tests/unit/test_isort.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4946,13 +4946,12 @@ def test_find_imports_in_code() -> None:
49464946
":3 import second_straight",
49474947
":4 import first_from.first_from_function_1",
49484948
":4 import first_from.first_from_function_2",
4949-
":5 import bad_name.good_name",
4950-
":5 import bad_name",
4949+
":5 import bad_name as good_name",
49514950
":6 import parent.some_bad_defs.bad_name_1 as ok_name_1",
49524951
":6 import parent.some_bad_defs.bad_name_2 as ok_name_2",
4953-
":12 import needed_in_bla_2",
4954-
":15 import needed_in_bla",
4955-
":18 import needed_in_bla_bla",
4952+
":12 indented import needed_in_bla_2",
4953+
":15 indented import needed_in_bla",
4954+
":18 indented import needed_in_bla_bla",
49564955
":22 import needed_in_end",
49574956
]
49584957

tests/unit/test_main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -995,7 +995,7 @@ def test_only_modified_flag(tmpdir, capsys):
995995

996996

997997
def test_identify_imports_main(tmpdir, capsys):
998-
file_content = "import mod2\n import mod2\n" "a = 1\n" "import mod1\n"
998+
file_content = "import mod2\nimport mod2\n" "a = 1\n" "import mod1\n"
999999
some_file = tmpdir.join("some_file.py")
10001000
some_file.write(file_content)
10011001
file_imports = f"{some_file}:1 import mod2\n{some_file}:4 import mod1\n"

0 commit comments

Comments
 (0)