File tree 1 file changed +139
-0
lines changed 1 file changed +139
-0
lines changed Original file line number Diff line number Diff line change
1
+ from io import StringIO
2
+ from typing import List
3
+
4
+ from isort import identify
5
+
6
+
7
+ def imports_in_code (code : str , ** kwargs ) -> List [identify .Import ]:
8
+ return list (identify .imports (StringIO (code , ** kwargs )))
9
+
10
+
11
+ def test_yield_edge_cases ():
12
+ assert not imports_in_code (
13
+ """
14
+ raise SomeException("Blah") \\
15
+ from exceptionsInfo.popitem()[1]
16
+ """
17
+ )
18
+ assert not imports_in_code (
19
+ """
20
+ def generator_function():
21
+ yield \\
22
+ from other_function()[1]
23
+ """
24
+ )
25
+ assert (
26
+ len (
27
+ imports_in_code (
28
+ """
29
+ # one
30
+
31
+ # two
32
+
33
+
34
+ def function():
35
+ # three \\
36
+ import b
37
+ import a
38
+ """
39
+ )
40
+ )
41
+ == 2
42
+ )
43
+ assert (
44
+ len (
45
+ imports_in_code (
46
+ """
47
+ # one
48
+
49
+ # two
50
+
51
+
52
+ def function():
53
+ raise \\
54
+ import b
55
+ import a
56
+ """
57
+ )
58
+ )
59
+ == 1
60
+ )
61
+ assert not imports_in_code (
62
+ """
63
+ def generator_function():
64
+ (
65
+ yield
66
+ from other_function()[1]
67
+ )
68
+ """
69
+ )
70
+ assert not imports_in_code (
71
+ """
72
+ def generator_function():
73
+ (
74
+ (
75
+ ((((
76
+ (((((
77
+ ((
78
+ (((
79
+ yield
80
+
81
+
82
+
83
+ from other_function()[1]
84
+ )))))))))))))
85
+ )))
86
+ """
87
+ )
88
+ assert (
89
+ len (
90
+ imports_in_code (
91
+ """
92
+ def generator_function():
93
+ import os
94
+
95
+ yield \\
96
+ from other_function()[1]
97
+ """
98
+ )
99
+ )
100
+ == 1
101
+ )
102
+
103
+ assert not imports_in_code (
104
+ """
105
+ def generator_function():
106
+ (
107
+ (
108
+ ((((
109
+ (((((
110
+ ((
111
+ (((
112
+ yield
113
+ """
114
+ )
115
+ assert not imports_in_code (
116
+ """
117
+ def generator_function():
118
+ (
119
+ (
120
+ ((((
121
+ (((((
122
+ ((
123
+ (((
124
+ raise (
125
+ """
126
+ )
127
+ assert not imports_in_code (
128
+ """
129
+ def generator_function():
130
+ (
131
+ (
132
+ ((((
133
+ (((((
134
+ ((
135
+ (((
136
+ raise \\
137
+ from \\
138
+ """
139
+ )
You can’t perform that action at this time.
0 commit comments