File tree 1 file changed +63
-2
lines changed
1 file changed +63
-2
lines changed Original file line number Diff line number Diff line change 5
5
6
6
7
7
def imports_in_code (code : str , ** kwargs ) -> List [identify .Import ]:
8
- return list (identify .imports (StringIO (code , ** kwargs ) ))
8
+ return list (identify .imports (StringIO (code ) , ** kwargs ))
9
9
10
10
11
- def test_yield_edge_cases ():
11
+ def test_top_only ():
12
+ imports_in_function = """
13
+ import abc
14
+
15
+ def xyz():
16
+ import defg
17
+ """
18
+ assert len (imports_in_code (imports_in_function )) == 2
19
+ assert len (imports_in_code (imports_in_function , top_only = True )) == 1
20
+
21
+ imports_after_class = """
22
+ import abc
23
+
24
+ class MyObject:
25
+ pass
26
+
27
+ import defg
28
+ """
29
+ assert len (imports_in_code (imports_after_class )) == 2
30
+ assert len (imports_in_code (imports_after_class , top_only = True )) == 1
31
+
32
+
33
+ def test_top_doc_string ():
34
+ assert (
35
+ len (
36
+ imports_in_code (
37
+ '''
38
+ #! /bin/bash import x
39
+ """import abc
40
+ from y import z
41
+ """
42
+ import abc
43
+ '''
44
+ )
45
+ )
46
+ == 1
47
+ )
48
+
49
+
50
+ def test_yield_and_raise_edge_cases ():
12
51
assert not imports_in_code (
13
52
"""
14
53
raise SomeException("Blah") \\
@@ -137,3 +176,25 @@ def generator_function():
137
176
from \\
138
177
"""
139
178
)
179
+ assert (
180
+ len (
181
+ imports_in_code (
182
+ """
183
+ def generator_function():
184
+ (
185
+ (
186
+ ((((
187
+ (((((
188
+ ((
189
+ (((
190
+ raise \\
191
+ from \\
192
+ import c
193
+
194
+ import abc
195
+ import xyz
196
+ """
197
+ )
198
+ )
199
+ == 2
200
+ )
You can’t perform that action at this time.
0 commit comments