Skip to content

Commit 8e70db8

Browse files
100% test coverage for new identify module
1 parent 0383c36 commit 8e70db8

File tree

2 files changed

+37
-9
lines changed

2 files changed

+37
-9
lines changed

isort/identify.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ def imports(
7474
break
7575

7676
stripped_line = next_line.strip().split("#")[0]
77-
continue
77+
continue # pragma: no cover
7878

7979
line, *end_of_line_comment = raw_line.split("#", 1)
8080
statements = [line.strip() for line in line.split(";")]
@@ -88,7 +88,7 @@ def imports(
8888
elif line.startswith("from "):
8989
type_of_import = "from"
9090
else:
91-
continue
91+
continue # pragma: no cover
9292

9393
import_string, _ = parse_comments(line)
9494
normalized_import_string = (
@@ -135,13 +135,15 @@ def imports(
135135
break
136136
line, _ = parse_comments(next_line)
137137
import_string += "\n" + line
138-
139-
if import_string.strip().endswith(
140-
(" import", " cimport")
141-
) or line.strip().startswith(("import ", "cimport ")):
142-
import_string += "\n" + line
143138
else:
144-
import_string = import_string.rstrip().rstrip("\\") + " " + line.lstrip()
139+
if import_string.strip().endswith(
140+
(" import", " cimport")
141+
) or line.strip().startswith(("import ", "cimport ")):
142+
import_string += "\n" + line
143+
else:
144+
import_string = (
145+
import_string.rstrip().rstrip("\\") + " " + line.lstrip()
146+
)
145147

146148
if type_of_import == "from":
147149
import_string = (

tests/unit/test_identify.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from typing import List
33

44
from isort import Config, identify
5+
from isort.identify import Import
56

67

78
def imports_in_code(code: str, **kwargs) -> List[identify.Import]:
@@ -219,12 +220,37 @@ def test_complex_examples():
219220
from os (
220221
import path
221222
)
223+
from os import \\
224+
path
225+
from os \\
226+
import (
227+
path
228+
)
222229
from os import ( \\"""
223230
)
224231
)
225-
== 7
232+
== 9
226233
)
227234
assert not imports_in_code("from os import \\")
235+
assert (
236+
imports_in_code(
237+
"""
238+
from os \\
239+
import (
240+
system"""
241+
)
242+
== [
243+
Import(
244+
line_number=2,
245+
indented=False,
246+
module="os",
247+
attribute="system",
248+
alias=None,
249+
cimport=False,
250+
file_path=None,
251+
)
252+
]
253+
)
228254

229255

230256
def test_aliases():

0 commit comments

Comments
 (0)