Debian Codegraph @

tests/unit/test_identify.py

raw
1 from io import StringIO
2 from typing import Any
3
4 from isort import Config, identify
5 from isort.identify import Import
6
7
8 def imports_in_code(code: str, **kwargs: Any) -> list[identify.Import]:
9 return list(identify.imports(StringIO(code), **kwargs))
10
11
12 def test_top_only() -> None:
13 imports_in_function = """
14 import abc
15
16 def xyz():
17 import defg
18 """
19 assert len(imports_in_code(imports_in_function)) == 2
20 assert len(imports_in_code(imports_in_function, top_only=True)) == 1
21
22 imports_after_class = """
23 import abc
24
25 class MyObject:
26 pass
27
28 import defg
29 """
30 assert len(imports_in_code(imports_after_class)) == 2
31 assert len(imports_in_code(imports_after_class, top_only=True)) == 1
32
33
34 def test_top_doc_string() -> None:
35 assert (
36 len(
37 imports_in_code(
38 '''
39 #! /bin/bash import x
40 """import abc
41 from y import z
42 """
43 import abc
44 '''
45 )
46 )
47 == 1
48 )
49
50
51 def test_yield_and_raise_edge_cases() -> None:
52 assert not imports_in_code(
53 """
54 raise SomeException("Blah") \\
55 from exceptionsInfo.popitem()[1]
56 """
57 )
58 assert not imports_in_code(
59 """
60 def generator_function():
61 yield \\
62 from other_function()[1]
63 """
64 )
65 assert (
66 len(
67 imports_in_code(
68 """
69 # one
70
71 # two
72
73
74 def function():
75 # three \\
76 import b
77 import a
78 """
79 )
80 )
81 == 2
82 )
83 assert (
84 len(
85 imports_in_code(
86 """
87 # one
88
89 # two
90
91
92 def function():
93 raise \\
94 import b
95 import a
96 """
97 )
98 )
99 == 1
100 )
101 assert not imports_in_code(
102 """
103 def generator_function():
104 (
105 yield
106 from other_function()[1]
107 )
108 """
109 )
110 assert not imports_in_code(
111 """
112 def generator_function():
113 (
114 (
115 ((((
116 (((((
117 ((
118 (((
119 yield
120
121
122
123 from other_function()[1]
124 )))))))))))))
125 )))
126 """
127 )
128 assert (
129 len(
130 imports_in_code(
131 """
132 def generator_function():
133 import os
134
135 yield \\
136 from other_function()[1]
137 """
138 )
139 )
140 == 1
141 )
142
143 assert not imports_in_code(
144 """
145 def generator_function():
146 (
147 (
148 ((((
149 (((((
150 ((
151 (((
152 yield
153 """
154 )
155 assert not imports_in_code(
156 """
157 def generator_function():
158 (
159 (
160 ((((
161 (((((
162 ((
163 (((
164 raise (
165 """
166 )
167 assert not imports_in_code(
168 """
169 def generator_function():
170 (
171 (
172 ((((
173 (((((
174 ((
175 (((
176 raise \\
177 from \\
178 """
179 )
180 assert (
181 len(
182 imports_in_code(
183 """
184 def generator_function():
185 (
186 (
187 ((((
188 (((((
189 ((
190 (((
191 raise \\
192 from \\
193 import c
194
195 import abc
196 import xyz
197 """
198 )
199 )
200 == 2
201 )
202
203
204 def test_complex_examples() -> None:
205 assert (
206 len(
207 imports_in_code(
208 """
209 import a, b, c; import n
210
211 x = (
212 1,
213 2,
214 3
215 )
216
217 import x
218 from os \\
219 import path
220 from os (
221 import path
222 )
223 from os import \\
224 path
225 from os \\
226 import (
227 path
228 )
229 from os import ( \\"""
230 )
231 )
232 == 9
233 )
234 assert not imports_in_code("from os import \\")
235 assert imports_in_code(
236 """
237 from os \\
238 import (
239 system"""
240 ) == [
241 Import(
242 line_number=2,
243 indented=False,
244 module="os",
245 attribute="system",
246 alias=None,
247 cimport=False,
248 file_path=None,
249 )
250 ]
251 assert imports_in_code(
252 """
253 from os \\
254 cimport (
255 system"""
256 ) == [
257 Import(
258 line_number=2,
259 indented=False,
260 module="os",
261 attribute="system",
262 alias=None,
263 cimport=True,
264 file_path=None,
265 )
266 ]
267
268
269 def test_aliases() -> None:
270 assert imports_in_code("import os as os")[0].alias == "os"
271 assert not imports_in_code(
272 "import os as os",
273 config=Config(
274 remove_redundant_aliases=True,
275 ),
276 )[0].alias
277
278 assert imports_in_code("from os import path as path")[0].alias == "path"
279 assert not imports_in_code(
280 "from os import path as path", config=Config(remove_redundant_aliases=True)
281 )[0].alias
282
283
284 def test_indented() -> None:
285 assert not imports_in_code("import os")[0].indented
286 assert imports_in_code(" import os")[0].indented
287 assert imports_in_code("\timport os")[0].indented