Debian Codegraph @

test/completion/generators.py

raw symbols SCIP
1 # -----------------
2 # yield statement
3 # -----------------
4 def gen():
5 if random.choice([0, 1]):
6 yield 1
7 else:
8 yield ""
9
10 gen_exe = gen()
11 #? int() str()
12 next(gen_exe)
13
14 #? int() str() list
15 next(gen_exe, list)
16
17
18 def gen_ret(value):
19 yield value
20
21 #? int()
22 next(gen_ret(1))
23
24 #? []
25 next(gen_ret()).
26
27 # generators infer to true if cast by bool.
28 a = ''
29 if gen_ret():
30 a = 3
31 #? int()
32 a
33
34
35 # -----------------
36 # generators should not be indexable
37 # -----------------
38 def get(param):
39 if random.choice([0, 1]):
40 yield 1
41 else:
42 yield ""
43
44 #? []
45 get()[0].
46
47 # -----------------
48 # __iter__
49 # -----------------
50 for a in get():
51 #? int() str()
52 a
53
54
55 class Get():
56 def __iter__(self):
57 if random.choice([0, 1]):
58 yield 1
59 else:
60 yield ""
61
62 b = []
63 for a in Get():
64 #? int() str()
65 a
66 b += [a]
67
68 #? list()
69 b
70 #? int() str()
71 b[0]
72
73 g = iter(Get())
74 #? int() str()
75 next(g)
76
77 g = iter([1.0])
78 #? float()
79 next(g)
80
81 x, y = Get()
82 #? int() str()
83 x
84 #? int() str()
85 x
86
87 class Iter:
88 def __iter__(self):
89 yield ""
90 i = 0
91 while True:
92 v = 1
93 yield v
94 i += 1
95 a, b, c = Iter()
96 #? str() int()
97 a
98 #? str() int()
99 b
100 #? str() int()
101 c
102
103
104 # -----------------
105 # __next__
106 # -----------------
107 class Counter:
108 def __init__(self, low, high):
109 self.current = low
110 self.high = high
111
112 def __iter__(self):
113 return self
114
115 def next(self):
116 """ need to have both __next__ and next, because of py2/3 testing """
117 return self.__next__()
118
119 def __next__(self):
120 if self.current > self.high:
121 raise StopIteration
122 else:
123 self.current += 1
124 return self.current - 1
125
126
127 for c in Counter(3, 8):
128 #? int()
129 print c
130
131
132 # -----------------
133 # tuple assignments
134 # -----------------
135 def gen():
136 if random.choice([0,1]):
137 yield 1, ""
138 else:
139 yield 2, 1.0
140
141
142 a, b = next(gen())
143 #? int()
144 a
145 #? str() float()
146 b
147
148
149 def simple():
150 if random.choice([0, 1]):
151 yield 1
152 else:
153 yield ""
154
155 a, b = simple()
156 #? int() str()
157 a
158 # For now this is ok.
159 #? int() str()
160 b
161
162
163 def simple2():
164 yield 1
165 yield ""
166
167 a, b = simple2()
168 #? int()
169 a
170 #? str()
171 b
172
173 a, = (a for a in [1])
174 #? int()
175 a
176
177 # -----------------
178 # More complicated access
179 # -----------------
180
181 # `close` is a method wrapper.
182 #? ['__call__']
183 gen().close.__call__
184
185 #?
186 gen().throw()
187
188 #? ['co_consts']
189 gen().gi_code.co_consts
190
191 #? []
192 gen.gi_code.co_consts
193
194 # `send` is also a method wrapper.
195 #? ['__call__']
196 gen().send.__call__
197
198 #? tuple()
199 gen().send()
200
201 #?
202 gen()()
203
204 # -----------------
205 # empty yield
206 # -----------------
207
208 def x():
209 yield
210
211 #? None
212 next(x())
213 #? gen()
214 x()
215
216 def x():
217 for i in range(3):
218 yield
219
220 #? None
221 next(x())
222
223 # -----------------
224 # yield in expression
225 # -----------------
226
227 def x():
228 a= [(yield 1)]
229
230 #? int()
231 next(x())
232
233 # -----------------
234 # statements
235 # -----------------
236 def x():
237 foo = yield
238 #?
239 foo
240
241 # -----------------
242 # yield from
243 # -----------------
244
245 def yield_from():
246 yield from iter([1])
247
248 #? int()
249 next(yield_from())
250
251 def yield_from_multiple():
252 yield from iter([1])
253 yield str()
254 return 2.0
255
256 x, y = yield_from_multiple()
257 #? int()
258 x
259 #? str()
260 y
261
262 def test_nested():
263 x = yield from yield_from_multiple()
264 #? float()
265 x
266 yield x
267
268 x, y, z = test_nested()
269 #? int()
270 x
271 #? str()
272 y
273 # For whatever reason this is currently empty
274 #? float()
275 z
276
277
278 def test_in_brackets():
279 x = 1 + (yield from yield_from_multiple())
280 #? float()
281 x
282
283 generator = (1 for 1 in [1])
284 x = yield from generator
285 #? None
286 x
287 x = yield from 1
288 #?
289 x
290 x = yield from [1]
291 #? None
292 x
293
294
295 # -----------------
296 # Annotations
297 # -----------------
298
299 from typing import Iterator
300
301 def annotation1() -> float:
302 yield 1
303
304 def annotation2() -> Iterator[float]:
305 yield 1
306
307
308 #?
309 next(annotation1())
310 #? float()
311 next(annotation2())
312
313
314 # annotations should override generator inference
315 #? float()
316 annotation1()