Debian Codegraph @

test/completion/pep0484_generic_passthroughs.py

raw symbols SCIP
1 from typing import (
2 Any,
3 Callable,
4 Iterable,
5 List,
6 Sequence,
7 Tuple,
8 Type,
9 TypeVar,
10 Union,
11 Generic,
12 )
13
14 T = TypeVar('T')
15 U = TypeVar('U')
16 TList = TypeVar('TList', bound=List[Any])
17 TType = TypeVar('TType', bound=Type)
18 TTypeAny = TypeVar('TTypeAny', bound=Type[Any])
19 TCallable = TypeVar('TCallable', bound=Callable[..., Any])
20
21 untyped_list_str = ['abc', 'def']
22 typed_list_str: List[str] = ['abc', 'def']
23
24 untyped_tuple_str = ('abc',)
25 typed_tuple_str: Tuple[str] = ('abc',)
26
27 untyped_tuple_str_int = ('abc', 4)
28 typed_tuple_str_int: Tuple[str, int] = ('abc', 4)
29
30 variadic_tuple_str: Tuple[str, ...] = ('abc',)
31 variadic_tuple_str_int: Tuple[Union[str, int], ...] = ('abc', 4)
32
33
34 def untyped_passthrough(x):
35 return x
36
37 def typed_list_generic_passthrough(x: List[T]) -> List[T]:
38 return x
39
40 def typed_tuple_generic_passthrough(x: Tuple[T]) -> Tuple[T]:
41 return x
42
43 def typed_multi_typed_tuple_generic_passthrough(x: Tuple[T, U]) -> Tuple[U, T]:
44 return x[1], x[0]
45
46 def typed_variadic_tuple_generic_passthrough(x: Tuple[T, ...]) -> Sequence[T]:
47 return x
48
49 def typed_iterable_generic_passthrough(x: Iterable[T]) -> Iterable[T]:
50 return x
51
52 def typed_fully_generic_passthrough(x: T) -> T:
53 return x
54
55 def typed_bound_generic_passthrough(x: TList) -> TList:
56 #? list()
57 x
58
59 return x
60
61 # Forward references are more likely with custom types, however this aims to
62 # test just the handling of the quoted type rather than any other part of the
63 # machinery.
64 def typed_quoted_return_generic_passthrough(x: T) -> 'List[T]':
65 return [x]
66
67 def typed_quoted_input_generic_passthrough(x: 'Tuple[T]') -> T:
68 x
69 return x[0]
70
71
72 for a in untyped_passthrough(untyped_list_str):
73 #? str()
74 a
75
76 for b in untyped_passthrough(typed_list_str):
77 #? str()
78 b
79
80
81 for c in typed_list_generic_passthrough(untyped_list_str):
82 #? str()
83 c
84
85 for d in typed_list_generic_passthrough(typed_list_str):
86 #? str()
87 d
88
89
90 for e in typed_iterable_generic_passthrough(untyped_list_str):
91 #? str()
92 e
93
94 for f in typed_iterable_generic_passthrough(typed_list_str):
95 #? str()
96 f
97
98
99 for g in typed_tuple_generic_passthrough(untyped_tuple_str):
100 #? str()
101 g
102
103 for h in typed_tuple_generic_passthrough(typed_tuple_str):
104 #? str()
105 h
106
107
108 out_untyped = typed_multi_typed_tuple_generic_passthrough(untyped_tuple_str_int)
109 #? int()
110 out_untyped[0]
111 #? str()
112 out_untyped[1]
113
114
115 out_typed = typed_multi_typed_tuple_generic_passthrough(typed_tuple_str_int)
116 #? int()
117 out_typed[0]
118 #? str()
119 out_typed[1]
120
121
122 for j in typed_variadic_tuple_generic_passthrough(untyped_tuple_str_int):
123 #? str() int()
124 j
125
126 for k in typed_variadic_tuple_generic_passthrough(typed_tuple_str_int):
127 #? str() int()
128 k
129
130 for l in typed_variadic_tuple_generic_passthrough(variadic_tuple_str):
131 #? str()
132 l
133
134 for m in typed_variadic_tuple_generic_passthrough(variadic_tuple_str_int):
135 #? str() int()
136 m
137
138 #? float
139 typed_fully_generic_passthrough(float)
140
141 for n in typed_fully_generic_passthrough(untyped_list_str):
142 #? str()
143 n
144
145 for o in typed_fully_generic_passthrough(typed_list_str):
146 #? str()
147 o
148
149
150 for p in typed_bound_generic_passthrough(untyped_list_str):
151 #? str()
152 p
153
154 for q in typed_bound_generic_passthrough(typed_list_str):
155 #? str()
156 q
157
158
159 for r in typed_quoted_return_generic_passthrough("something"):
160 #? str()
161 r
162
163 for s in typed_quoted_return_generic_passthrough(42):
164 #? int()
165 s
166
167
168 #? str()
169 typed_quoted_input_generic_passthrough(("something",))
170
171 #? int()
172 typed_quoted_input_generic_passthrough((42,))
173
174
175
176 class CustomList(List):
177 def get_first(self):
178 return self[0]
179
180
181 #? str()
182 CustomList[str]()[0]
183 #? str()
184 CustomList[str]().get_first()
185
186 #? str()
187 typed_fully_generic_passthrough(CustomList[str]())[0]
188 #?
189 typed_list_generic_passthrough(CustomList[str])[0]
190
191
192 def typed_bound_type_implicit_any_generic_passthrough(x: TType) -> TType:
193 #? Type()
194 x
195 return x
196
197 def typed_bound_type_any_generic_passthrough(x: TTypeAny) -> TTypeAny:
198 # Should be Type(), though we don't get the handling of the nested argument
199 # to `Type[...]` quite right here.
200 x
201 return x
202
203
204 class MyClass:
205 pass
206
207 def my_func(a: str, b: int) -> float:
208 pass
209
210 #? MyClass
211 typed_fully_generic_passthrough(MyClass)
212
213 #? MyClass()
214 typed_fully_generic_passthrough(MyClass())
215
216 #? my_func
217 typed_fully_generic_passthrough(my_func)
218
219 #? CustomList()
220 typed_bound_generic_passthrough(CustomList[str]())
221
222 # should be list(), but we don't validate generic typevar upper bounds
223 #? int()
224 typed_bound_generic_passthrough(42)
225
226 #? MyClass
227 typed_bound_type_implicit_any_generic_passthrough(MyClass)
228
229 #? MyClass
230 typed_bound_type_any_generic_passthrough(MyClass)
231
232 # should be Type(), but we don't validate generic typevar upper bounds
233 #? int()
234 typed_bound_type_implicit_any_generic_passthrough(42)
235
236 # should be Type(), but we don't validate generic typevar upper bounds
237 #? int()
238 typed_bound_type_any_generic_passthrough(42)
239
240
241 def decorator(fn: TCallable) -> TCallable:
242 pass
243
244
245 def will_be_decorated(the_param: complex) -> float:
246 pass
247
248
249 is_decorated = decorator(will_be_decorated)
250
251 #? will_be_decorated
252 is_decorated
253
254 #? ['the_param=']
255 is_decorated(the_para
256 )
257
258
259 class class_decorator_factory_plain:
260 def __call__(self, func: T) -> T:
261 ...
262
263 #? class_decorator_factory_plain()
264 class_decorator_factory_plain()
265
266 #?
267 class_decorator_factory_plain()()
268
269 is_decorated_by_class_decorator_factory = class_decorator_factory_plain()(will_be_decorated)
270
271 #? will_be_decorated
272 is_decorated_by_class_decorator_factory
273
274 #? ['the_param=']
275 is_decorated_by_class_decorator_factory(the_par
276 )
277
278
279 def decorator_factory_plain() -> Callable[[T], T]:
280 pass
281
282 #? Callable()
283 decorator_factory_plain()
284
285 #?
286 decorator_factory_plain()()
287
288 #? int()
289 decorator_factory_plain()(42)
290
291 is_decorated_by_plain_factory = decorator_factory_plain()(will_be_decorated)
292
293 #? will_be_decorated
294 is_decorated_by_plain_factory
295
296 #? ['the_param=']
297 is_decorated_by_plain_factory(the_par
298 )
299
300
301 class class_decorator_factory_bound_callable:
302 def __call__(self, func: TCallable) -> TCallable:
303 ...
304
305 #? class_decorator_factory_bound_callable()
306 class_decorator_factory_bound_callable()
307
308 #? Callable()
309 class_decorator_factory_bound_callable()()
310
311 is_decorated_by_class_bound_factory = class_decorator_factory_bound_callable()(will_be_decorated)
312
313 #? will_be_decorated
314 is_decorated_by_class_bound_factory
315
316 #? ['the_param=']
317 is_decorated_by_class_bound_factory(the_par
318 )
319
320
321 def decorator_factory_bound_callable() -> Callable[[TCallable], TCallable]:
322 pass
323
324 #? Callable()
325 decorator_factory_bound_callable()
326
327 #? Callable()
328 decorator_factory_bound_callable()()
329
330 is_decorated_by_bound_factory = decorator_factory_bound_callable()(will_be_decorated)
331
332 #? will_be_decorated
333 is_decorated_by_bound_factory
334
335 #? ['the_param=']
336 is_decorated_by_bound_factory(the_par
337 )
338
339
340 class That(Generic[T]):
341 def __init__(self, items: List[Tuple[str, T]]) -> None:
342 pass
343
344 def get(self) -> T:
345 pass
346
347 inst = That([("abc", 2)])
348
349 # No completions here, but should have completions for `int`
350 #? int()
351 inst.get()