Debian Codegraph @

test/completion/pep0484_generic_parameters.py

raw symbols SCIP
1 from typing import (
2 Callable,
3 Dict,
4 Generic,
5 Iterable,
6 List,
7 Mapping,
8 Optional,
9 Tuple,
10 Type,
11 TypeVar,
12 Union,
13 Sequence,
14 Self,
15 )
16
17 K = TypeVar('K')
18 T = TypeVar('T')
19 T_co = TypeVar('T_co', covariant=True)
20 V = TypeVar('V')
21
22
23 just_float: float = 42.
24 optional_float: Optional[float] = 42.
25 list_of_ints: List[int] = [42]
26 list_of_floats: List[float] = [42.]
27 list_of_optional_floats: List[Optional[float]] = [x or None for x in list_of_floats]
28 list_of_ints_and_strs: List[Union[int, str]] = [42, 'abc']
29
30 # Test that simple parameters are handled
31 def list_t_to_list_t(the_list: List[T]) -> List[T]:
32 return the_list
33
34 x0 = list_t_to_list_t(list_of_ints)[0]
35 #? int()
36 x0
37
38 for a in list_t_to_list_t(list_of_ints):
39 #? int()
40 a
41
42 # Test that unions are handled
43 x2 = list_t_to_list_t(list_of_ints_and_strs)[0]
44 #? int() str()
45 x2
46
47 for z in list_t_to_list_t(list_of_ints_and_strs):
48 #? int() str()
49 z
50
51
52 list_of_int_type: List[Type[int]] = [int]
53
54 # Test that nested parameters are handled
55 def list_optional_t_to_list_t(the_list: List[Optional[T]]) -> List[T]:
56 return [x for x in the_list if x is not None]
57
58
59 for xa in list_optional_t_to_list_t(list_of_optional_floats):
60 #? float()
61 xa
62
63 # Under covariance rules this is strictly incorrect (because List is mutable,
64 # the function would be allowed to put `None`s into our List[float], which would
65 # be bad), however we don't expect jedi to enforce that.
66 for xa1 in list_optional_t_to_list_t(list_of_floats):
67 #? float()
68 xa1
69
70
71 def optional_t_to_list_t(x: Optional[T]) -> List[T]:
72 return [x] if x is not None else []
73
74
75 for xb in optional_t_to_list_t(optional_float):
76 #? float()
77 xb
78
79
80 for xb2 in optional_t_to_list_t(just_float):
81 #? float()
82 xb2
83
84
85 def optional_list_t_to_list_t(x: Optional[List[T]]) -> List[T]:
86 return x if x is not None else []
87
88
89 optional_list_float: Optional[List[float]] = None
90 for xc in optional_list_t_to_list_t(optional_list_float):
91 #? float()
92 xc
93
94 for xc2 in optional_list_t_to_list_t(list_of_floats):
95 #? float()
96 xc2
97
98
99 def list_type_t_to_list_t(the_list: List[Type[T]]) -> List[T]:
100 return [x() for x in the_list]
101
102
103 x1 = list_type_t_to_list_t(list_of_int_type)[0]
104 #? int()
105 x1
106
107
108 for b in list_type_t_to_list_t(list_of_int_type):
109 #? int()
110 b
111
112
113 # Test construction of nested generic tuple return parameters
114 def list_t_to_list_tuple_t(the_list: List[T]) -> List[Tuple[T]]:
115 return [(x,) for x in the_list]
116
117
118 x1t = list_t_to_list_tuple_t(list_of_ints)[0][0]
119 #? int()
120 x1t
121
122
123 for c1 in list_t_to_list_tuple_t(list_of_ints):
124 #? int()
125 c1[0]
126
127
128 for c2, in list_t_to_list_tuple_t(list_of_ints):
129 #? int()
130 c2
131
132
133 # Test handling of nested tuple input parameters
134 def list_tuple_t_to_tuple_list_t(the_list: List[Tuple[T]]) -> Tuple[List[T], ...]:
135 return tuple(list(x) for x in the_list)
136
137
138 list_of_int_tuples: List[Tuple[int]] = [(x,) for x in list_of_ints]
139
140 for b in list_tuple_t_to_tuple_list_t(list_of_int_tuples):
141 #? int()
142 b[0]
143
144
145 def list_tuple_t_elipsis_to_tuple_list_t(the_list: List[Tuple[T, ...]]) -> Tuple[List[T], ...]:
146 return tuple(list(x) for x in the_list)
147
148
149 list_of_int_tuple_elipsis: List[Tuple[int, ...]] = [tuple(list_of_ints)]
150
151 for b in list_tuple_t_elipsis_to_tuple_list_t(list_of_int_tuple_elipsis):
152 #? int()
153 b[0]
154
155
156 # Test handling of nested callables
157 def foo(x: int) -> int:
158 return x
159
160
161 list_of_funcs: List[Callable[[int], int]] = [foo]
162
163 def list_func_t_to_list_func_type_t(the_list: List[Callable[[T], T]]) -> List[Callable[[Type[T]], T]]:
164 def adapt(func: Callable[[T], T]) -> Callable[[Type[T]], T]:
165 def wrapper(typ: Type[T]) -> T:
166 return func(typ())
167 return wrapper
168 return [adapt(x) for x in the_list]
169
170
171 for b in list_func_t_to_list_func_type_t(list_of_funcs):
172 #? int()
173 b(int)
174
175
176 def bar(*a, **k) -> int:
177 return len(a) + len(k)
178
179
180 list_of_funcs_2: List[Callable[..., int]] = [bar]
181
182 def list_func_t_passthrough(the_list: List[Callable[..., T]]) -> List[Callable[..., T]]:
183 return the_list
184
185
186 for b in list_func_t_passthrough(list_of_funcs_2):
187 #? int()
188 b(None, x="x")
189
190
191 mapping_int_str: Dict[int, str] = {42: 'a'}
192
193 # Test that mappings (that have more than one parameter) are handled
194 def invert_mapping(mapping: Mapping[K, V]) -> Mapping[V, K]:
195 return {v: k for k, v in mapping.items()}
196
197 #? int()
198 invert_mapping(mapping_int_str)['a']
199
200
201 # Test that the right type is chosen when a mapping is passed to something with
202 # only a single parameter. This checks that our inheritance checking picks the
203 # right thing.
204 def first(iterable: Iterable[T]) -> T:
205 return next(iter(iterable))
206
207 #? int()
208 first(mapping_int_str)
209
210 # Test inference of str as an iterable of str.
211 #? str()
212 first("abc")
213
214 some_str: str = NotImplemented
215 #? str()
216 first(some_str)
217
218 annotated: List[ Callable[[Sequence[float]], int] ] = [len]
219 #? int()
220 first(annotated)()
221
222 # Test that the right type is chosen when a partially realised mapping is expected
223 def values(mapping: Mapping[int, T]) -> List[T]:
224 return list(mapping.values())
225
226 #? str()
227 values(mapping_int_str)[0]
228
229 x2 = values(mapping_int_str)[0]
230 #? str()
231 x2
232
233 for b in values(mapping_int_str):
234 #? str()
235 b
236
237
238 #
239 # Tests that user-defined generic types are handled
240 #
241 list_ints: List[int] = [42]
242
243 class CustomGeneric(Generic[T_co]):
244 def __init__(self, val: T_co) -> None:
245 self.val = val
246
247
248 # Test extraction of type from a custom generic type
249 def custom(x: CustomGeneric[T]) -> T:
250 return x.val
251
252 custom_instance: CustomGeneric[int] = CustomGeneric(42)
253
254 #? int()
255 custom(custom_instance)
256
257 x3 = custom(custom_instance)
258 #? int()
259 x3
260
261
262 # Test construction of a custom generic type
263 def wrap_custom(iterable: Iterable[T]) -> List[CustomGeneric[T]]:
264 return [CustomGeneric(x) for x in iterable]
265
266 #? int()
267 wrap_custom(list_ints)[0].val
268
269 x4 = wrap_custom(list_ints)[0]
270 #? int()
271 x4.val
272
273 for x5 in wrap_custom(list_ints):
274 #? int()
275 x5.val
276
277
278 # Test extraction of type from a nested custom generic type
279 list_custom_instances: List[CustomGeneric[int]] = [CustomGeneric(42)]
280
281 def unwrap_custom(iterable: Iterable[CustomGeneric[T]]) -> List[T]:
282 return [x.val for x in iterable]
283
284 #? int()
285 unwrap_custom(list_custom_instances)[0]
286
287 x6 = unwrap_custom(list_custom_instances)[0]
288 #? int()
289 x6
290
291 for x7 in unwrap_custom(list_custom_instances):
292 #? int()
293 x7
294
295
296 for xc in unwrap_custom([CustomGeneric(s) for s in 'abc']):
297 #? str()
298 xc
299
300
301 for xg in unwrap_custom(CustomGeneric(s) for s in 'abc'):
302 #? str()
303 xg
304
305
306 # Test extraction of type from type parameer nested within a custom generic type
307 custom_instance_list_int: CustomGeneric[List[int]] = CustomGeneric([42])
308
309 def unwrap_custom2(instance: CustomGeneric[Iterable[T]]) -> List[T]:
310 return list(instance.val)
311
312 #? int()
313 unwrap_custom2(custom_instance_list_int)[0]
314
315 x8 = unwrap_custom2(custom_instance_list_int)[0]
316 #? int()
317 x8
318
319 for x9 in unwrap_custom2(custom_instance_list_int):
320 #? int()
321 x9
322
323
324 # Test that classes which have generic parents but are not generic themselves
325 # are still inferred correctly.
326 class Specialised(Mapping[int, str]):
327 pass
328
329
330 specialised_instance: Specialised = NotImplemented
331
332 #? int()
333 first(specialised_instance)
334
335 #? str()
336 values(specialised_instance)[0]
337
338
339 # Test that classes which have generic ancestry but neither they nor their
340 # parents are not generic are still inferred correctly.
341 class ChildOfSpecialised(Specialised):
342 pass
343
344
345 child_of_specialised_instance: ChildOfSpecialised = NotImplemented
346
347 #? int()
348 first(child_of_specialised_instance)
349
350 #? str()
351 values(child_of_specialised_instance)[0]
352
353
354 # Test that unbound generics are inferred as much as possible
355 class CustomPartialGeneric1(Mapping[str, T]):
356 pass
357
358
359 custom_partial1_instance: CustomPartialGeneric1[int] = NotImplemented
360
361 #? str()
362 first(custom_partial1_instance)
363
364
365 custom_partial1_unbound_instance: CustomPartialGeneric1 = NotImplemented
366
367 #? str()
368 first(custom_partial1_unbound_instance)
369
370
371 class CustomPartialGeneric2(Mapping[T, str]):
372 pass
373
374
375 custom_partial2_instance: CustomPartialGeneric2[int] = NotImplemented
376
377 #? int()
378 first(custom_partial2_instance)
379
380 #? str()
381 values(custom_partial2_instance)[0]
382
383
384 custom_partial2_unbound_instance: CustomPartialGeneric2 = NotImplemented
385
386 #? []
387 first(custom_partial2_unbound_instance)
388
389 #? str()
390 values(custom_partial2_unbound_instance)[0]
391
392 def generic_func1(arg: T) -> int | str | T: pass
393 def generic_func2(arg: T) -> Union[int, str, T]: pass
394
395 #? int() str() bytes()
396 generic_func1(b"hello")
397 #? int() str() bytes()
398 generic_func2(b"hello")
399
400 class CustomGeneric2(Generic[T_co]):
401 val: T_co
402 def __init__(cls, val: T_co) -> Self:
403 raise NotImplementedError
404
405 #? int()
406 CustomGeneric2(1).val