Debian Codegraph @

test/completion/pep0484_typing.py

raw symbols SCIP
1 """
2 Test the typing library, with docstrings and annotations
3 """
4 import typing
5 from typing import Sequence, MutableSequence, List, Iterable, Iterator, \
6 AbstractSet, Tuple, Mapping, Dict, Union, Optional, Final, Self
7
8 class B:
9 pass
10
11 def we_can_has_sequence(p: Sequence[int], q: Sequence[B], r: Sequence[int],
12 s: Sequence["int"], t: MutableSequence[dict], u: List[float]):
13 #? ["count"]
14 p.c
15 #? int()
16 p[1]
17 #? ["count"]
18 q.c
19 #? B()
20 q[1]
21 #? ["count"]
22 r.c
23 #? int()
24 r[1]
25 #? ["count"]
26 s.c
27 #? int()
28 s[1]
29 #? []
30 s.a
31 #? ["append"]
32 t.a
33 #? dict()
34 t[1]
35 #? ["append"]
36 u.a
37 #? float() list()
38 u[1.0]
39 #? float()
40 u[1]
41
42 def iterators(ps: Iterable[int], qs: Iterator[str], rs:
43 Sequence["ForwardReference"], ts: AbstractSet["float"]):
44 for p in ps:
45 #? int()
46 p
47 #?
48 next(ps)
49 a, b = ps
50 #? int()
51 a
52 #? int()
53 b
54
55 for q in qs:
56 #? str()
57 q
58 #? str()
59 next(qs)
60 for r in rs:
61 #? ForwardReference()
62 r
63 #?
64 next(rs)
65 for t in ts:
66 #? float()
67 t
68
69 def sets(p: AbstractSet[int], q: typing.MutableSet[float]):
70 #? []
71 p.a
72 #? ["add"]
73 q.a
74
75 def tupletest(p: Tuple[int], q: Tuple[int, str, float], r: Tuple[B, ...]):
76 #? int()
77 p[0]
78 #? ['index']
79 p.index
80 #? int()
81 q[0]
82 #? str()
83 q[1]
84 #? float()
85 q[2]
86 #? B()
87 r[0]
88 #? B()
89 r[1]
90 #? B()
91 r[2]
92 #? B()
93 r[10000]
94 i, s, f = q
95 #? int()
96 i
97 #? str()
98 s
99 #? float()
100 f
101
102 class Key:
103 pass
104
105 class Value:
106 pass
107
108 def mapping(
109 p: Mapping[Key, Value],
110 q: typing.MutableMapping[Key, Value],
111 d: Dict[Key, Value],
112 dd: typing.DefaultDict[Key, Value],
113 r: typing.KeysView[Key],
114 s: typing.ValuesView[Value],
115 t: typing.ItemsView[Key, Value]):
116 #? []
117 p.setd
118 #? ["setdefault"]
119 q.setd
120 #? ["setdefault"]
121 d.setd
122 #? ["setdefault"]
123 dd.setd
124 #? Value()
125 p[1]
126 for key in p:
127 #? Key()
128 key
129 for key in p.keys():
130 #? Key()
131 key
132 for value in p.values():
133 #? Value()
134 value
135 for item in p.items():
136 #? Key()
137 item[0]
138 #? Value()
139 item[1]
140 (key, value) = item
141 #? Key()
142 key
143 #? Value()
144 value
145 for key, value in p.items():
146 #? Key()
147 key
148 #? Value()
149 value
150 for key, value in q.items():
151 #? Key()
152 key
153 #? Value()
154 value
155 for key, value in d.items():
156 #? Key()
157 key
158 #? Value()
159 value
160 for key, value in dd.items():
161 #? Key()
162 key
163 #? Value()
164 value
165 for key in r:
166 #? Key()
167 key
168 for value in s:
169 #? Value()
170 value
171 for key, value in t:
172 #? Key()
173 key
174 #? Value()
175 value
176
177 def union(
178 p: Union[int],
179 q: Union[int, int],
180 r: Union[int, str, "int"],
181 s: Union[int, typing.Union[str, "typing.Union['float', 'dict']"]],
182 t: Union[int, None]):
183 #? int()
184 p
185 #? int()
186 q
187 #? int() str()
188 r
189 #? int() str() float() dict()
190 s
191 #? int() None
192 t
193
194 def optional(p: Optional[int]):
195 """
196 Optional does not do anything special. However it should be recognised
197 as being of that type. Jedi doesn't do anything with the extra into that
198 it can be None as well
199 """
200 #? int() None
201 p
202
203 class ForwardReference:
204 pass
205
206 class TestDict(typing.Dict[str, int]):
207 def setdud(self):
208 pass
209
210 def testdict(x: TestDict):
211 #? ["setdud", "setdefault"]
212 x.setd
213 for key in x.keys():
214 #? str()
215 key
216 for value in x.values():
217 #? int()
218 value
219
220 x = TestDict()
221 #? ["setdud", "setdefault"]
222 x.setd
223 for key in x.keys():
224 #? str()
225 key
226 for value in x.values():
227 #? int()
228 value
229
230 WrappingType = typing.NewType('WrappingType', str) # Chosen arbitrarily
231 y = WrappingType(0) # Per https://github.com/davidhalter/jedi/issues/1015#issuecomment-355795929
232 #? str()
233 y
234
235 def testnewtype(y: WrappingType):
236 #? str()
237 y
238 #? ["upper"]
239 y.u
240
241 WrappingType2 = typing.NewType()
242
243 def testnewtype2(y: WrappingType2):
244 #?
245 y
246 #? []
247 y.
248
249 # The type of a NewType is equivalent to the type of its underlying type.
250 MyInt = typing.NewType('MyInt', int)
251 x = type(MyInt)
252 #? type.mro
253 x.mro
254
255 PlainInt = int
256 y = type(PlainInt)
257 #? type.mro
258 y.mro
259
260 class TestDefaultDict(typing.DefaultDict[str, int]):
261 def setdud(self):
262 pass
263
264 def testdict(x: TestDefaultDict):
265 #? ["setdud", "setdefault"]
266 x.setd
267 for key in x.keys():
268 #? str()
269 key
270 for value in x.values():
271 #? int()
272 value
273
274 x = TestDefaultDict()
275 #? ["setdud", "setdefault"]
276 x.setd
277 for key in x.keys():
278 #? str()
279 key
280 for value in x.values():
281 #? int()
282 value
283
284
285 """
286 docstrings have some auto-import, annotations can use all of Python's
287 import logic
288 """
289 import typing as t
290 def union2(x: t.Union[int, str]):
291 #? int() str()
292 x
293 from typing import Union
294 def union3(x: Union[int, str]):
295 #? int() str()
296 x
297
298 from typing import Union as U
299 def union4(x: U[int, str]):
300 #? int() str()
301 x
302
303 #? typing.Optional
304 typing.Optional[0]
305
306 # -------------------------
307 # Type Vars
308 # -------------------------
309
310 TYPE_VARX = typing.TypeVar('TYPE_VARX')
311 TYPE_VAR_CONSTRAINTSX = typing.TypeVar('TYPE_VAR_CONSTRAINTSX', str, int)
312 #? ['__class__']
313 TYPE_VARX.__clas
314 #! ["TYPE_VARX = typing.TypeVar('TYPE_VARX')"]
315 TYPE_VARX
316
317
318 class WithTypeVar(typing.Generic[TYPE_VARX]):
319 def lala(self) -> TYPE_VARX:
320 ...
321
322
323 def maaan(p: WithTypeVar[int]):
324 #? int()
325 p.lala()
326
327 def in_out1(x: TYPE_VARX) -> TYPE_VARX: ...
328
329 #? int()
330 in_out1(1)
331 #? str()
332 in_out1("")
333 #? str()
334 in_out1(str())
335 #?
336 in_out1()
337
338 def type_in_out1(x: typing.Type[TYPE_VARX]) -> TYPE_VARX: ...
339
340 #? int()
341 type_in_out1(int)
342 #? str()
343 type_in_out1(str)
344 #? float()
345 type_in_out1(float)
346 #?
347 type_in_out1()
348
349 def in_out2(x: TYPE_VAR_CONSTRAINTSX) -> TYPE_VAR_CONSTRAINTSX: ...
350
351 #? int()
352 in_out2(1)
353 #? str()
354 in_out2("")
355 #? str()
356 in_out2(str())
357 #? str() int()
358 in_out2()
359 # TODO this should actually be str() int(), because of the constraints.
360 #? float()
361 in_out2(1.0)
362
363 def type_in_out2(x: typing.Type[TYPE_VAR_CONSTRAINTSX]) -> TYPE_VAR_CONSTRAINTSX: ...
364
365 #? int()
366 type_in_out2(int)
367 #? str()
368 type_in_out2(str)
369 #? str() int()
370 type_in_out2()
371 # TODO this should actually be str() int(), because of the constraints.
372 #? float()
373 type_in_out2(float)
374
375 def ma(a: typing.Callable[[str], TYPE_VARX]) -> typing.Callable[[str], TYPE_VARX]:
376 #? typing.Callable()
377 return a
378
379 def mf(s: str) -> int:
380 return int(s)
381
382 #? int()
383 ma(mf)('2')
384
385 def xxx(x: typing.Iterable[TYPE_VARX]) -> typing.Tuple[str, TYPE_VARX]: ...
386
387 #? str()
388 xxx([0])[0]
389 #? int()
390 xxx([0])[1]
391 #?
392 xxx([0])[2]
393
394 def call_pls() -> typing.Callable[[TYPE_VARX], TYPE_VARX]: ...
395 #? int()
396 call_pls()(1)
397
398 def call2_pls() -> typing.Callable[[str, typing.Callable[[int], TYPE_VARX]], TYPE_VARX]: ...
399 #? float()
400 call2_pls('')(1, lambda x: 3.0)
401
402 def call3_pls() -> typing.Callable[[typing.Callable[[int], TYPE_VARX]], typing.List[TYPE_VARX]]: ...
403 def the_callable() -> float: ...
404 #? float()
405 call3_pls()(the_callable)[0]
406
407 def call4_pls(fn: typing.Callable[..., TYPE_VARX]) -> typing.Callable[..., TYPE_VARX]:
408 return ""
409
410 #? int()
411 call4_pls(lambda x: 1)()
412
413 # -------------------------
414 # TYPE_CHECKING
415 # -------------------------
416
417 if typing.TYPE_CHECKING:
418 with_type_checking = 1
419 else:
420 without_type_checking = 1.0
421 #? int()
422 with_type_checking
423 #?
424 without_type_checking
425
426 def foo(a: typing.List, b: typing.Dict, c: typing.MutableMapping) -> typing.Type[int]:
427 #? ['append']
428 a.appen
429 #? list()
430 a
431 #?
432 a[0]
433 #? ['setdefault']
434 b.setd
435 #? ['setdefault']
436 c.setd
437 #? typing.MutableMapping()
438 c
439 #?
440 c['asdf']
441 #? int
442 foo()
443
444 # -------------------------
445 # cast
446 # -------------------------
447
448 def cast_tests():
449 x = 3.0
450 y = typing.cast(int, x)
451 #? int()
452 y
453 return typing.cast(str, x)
454
455
456 #? str()
457 cast_tests()
458
459
460 # -------------------------
461 # dynamic
462 # -------------------------
463
464 def dynamic_annotation(x: int):
465 #? int()
466 return x
467
468 #? int()
469 dynamic_annotation('')
470
471 # -------------------------
472 # TypeDict
473 # -------------------------
474
475 class Foo(typing.TypedDict):
476 foo: str
477 bar: typing.List[float]
478 an_int: int
479 #! ['foo: str']
480 foo
481 #? str()
482 foo
483 #? int()
484 an_int
485
486 def typed_dict_test_foo(arg: Foo):
487 a_string = arg['foo']
488 a_list_of_floats = arg['bar']
489 an_int = arg['an_int']
490
491 #? str()
492 a_string
493 #? list()
494 a_list_of_floats
495 #? float()
496 a_list_of_floats[0]
497 #? int()
498 an_int
499
500 #? ['isupper']
501 a_string.isuppe
502 #? ['pop']
503 a_list_of_floats.po
504 #? ['as_integer_ratio']
505 an_int.as_integer_rati
506
507 #! ['class Foo']
508 d: Foo
509 #? str()
510 d['foo']
511 #? float()
512 d['bar'][0]
513 #?
514 d['baz']
515
516 #?
517 d.foo
518 #?
519 d.bar
520 #! []
521 d.foo
522
523 #? []
524 Foo.set
525 #? ['setdefault']
526 d.setdefaul
527 #? []
528 Foo.setdefaul
529
530 #? 5 ["'foo"]
531 d['fo']
532 #? 5 ['"bar"']
533 d["bar"]
534
535 class Bar(Foo):
536 another_variable: int
537
538 #? int()
539 another_variable
540 #?
541 an_int
542
543 def typed_dict_test_foo(arg: Bar):
544 #? str()
545 arg['foo']
546 #? list()
547 arg['bar']
548 #? float()
549 arg['bar'][0]
550 #? int()
551 arg['an_int']
552 #? int()
553 arg['another_variable']
554
555 # -----------------
556 # Self
557 # -----------------
558
559 import typing_extensions
560
561 # From #2023, #2068
562 class Builder:
563 def __init__(self):
564 self.x = 0
565 self.y = 0
566
567 def add_x(self: Self, x: int) -> Self:
568 self.x = x
569 return self
570
571 def add_y(self: Self, y: int) -> Self:
572 self.y = y
573 return self
574
575 def add_not_implemented(self: Self, y: int) -> Self:
576 raise NotImplementedError
577
578 def add_not_implemented_typing_extensions(self: Self, y: int) -> typing_extensions.Self:
579 raise NotImplementedError
580
581 b = Builder()
582 #? Builder()
583 b.add_x(2)
584 #? Builder()
585 b.add_x(2).add_y(5)
586 # python >= 3.11
587 #? Builder()
588 b.add_x(2).add_not_implemented(5)
589 #? Builder()
590 b.add_x(2).add_not_implemented_typing_extensions(5)
591
592 # -----------------
593 # TypeAlias (see also #1969)
594 # -----------------
595
596 from typing import TypeAlias
597
598 IntX: typing.TypeAlias = int
599 IntY: TypeAlias = int
600
601 #? int
602 IntX
603 def f(x: IntX, y: IntY):
604 #? int()
605 x
606 #? int()
607 y