Debian Codegraph @

test-data/unit/stubgen.test

raw
1 -- Test cases for stubgen that generate stubs from Python code
2
3 [case testEmptyFile]
4 [out]
5
6 [case testSingleFunction]
7 def f():
8 x = 1
9 [out]
10 def f() -> None: ...
11
12 [case testTwoFunctions]
13 def f(a, b):
14 """
15 this is a docstring
16
17 more.
18 """
19 x = 1
20 def g(arg):
21 pass
22 [out]
23 def f(a, b) -> None: ...
24 def g(arg) -> None: ...
25
26 [case testDefaultArgInt]
27 def f(a, b=2): ...
28 def g(b=-1, c=0): ...
29 [out]
30 def f(a, b: int = 2) -> None: ...
31 def g(b: int = -1, c: int = 0) -> None: ...
32
33 [case testFuncDefaultArgNone]
34 def f(x=None): ...
35 [out]
36 def f(x=None) -> None: ...
37
38 [case testDefaultArgBool]
39 def f(x=True, y=False): ...
40 [out]
41 def f(x: bool = True, y: bool = False) -> None: ...
42
43 [case testDefaultArgBool_inspect]
44 def f(x=True, y=False): ...
45 [out]
46 def f(x: bool = ..., y: bool = ...): ...
47
48 [case testDefaultArgStr]
49 def f(x='foo',y="how's quotes"): ...
50 [out]
51 def f(x: str = 'foo', y: str = "how's quotes") -> None: ...
52
53 [case testDefaultArgStr_inspect]
54 def f(x='foo'): ...
55 [out]
56 def f(x: str = ...): ...
57
58 [case testDefaultArgBytes]
59 def f(x=b'foo',y=b"what's up",z=b'\xc3\xa0 la une'): ...
60 [out]
61 def f(x: bytes = b'foo', y: bytes = b"what's up", z: bytes = b'\xc3\xa0 la une') -> None: ...
62
63 [case testDefaultArgFloat]
64 def f(x=1.2,y=1e-6,z=0.0,w=-0.0,v=+1.0): ...
65 def g(x=float("nan"), y=float("inf"), z=float("-inf")): ...
66 [out]
67 def f(x: float = 1.2, y: float = 1e-06, z: float = 0.0, w: float = -0.0, v: float = +1.0) -> None: ...
68 def g(x=..., y=..., z=...) -> None: ...
69
70 [case testDefaultArgOther]
71 def f(x=ord): ...
72 [out]
73 def f(x=...) -> None: ...
74
75 [case testPreserveFunctionAnnotation]
76 def f(x: Foo) -> Bar: ...
77 def g(x: Foo = Foo()) -> Bar: ...
78 [out]
79 def f(x: Foo) -> Bar: ...
80 def g(x: Foo = ...) -> Bar: ...
81
82 [case testPreserveFunctionAnnotationWithArgs]
83 def f(x: foo['x']) -> bar: ...
84 def g(x: foo[x]) -> bar: ...
85 def h(x: foo['x', 'y']) -> bar: ...
86 def i(x: foo[x, y]) -> bar: ...
87 def j(x: foo['x', y]) -> bar: ...
88 def k(x: foo[x, 'y']) -> bar: ...
89 def lit_str(x: Literal['str']) -> Literal['str']: ...
90 def lit_int(x: Literal[1]) -> Literal[1]: ...
91 [out]
92 def f(x: foo['x']) -> bar: ...
93 def g(x: foo[x]) -> bar: ...
94 def h(x: foo['x', 'y']) -> bar: ...
95 def i(x: foo[x, y]) -> bar: ...
96 def j(x: foo['x', y]) -> bar: ...
97 def k(x: foo[x, 'y']) -> bar: ...
98 def lit_str(x: Literal['str']) -> Literal['str']: ...
99 def lit_int(x: Literal[1]) -> Literal[1]: ...
100
101 [case testPreserveVarAnnotation]
102 x: Foo
103 [out]
104 x: Foo
105
106 [case testPreserveVarAnnotationWithoutQuotes]
107 x: 'Foo'
108 [out]
109 x: Foo
110
111 [case testVarArgs]
112 def f(x, *y): ...
113 [out]
114 def f(x, *y) -> None: ...
115
116 [case testKwVarArgs]
117 def f(x, **y): ...
118 [out]
119 def f(x, **y) -> None: ...
120
121 [case testVarArgsWithKwVarArgs]
122 def f(a, *b, **c): ...
123 def g(a, *b, c=1): ...
124 def h(a, *b, c=1, **d): ...
125 def i(a, *, b=1): ...
126 def j(a, *, b=1, **c): ...
127 [out]
128 def f(a, *b, **c) -> None: ...
129 def g(a, *b, c: int = 1) -> None: ...
130 def h(a, *b, c: int = 1, **d) -> None: ...
131 def i(a, *, b: int = 1) -> None: ...
132 def j(a, *, b: int = 1, **c) -> None: ...
133
134 [case testClass]
135 class A:
136 def f(self, x):
137 x = 1
138 def g(): ...
139 [out]
140 class A:
141 def f(self, x) -> None: ...
142
143 def g() -> None: ...
144
145 [case testVariables]
146 i = 1
147 s = 'a'
148 f = 1.5
149 c1 = 1j
150 c2 = 0j + 1
151 bl1 = True
152 bl2 = False
153 bts = b''
154 [out]
155 i: int
156 s: str
157 f: float
158 c1: complex
159 c2: complex
160 bl1: bool
161 bl2: bool
162 bts: bytes
163
164 [case testVariablesWithUnary]
165 i = +-1
166 f = -1.5
167 c1 = -1j
168 c2 = -1j + 1
169 bl1 = not True
170 bl2 = not not False
171 [out]
172 i: int
173 f: float
174 c1: complex
175 c2: complex
176 bl1: bool
177 bl2: bool
178
179 [case testVariablesWithUnaryWrong]
180 i = not +1
181 bl1 = -True
182 bl2 = not -False
183 bl3 = -(not False)
184 [out]
185 from _typeshed import Incomplete
186
187 i: Incomplete
188 bl1: Incomplete
189 bl2: Incomplete
190 bl3: Incomplete
191
192 [case testAnnotatedVariable]
193 x: int = 1
194 [out]
195 x: int
196
197 [case testAnnotatedVariableGeneric]
198 x: Foo[int, str] = ...
199 [out]
200 x: Foo[int, str]
201
202 [case testAnnotatedVariableOldSyntax]
203 x = 1 # type: int
204 [out]
205 x: int
206
207 [case testAnnotatedVariableNone]
208 x: None
209 [out]
210 x: None
211
212 [case testAnnotatedVariableNoneOldSyntax]
213 x = None # type: None
214 [out]
215 x: None
216
217 [case testMultipleVariable]
218 x = y = 1
219 [out]
220 x: int
221 y: int
222
223 [case testClassVariable]
224 class C:
225 x = 1
226 [out]
227 class C:
228 x: int
229
230 [case testInitTypeAnnotationPreserved]
231 class C:
232 def __init__(self, x: str):
233 pass
234 [out]
235 class C:
236 def __init__(self, x: str) -> None: ...
237
238 [case testSelfAssignment]
239 from mod import A
240 from typing import Any, Dict, Union
241 class C:
242 def __init__(self):
243 self.a: A = A()
244 self.x = 1
245 x.y = 2
246 self.y: Dict[str, Any] = {}
247 self.z: Union[int, str, bool, None] = None
248 [out]
249 from mod import A
250 from typing import Any
251
252 class C:
253 a: A
254 x: int
255 y: dict[str, Any]
256 z: int | str | bool | None
257 def __init__(self) -> None: ...
258
259 [case testSelfAndClassBodyAssignment]
260 x = 1
261 class C:
262 x = 1
263 def __init__(self):
264 self.x = 1
265 self.x = 1
266 [out]
267 x: int
268
269 class C:
270 x: int
271 def __init__(self) -> None: ...
272
273 [case testEmptyClass]
274 class A: ...
275 [out]
276 class A: ...
277
278 [case testSkipPrivateFunction]
279 def _f(): ...
280 def g(): ...
281 [out]
282 def g() -> None: ...
283
284 [case testIncludePrivateFunction]
285 # flags: --include-private
286 def _f(): ...
287 def g(): ...
288 [out]
289 def _f() -> None: ...
290 def g() -> None: ...
291
292 [case testSkipPrivateMethod]
293 class A:
294 def _f(self): ...
295 [out]
296 class A: ...
297
298 [case testIncludePrivateMethod]
299 # flags: --include-private
300 class A:
301 def _f(self): ...
302 [out]
303 class A:
304 def _f(self) -> None: ...
305
306 [case testSkipPrivateVar]
307 _x = 1
308 class A:
309 _y = 1
310 [out]
311 class A: ...
312
313 [case testIncludePrivateVar]
314 # flags: --include-private
315 _x = 1
316 class A:
317 _y = 1
318 [out]
319 _x: int
320
321 class A:
322 _y: int
323
324 [case testSpecialInternalVar]
325 __all__ = []
326 __author__ = ''
327 __version__ = ''
328 [out]
329 __version__: str
330
331 [case testBaseClass]
332 class A: ...
333 class B(A): ...
334 [out]
335 class A: ...
336 class B(A): ...
337
338 [case testDecoratedFunction]
339 import x
340
341 @decorator
342 def foo(x): ...
343
344 @x.decorator
345 def bar(x): ...
346
347 @decorator(x=1, y={"a": 1})
348 def foo_bar(x): ...
349 [out]
350 import x
351
352 @decorator
353 def foo(x) -> None: ...
354 @x.decorator
355 def bar(x) -> None: ...
356 def foo_bar(x) -> None: ...
357
358 [case testMultipleAssignment]
359 x, y = 1, 2
360 [out]
361 from _typeshed import Incomplete
362
363 x: Incomplete
364 y: Incomplete
365
366 [case testMultipleAssignmentAnnotated]
367 x, y = 1, "2" # type: int, str
368 [out]
369 x: int
370 y: str
371
372 [case testMultipleAssignment2]
373 [x, y] = 1, 2
374 [out]
375 from _typeshed import Incomplete
376
377 x: Incomplete
378 y: Incomplete
379
380 [case testKeywordOnlyArg]
381 def f(x, *, y=1): ...
382 def g(x, *, y=1, z=2): ...
383 [out]
384 def f(x, *, y: int = 1) -> None: ...
385 def g(x, *, y: int = 1, z: int = 2) -> None: ...
386
387 [case testKeywordOnlyArg_inspect]
388 def f(x, *, y=1): ...
389 def g(x, *, y=1, z=2): ...
390 def h(x, *, y, z=2): ...
391 [out]
392 def f(x, *, y: int = ...): ...
393 def g(x, *, y: int = ..., z: int = ...): ...
394 def h(x, *, y, z: int = ...): ...
395
396 [case testProperty]
397 class A:
398 @property
399 def f(self):
400 return 1
401 @f.setter
402 def f(self, x): ...
403 @f.deleter
404 def f(self): ...
405
406 def h(self):
407 self.f = 1
408 [out]
409 class A:
410 @property
411 def f(self): ...
412 @f.setter
413 def f(self, x) -> None: ...
414 @f.deleter
415 def f(self) -> None: ...
416 def h(self) -> None: ...
417
418 [case testProperty_semanal]
419 class A:
420 @property
421 def f(self):
422 return 1
423 @f.setter
424 def f(self, x): ...
425 @f.deleter
426 def f(self): ...
427
428 def h(self):
429 self.f = 1
430 [out]
431 class A:
432 @property
433 def f(self): ...
434 @f.setter
435 def f(self, x) -> None: ...
436 @f.deleter
437 def f(self) -> None: ...
438 def h(self) -> None: ...
439
440 -- a read/write property is treated the same as an attribute
441 [case testProperty_inspect]
442 class A:
443 @property
444 def f(self):
445 return 1
446 @f.setter
447 def f(self, x): ...
448
449 def h(self):
450 self.f = 1
451 [out]
452 from _typeshed import Incomplete
453
454 class A:
455 f: Incomplete
456 def h(self): ...
457
458 [case testFunctoolsCachedProperty]
459 import functools
460
461 class A:
462 @functools.cached_property
463 def x(self):
464 return 'x'
465 [out]
466 import functools
467
468 class A:
469 @functools.cached_property
470 def x(self): ...
471
472 [case testFunctoolsCachedPropertyAlias]
473 import functools as ft
474
475 class A:
476 @ft.cached_property
477 def x(self):
478 return 'x'
479 [out]
480 import functools as ft
481
482 class A:
483 @ft.cached_property
484 def x(self): ...
485
486 [case testCachedProperty]
487 from functools import cached_property
488
489 class A:
490 @cached_property
491 def x(self):
492 return 'x'
493 [out]
494 from functools import cached_property
495
496 class A:
497 @cached_property
498 def x(self): ...
499
500 [case testCachedPropertyAlias]
501 from functools import cached_property as cp
502
503 class A:
504 @cp
505 def x(self):
506 return 'x'
507 [out]
508 from functools import cached_property as cp
509
510 class A:
511 @cp
512 def x(self): ...
513
514 [case testStaticMethod]
515 class A:
516 @staticmethod
517 def f(x): ...
518 [out]
519 class A:
520 @staticmethod
521 def f(x) -> None: ...
522
523 [case testClassMethod]
524 class A:
525 @classmethod
526 def f(cls): ...
527 [out]
528 class A:
529 @classmethod
530 def f(cls) -> None: ...
531
532 [case testClassMethod_inspect]
533 class A:
534 @classmethod
535 def f(cls): ...
536 [out]
537 class A:
538 @classmethod
539 def f(cls): ...
540
541 [case testIfMainCheck]
542 def a(): ...
543 if __name__ == '__main__':
544 x = 1
545 def f(): ...
546 def b(): ...
547 [out]
548 def a() -> None: ...
549 def b() -> None: ...
550
551 [case testImportStar]
552 from x import *
553 from a.b import *
554 def f(): ...
555 [out]
556 from x import *
557 from a.b import *
558
559 def f() -> None: ...
560
561 [case testNoSpacesBetweenEmptyClasses]
562 class X:
563 def g(self): ...
564 class A: ...
565 class B: ...
566 class C:
567 def f(self): ...
568 [out]
569 class X:
570 def g(self) -> None: ...
571
572 class A: ...
573 class B: ...
574
575 class C:
576 def f(self) -> None: ...
577
578 [case testNoSpacesBetweenEmptyClasses_inspect]
579 class X:
580 def g(self): ...
581 class A: ...
582 class B: ...
583 class C:
584 def f(self): ...
585 [out]
586 class X:
587 def g(self): ...
588
589 class A: ...
590 class B: ...
591
592 class C:
593 def f(self): ...
594
595 [case testExceptionBaseClasses]
596 class A(Exception): ...
597 class B(ValueError): ...
598 [out]
599 class A(Exception): ...
600 class B(ValueError): ...
601
602 [case testOmitSomeSpecialMethods]
603 class A:
604 def __str__(self): ...
605 def __repr__(self): ...
606 def __eq__(self): ...
607 def __getstate__(self): ...
608 def __setstate__(self, state): ...
609 [out]
610 class A:
611 def __eq__(self): ...
612
613 [case testOmitSomeSpecialMethods_inspect]
614 class A:
615 def __str__(self): ...
616 def __repr__(self): ...
617 def __eq__(self): ...
618 def __getstate__(self): ...
619 def __setstate__(self, state): ...
620 [out]
621 class A:
622 def __eq__(self) -> bool: ...
623
624 -- Tests that will perform runtime imports of modules.
625 -- Don't use `_import` suffix if there are unquoted forward references.
626
627 [case testOmitDefsNotInAll_import]
628 __all__ = [] + ['f']
629 def f(): ...
630 def g(): ...
631 [out]
632 __all__ = ['f']
633
634 def f() -> None: ...
635
636 [case testOmitDefsNotInAll_semanal]
637 __all__ = ['f']
638 def f(): ...
639 def g(): ...
640 [out]
641 __all__ = ['f']
642
643 def f() -> None: ...
644
645 [case testOmitDefsNotInAll_inspect]
646 __all__ = [] + ['f']
647 def f(): ...
648 def g(): ...
649 [out]
650 __all__ = ['f']
651
652 def f(): ...
653
654 [case testVarDefsNotInAll_import]
655 __all__ = [] + ['f', 'g']
656 def f(): ...
657 x = 1
658 y = 1
659 def g(): ...
660 [out]
661 __all__ = ['f', 'g']
662
663 def f() -> None: ...
664 def g() -> None: ...
665
666 [case testVarDefsNotInAll_inspect]
667 __all__ = [] + ['f', 'g']
668 def f(): ...
669 x = 1
670 y = 1
671 def g(): ...
672 [out]
673 __all__ = ['f', 'g']
674
675 def f(): ...
676 def g(): ...
677
678 [case testIncludeClassNotInAll_import]
679 __all__ = [] + ['f']
680 def f(): ...
681 class A: ...
682 [out]
683 __all__ = ['f']
684
685 def f() -> None: ...
686
687 class A: ...
688
689 [case testIncludeClassNotInAll_inspect]
690 __all__ = [] + ['f']
691 def f(): ...
692 class A: ...
693 [out]
694 __all__ = ['f']
695
696 def f(): ...
697
698 class A: ...
699
700 [case testAllAndClass_import]
701 __all__ = ['A']
702 class A:
703 x = 1
704 def f(self): ...
705 [out]
706 __all__ = ['A']
707
708 class A:
709 x: int
710 def f(self) -> None: ...
711
712 [case testSkipMultiplePrivateDefs]
713 class A: ...
714 _x = 1
715 _y = 1
716 _z = 1
717 class C: ...
718 [out]
719 class A: ...
720 class C: ...
721
722 [case testIncludeMultiplePrivateDefs]
723 # flags: --include-private
724 class A: ...
725 _x = 1
726 _y = 1
727 _z = 1
728 class C: ...
729 [out]
730 class A: ...
731
732 _x: int
733 _y: int
734 _z: int
735
736 class C: ...
737
738 [case testIncludeFromImportIfInAll_import]
739 from re import match, search, sub
740 __all__ = ['match', 'sub', 'x']
741 x = 1
742 [out]
743 from re import match as match, sub as sub
744
745 __all__ = ['match', 'sub', 'x']
746
747 x: int
748
749 [case testExportModule_import]
750 import re
751 __all__ = ['re', 'x']
752 x = 1
753 y = 2
754 [out]
755 import re as re
756
757 __all__ = ['re', 'x']
758
759 x: int
760
761 [case testExportModule2_import]
762 import re
763 __all__ = ['re', 'x']
764 x = 1
765 y = 2
766 [out]
767 import re as re
768
769 __all__ = ['re', 'x']
770
771 x: int
772
773 [case testExportModuleAs_import]
774 import re as rex
775 __all__ = ['rex', 'x']
776 x = 1
777 y = 2
778 [out]
779 import re as rex
780
781 __all__ = ['rex', 'x']
782
783 x: int
784
785 [case testExportModuleInPackage_import]
786 import urllib.parse as p
787 __all__ = ['p']
788 [out]
789 import urllib.parse as p
790
791 __all__ = ['p']
792
793 [case testExportPackageOfAModule_import]
794 import urllib.parse
795 __all__ = ['urllib']
796
797 [out]
798 import urllib as urllib
799
800 __all__ = ['urllib']
801
802 [case testRelativeImportAll]
803 from .x import *
804 [out]
805 from .x import *
806
807 [case testCommentForUndefinedName_import]
808 __all__ = ['f', 'x', 'C', 'g']
809 def f(): ...
810 x = 1
811 class C:
812 def g(self): ...
813 [out]
814 __all__ = ['f', 'x', 'C', 'g']
815
816 def f() -> None: ...
817
818 x: int
819
820 class C:
821 def g(self) -> None: ...
822
823 # Names in __all__ with no definition:
824 # g
825
826 [case testCommentForUndefinedName_inspect]
827 __all__ = ['f', 'x', 'C', 'g']
828 def f(): ...
829 x = 1
830 class C:
831 def g(self): ...
832 [out]
833 __all__ = ['f', 'x', 'C', 'g']
834
835 def f(): ...
836
837 x: int
838
839 class C:
840 def g(self): ...
841
842 # Names in __all__ with no definition:
843 # g
844
845 [case testIgnoreSlots]
846 class A:
847 __slots__ = ()
848 [out]
849 class A: ...
850
851 [case testSkipPrivateProperty]
852 class A:
853 @property
854 def _foo(self): ...
855 [out]
856 class A: ...
857
858 [case testSkipPrivateProperty_inspect]
859 class A:
860 @property
861 def _foo(self): ...
862 [out]
863 class A: ...
864
865 [case testIncludePrivateProperty]
866 # flags: --include-private
867 class A:
868 @property
869 def _foo(self): ...
870 [out]
871 class A:
872 @property
873 def _foo(self) -> None: ...
874
875 [case testIncludePrivateProperty_inspect]
876 # flags: --include-private
877 class A:
878 @property
879 def _foo(self): ...
880 [out]
881 class A:
882 @property
883 def _foo(self): ...
884
885 [case testSkipPrivateStaticAndClassMethod]
886 class A:
887 @staticmethod
888 def _foo(): ...
889 @classmethod
890 def _bar(cls): ...
891 [out]
892 class A: ...
893
894 [case testSkipPrivateStaticAndClassMethod_inspect]
895 class A:
896 @staticmethod
897 def _foo(): ...
898 @classmethod
899 def _bar(cls): ...
900 [out]
901 class A: ...
902
903 [case testIncludePrivateStaticAndClassMethod]
904 # flags: --include-private
905 class A:
906 @staticmethod
907 def _foo(): ...
908 @classmethod
909 def _bar(cls): ...
910 [out]
911 class A:
912 @staticmethod
913 def _foo() -> None: ...
914 @classmethod
915 def _bar(cls) -> None: ...
916
917 [case testIncludePrivateStaticAndClassMethod_inspect]
918 # flags: --include-private
919 class A:
920 @staticmethod
921 def _foo(): ...
922 @classmethod
923 def _bar(cls): ...
924 [out]
925 class A:
926 @staticmethod
927 def _foo(): ...
928 @classmethod
929 def _bar(cls): ...
930
931 [case testNamedtuple]
932 import collections, typing, x
933 X = collections.namedtuple('X', ['a', 'b'])
934 Y = typing.NamedTuple('Y', [('a', int), ('b', str)])
935 [out]
936 from _typeshed import Incomplete
937 from typing import NamedTuple
938
939 class X(NamedTuple):
940 a: Incomplete
941 b: Incomplete
942
943 class Y(NamedTuple):
944 a: int
945 b: str
946
947 [case testNamedTupleClassSyntax_semanal]
948 from typing import NamedTuple
949
950 class A(NamedTuple):
951 x: int
952 y: str = 'a'
953
954 class B(A):
955 z1: str
956 z2 = 1
957 z3: str = 'b'
958
959 class RegularClass:
960 x: int
961 y: str = 'a'
962 class NestedNamedTuple(NamedTuple):
963 x: int
964 y: str = 'a'
965 z: str = 'b'
966 [out]
967 from typing import NamedTuple
968
969 class A(NamedTuple):
970 x: int
971 y: str = ...
972
973 class B(A):
974 z1: str
975 z2: int
976 z3: str
977
978 class RegularClass:
979 x: int
980 y: str
981 class NestedNamedTuple(NamedTuple):
982 x: int
983 y: str = ...
984 z: str
985
986
987 [case testNestedClassInNamedTuple_semanal-xfail]
988 from typing import NamedTuple
989
990 # TODO: make sure that nested classes in `NamedTuple` are supported:
991 class NamedTupleWithNestedClass(NamedTuple):
992 class Nested:
993 x: int
994 y: str = 'a'
995 [out]
996 from typing import NamedTuple
997
998 class NamedTupleWithNestedClass(NamedTuple):
999 class Nested:
1000 x: int
1001 y: str
1002
1003 [case testEmptyNamedtuple]
1004 import collections, typing
1005 X = collections.namedtuple('X', [])
1006 Y = typing.NamedTuple('Y', [])
1007 [out]
1008 from typing import NamedTuple
1009
1010 class X(NamedTuple): ...
1011 class Y(NamedTuple): ...
1012
1013 [case testNamedtupleAltSyntax]
1014 from collections import namedtuple, xx
1015 X = namedtuple('X', 'a b')
1016 xx
1017 [out]
1018 from _typeshed import Incomplete
1019 from typing import NamedTuple
1020
1021 class X(NamedTuple):
1022 a: Incomplete
1023 b: Incomplete
1024
1025 [case testNamedtupleAltSyntaxUsingComma]
1026 from collections import namedtuple, xx
1027 X = namedtuple('X', 'a, b')
1028 xx
1029 [out]
1030 from _typeshed import Incomplete
1031 from typing import NamedTuple
1032
1033 class X(NamedTuple):
1034 a: Incomplete
1035 b: Incomplete
1036
1037 [case testNamedtupleAltSyntaxUsingMultipleCommas]
1038 from collections import namedtuple, xx
1039 X = namedtuple('X', 'a,, b')
1040 xx
1041 [out]
1042 from _typeshed import Incomplete
1043 from typing import NamedTuple
1044
1045 class X(NamedTuple):
1046 a: Incomplete
1047 b: Incomplete
1048
1049 [case testNamedtupleWithUnderscore]
1050 from collections import namedtuple as _namedtuple
1051 from typing import NamedTuple as _NamedTuple
1052 def f(): ...
1053 X = _namedtuple('X', 'a b')
1054 Y = _NamedTuple('Y', [('a', int), ('b', str)])
1055 def g(): ...
1056 [out]
1057 from _typeshed import Incomplete
1058 from typing import NamedTuple
1059
1060 def f() -> None: ...
1061
1062 class X(NamedTuple):
1063 a: Incomplete
1064 b: Incomplete
1065
1066 class Y(NamedTuple):
1067 a: int
1068 b: str
1069
1070 def g() -> None: ...
1071
1072 [case testNamedtupleBaseClass]
1073 import collections, x
1074 _X = collections.namedtuple('_X', ['a', 'b'])
1075 class Y(_X): ...
1076 [out]
1077 from _typeshed import Incomplete
1078 from typing import NamedTuple
1079
1080 class _X(NamedTuple):
1081 a: Incomplete
1082 b: Incomplete
1083
1084 class Y(_X): ...
1085
1086 [case testNamedtupleAltSyntaxFieldsTuples]
1087 from collections import namedtuple, xx
1088 from typing import NamedTuple
1089 X = namedtuple('X', ())
1090 Y = namedtuple('Y', ('a',))
1091 Z = namedtuple('Z', ('a', 'b', 'c', 'd', 'e'))
1092 xx
1093 R = NamedTuple('R', ())
1094 S = NamedTuple('S', (('a', int),))
1095 T = NamedTuple('T', (('a', int), ('b', str)))
1096 [out]
1097 from _typeshed import Incomplete
1098 from typing import NamedTuple
1099
1100 class X(NamedTuple): ...
1101
1102 class Y(NamedTuple):
1103 a: Incomplete
1104
1105 class Z(NamedTuple):
1106 a: Incomplete
1107 b: Incomplete
1108 c: Incomplete
1109 d: Incomplete
1110 e: Incomplete
1111
1112 class R(NamedTuple): ...
1113
1114 class S(NamedTuple):
1115 a: int
1116
1117 class T(NamedTuple):
1118 a: int
1119 b: str
1120
1121 [case testDynamicNamedTuple]
1122 from collections import namedtuple
1123 from typing import NamedTuple
1124 N = namedtuple('N', ['x', 'y'] + ['z'])
1125 M = NamedTuple('M', [('x', int), ('y', str)] + [('z', float)])
1126 class X(namedtuple('X', ['a', 'b'] + ['c'])): ...
1127 [out]
1128 from _typeshed import Incomplete
1129
1130 N: Incomplete
1131 M: Incomplete
1132
1133 class X(Incomplete): ...
1134
1135 [case testNamedTupleInClassBases]
1136 import collections, typing
1137 from collections import namedtuple
1138 from typing import NamedTuple
1139 class X(namedtuple('X', ['a', 'b'])): ...
1140 class Y(NamedTuple('Y', [('a', int), ('b', str)])): ...
1141 class R(collections.namedtuple('R', ['a', 'b'])): ...
1142 class S(typing.NamedTuple('S', [('a', int), ('b', str)])): ...
1143 [out]
1144 import typing
1145 from _typeshed import Incomplete
1146 from typing import NamedTuple
1147
1148 class X(NamedTuple('X', [('a', Incomplete), ('b', Incomplete)])): ...
1149 class Y(NamedTuple('Y', [('a', int), ('b', str)])): ...
1150 class R(NamedTuple('R', [('a', Incomplete), ('b', Incomplete)])): ...
1151 class S(typing.NamedTuple('S', [('a', int), ('b', str)])): ...
1152
1153 [case testNotNamedTuple]
1154 from not_collections import namedtuple
1155 from not_typing import NamedTuple
1156 from collections import notnamedtuple
1157 from typing import NotNamedTuple
1158 X = namedtuple('X', ['a', 'b'])
1159 Y = notnamedtuple('Y', ['a', 'b'])
1160 Z = NamedTuple('Z', [('a', int), ('b', str)])
1161 W = NotNamedTuple('W', [('a', int), ('b', str)])
1162 [out]
1163 from _typeshed import Incomplete
1164
1165 X: Incomplete
1166 Y: Incomplete
1167 Z: Incomplete
1168 W: Incomplete
1169
1170 [case testNamedTupleFromImportAlias]
1171 import collections as c
1172 import typing as t
1173 import typing_extensions as te
1174 X = c.namedtuple('X', ['a', 'b'])
1175 Y = t.NamedTuple('Y', [('a', int), ('b', str)])
1176 Z = te.NamedTuple('Z', [('a', int), ('b', str)])
1177 [out]
1178 from _typeshed import Incomplete
1179 from typing import NamedTuple
1180
1181 class X(NamedTuple):
1182 a: Incomplete
1183 b: Incomplete
1184
1185 class Y(NamedTuple):
1186 a: int
1187 b: str
1188
1189 class Z(NamedTuple):
1190 a: int
1191 b: str
1192
1193 [case testArbitraryBaseClass]
1194 import x
1195 class D(x.C): ...
1196 [out]
1197 import x
1198
1199 class D(x.C): ...
1200
1201 [case testArbitraryBaseClass2]
1202 import x.y
1203 class D(x.y.C): ...
1204 [out]
1205 import x.y
1206
1207 class D(x.y.C): ...
1208
1209 [case testUnqualifiedArbitraryBaseClassWithNoDef]
1210 class A(int): ...
1211 [out]
1212 class A(int): ...
1213
1214 [case testUnqualifiedArbitraryBaseClass]
1215 from x import X
1216 class A(X): ...
1217 [out]
1218 from x import X
1219
1220 class A(X): ...
1221
1222 [case testUnqualifiedArbitraryBaseClassWithImportAs]
1223 from x import X as _X
1224 class A(_X): ...
1225 [out]
1226 from x import X as _X
1227
1228 class A(_X): ...
1229
1230 [case testGenericClass]
1231 class D(Generic[T]): ...
1232 [out]
1233 class D(Generic[T]): ...
1234
1235 [case testGenericClass_semanal]
1236 from typing import Generic, TypeVar
1237 T = TypeVar('T')
1238 class D(Generic[T]): ...
1239 [out]
1240 from typing import Generic, TypeVar
1241
1242 T = TypeVar('T')
1243
1244 class D(Generic[T]): ...
1245
1246 [case testGenericClassTypeVarTuple]
1247 from typing import Generic
1248 from typing_extensions import TypeVarTuple, Unpack
1249 Ts = TypeVarTuple('Ts')
1250 class D(Generic[Unpack[Ts]]): ...
1251 def callback(func: Callable[[Unpack[Ts]], None], *args: Unpack[Ts]) -> None: ...
1252 [out]
1253 from typing import Generic
1254 from typing_extensions import TypeVarTuple, Unpack
1255
1256 Ts = TypeVarTuple('Ts')
1257
1258 class D(Generic[Unpack[Ts]]): ...
1259
1260 def callback(func: Callable[[Unpack[Ts]], None], *args: Unpack[Ts]) -> None: ...
1261
1262 [case testGenericClassTypeVarTuple_semanal]
1263 from typing import Generic
1264 from typing_extensions import TypeVarTuple, Unpack
1265 Ts = TypeVarTuple('Ts')
1266 class D(Generic[Unpack[Ts]]): ...
1267 def callback(func: Callable[[Unpack[Ts]], None], *args: Unpack[Ts]) -> None: ...
1268 [out]
1269 from typing import Generic
1270 from typing_extensions import TypeVarTuple, Unpack
1271
1272 Ts = TypeVarTuple('Ts')
1273
1274 class D(Generic[Unpack[Ts]]): ...
1275
1276 def callback(func: Callable[[Unpack[Ts]], None], *args: Unpack[Ts]) -> None: ...
1277
1278 [case testGenericClassTypeVarTuplePy311]
1279 # flags: --python-version=3.11
1280 from typing import Generic, TypeVarTuple
1281 Ts = TypeVarTuple('Ts')
1282 class D(Generic[*Ts]): ...
1283 def callback(func: Callable[[*Ts], None], *args: *Ts) -> None: ...
1284 [out]
1285 from typing import Generic, TypeVarTuple
1286
1287 Ts = TypeVarTuple('Ts')
1288
1289 class D(Generic[*Ts]): ...
1290
1291 def callback(func: Callable[[*Ts], None], *args: *Ts) -> None: ...
1292
1293 [case testGenericClassTypeVarTuplePy311_semanal]
1294 # flags: --python-version=3.11
1295 from typing import Generic, TypeVarTuple
1296 Ts = TypeVarTuple('Ts')
1297 class D(Generic[*Ts]): ...
1298 def callback(func: Callable[[*Ts], None], *args: *Ts) -> None: ...
1299 [out]
1300 from typing import Generic, TypeVarTuple
1301
1302 Ts = TypeVarTuple('Ts')
1303
1304 class D(Generic[*Ts]): ...
1305
1306 def callback(func: Callable[[*Ts], None], *args: *Ts) -> None: ...
1307
1308 [case testObjectBaseClass]
1309 class A(object): ...
1310 [out]
1311 class A: ...
1312
1313 [case testObjectBaseClassWithImport]
1314 import builtins as b
1315 class A(b.object): ...
1316 [out]
1317 class A: ...
1318
1319 [case testEmptyLines]
1320 def x(): ...
1321 def f():
1322 class A:
1323 def f(self):
1324 self.x = 1
1325 def g(): ...
1326 [out]
1327 def x() -> None: ...
1328 def f() -> None: ...
1329 def g() -> None: ...
1330
1331 [case testNestedClass]
1332 class A:
1333 class B:
1334 x = 1
1335 def f(self): ...
1336 def g(self): ...
1337 [out]
1338 class A:
1339 class B:
1340 x: int
1341 def f(self) -> None: ...
1342 def g(self) -> None: ...
1343
1344 [case testExportViaRelativeImport]
1345 from .api import get
1346 [out]
1347 from .api import get as get
1348
1349 [case testExportViaRelativePackageImport]
1350 from .packages.urllib3.contrib import parse
1351 [out]
1352 from .packages.urllib3.contrib import parse as parse
1353
1354 [case testNoExportViaRelativeImport]
1355 from . import get
1356 get()
1357 [out]
1358
1359 [case testRelativeImportAndBase]
1360 from .x import X
1361 class A(X):
1362 pass
1363 [out]
1364 from .x import X
1365
1366 class A(X): ...
1367
1368 [case testDuplicateDef]
1369 def syslog(a): pass
1370 def syslog(a): pass
1371 [out]
1372 def syslog(a) -> None: ...
1373
1374 [case testAsyncAwait_fast_parser]
1375 async def f(a):
1376 x = await y
1377 [out]
1378 async def f(a) -> None: ...
1379
1380 [case testMethodDefaultArgNone]
1381 class A:
1382 x = None
1383 def __init__(self, a=None):
1384 self.x = []
1385 def method(self, a=None):
1386 self.x = []
1387 [out]
1388 from _typeshed import Incomplete
1389
1390 class A:
1391 x: Incomplete
1392 def __init__(self, a=None) -> None: ...
1393 def method(self, a=None) -> None: ...
1394
1395 [case testAnnotationImportsFrom]
1396 import foo
1397 from collections import defaultdict
1398 x: defaultdict
1399
1400 [out]
1401 from collections import defaultdict
1402
1403 x: defaultdict
1404
1405 [case testAnnotationImports]
1406 import foo
1407 import collections
1408 x: collections.defaultdict
1409
1410 [out]
1411 import collections
1412
1413 x: collections.defaultdict
1414
1415
1416 [case testAnnotationImports2]
1417 from typing import List
1418 import collections
1419 x: List[collections.defaultdict]
1420
1421 [out]
1422 import collections
1423
1424 x: list[collections.defaultdict]
1425
1426
1427 [case testAnnotationFwRefs]
1428 x: C
1429
1430 class C:
1431 attr: C
1432
1433 y: C
1434 [out]
1435 x: C
1436
1437 class C:
1438 attr: C
1439
1440 y: C
1441
1442 [case testTypeVarPreserved]
1443 tv = TypeVar('tv')
1444 ps = ParamSpec('ps')
1445 tvt = TypeVarTuple('tvt')
1446
1447 [out]
1448 from typing import TypeVar
1449 from typing_extensions import ParamSpec, TypeVarTuple
1450
1451 tv = TypeVar('tv')
1452 ps = ParamSpec('ps')
1453 tvt = TypeVarTuple('tvt')
1454
1455 [case testTypeVarArgsPreserved]
1456 tv = TypeVar('tv', int, str)
1457
1458 [out]
1459 from typing import TypeVar
1460
1461 tv = TypeVar('tv', int, str)
1462
1463 [case testTypeVarNamedArgsPreserved]
1464 tv = TypeVar('tv', bound=bool, covariant=True)
1465 ps = ParamSpec('ps', bound=bool, covariant=True)
1466
1467 [out]
1468 from typing import TypeVar
1469 from typing_extensions import ParamSpec
1470
1471 tv = TypeVar('tv', bound=bool, covariant=True)
1472 ps = ParamSpec('ps', bound=bool, covariant=True)
1473
1474 [case TypeVarImportAlias]
1475 from typing import TypeVar as t_TV, ParamSpec as t_PS
1476 from typing_extensions import TypeVar as te_TV, TypeVarTuple as te_TVT
1477 from x import TypeVar as x_TV
1478
1479 T = t_TV('T')
1480 U = te_TV('U')
1481 V = x_TV('V')
1482
1483 PS = t_PS('PS')
1484 TVT = te_TVT('TVT')
1485
1486 [out]
1487 from _typeshed import Incomplete
1488 from typing import ParamSpec as t_PS, TypeVar as t_TV
1489 from typing_extensions import TypeVar as te_TV, TypeVarTuple as te_TVT
1490
1491 T = t_TV('T')
1492 U = te_TV('U')
1493 V: Incomplete
1494 PS = t_PS('PS')
1495 TVT = te_TVT('TVT')
1496
1497 [case testTypeVarFromImportAlias]
1498 import typing as t
1499 import typing_extensions as te
1500 import x
1501
1502 T = t.TypeVar('T')
1503 U = te.TypeVar('U')
1504 V = x.TypeVar('V')
1505
1506 PS = t.ParamSpec('PS')
1507 TVT = te.TypeVarTuple('TVT')
1508
1509 [out]
1510 import typing as t
1511 import typing_extensions as te
1512 from _typeshed import Incomplete
1513
1514 T = t.TypeVar('T')
1515 U = te.TypeVar('U')
1516 V: Incomplete
1517 PS = t.ParamSpec('PS')
1518 TVT = te.TypeVarTuple('TVT')
1519
1520 [case testTypeAliasPreserved]
1521 alias = str
1522
1523 [out]
1524 alias = str
1525
1526 [case testDeepTypeAliasPreserved]
1527
1528 alias = Dict[str, List[str]]
1529
1530 [out]
1531 alias = Dict[str, List[str]]
1532
1533 [case testDeepGenericTypeAliasPreserved]
1534 from typing import TypeVar
1535
1536 T = TypeVar('T')
1537 alias = Union[T, List[T]]
1538
1539 [out]
1540 from typing import TypeVar
1541
1542 T = TypeVar('T')
1543 alias = Union[T, List[T]]
1544
1545 [case testExplicitTypeAlias]
1546 from typing import TypeAlias
1547
1548 explicit_alias: TypeAlias = tuple[int, str]
1549 implicit_alias = list[int]
1550
1551 [out]
1552 from typing import TypeAlias
1553
1554 explicit_alias: TypeAlias = tuple[int, str]
1555 implicit_alias = list[int]
1556
1557
1558 [case testEllipsisAliasPreserved]
1559
1560 alias = Tuple[int, ...]
1561
1562 [out]
1563 alias = Tuple[int, ...]
1564
1565 [case testCallableAliasPreserved]
1566
1567 alias1 = Callable[..., int]
1568 alias2 = Callable[[str, bool], None]
1569
1570 [out]
1571 alias1 = Callable[..., int]
1572 alias2 = Callable[[str, bool], None]
1573
1574 [case testAliasPullsImport]
1575 from module import Container
1576
1577 alias = Container[Any]
1578
1579 [out]
1580 from module import Container
1581 from typing import Any
1582
1583 alias = Container[Any]
1584
1585 [case testAliasExceptions]
1586 noalias1 = None
1587 noalias2 = ...
1588 noalias3 = True
1589
1590 [out]
1591 from _typeshed import Incomplete
1592
1593 noalias1: Incomplete
1594 noalias2: Incomplete
1595 noalias3: bool
1596
1597 [case testComplexAlias]
1598 # modules: main a
1599
1600 from a import valid
1601
1602 def func() -> int:
1603 return 2
1604
1605 aliased_func = func
1606 int_value = 1
1607
1608 class A:
1609 cls_var = valid
1610
1611 def __init__(self, arg: str) -> None:
1612 self.self_var = arg
1613
1614 def meth(self) -> None:
1615 func_value = int_value
1616
1617 alias_meth = meth
1618 alias_func = func
1619 alias_alias_func = aliased_func
1620 int_value = int_value
1621
1622 [file a.py]
1623 valid : list[int] = [1, 2, 3]
1624
1625
1626 [out]
1627 # main.pyi
1628 from _typeshed import Incomplete
1629 from a import valid
1630
1631 def func() -> int: ...
1632 aliased_func = func
1633 int_value: int
1634
1635 class A:
1636 cls_var = valid
1637 self_var: Incomplete
1638 def __init__(self, arg: str) -> None: ...
1639 def meth(self) -> None: ...
1640 alias_meth = meth
1641 alias_func = func
1642 alias_alias_func = aliased_func
1643 int_value = int_value
1644 # a.pyi
1645 valid: list[int]
1646
1647 -- More features/fixes:
1648 -- do not export deleted names
1649
1650 [case testFunctionNoReturnInfersReturnNone]
1651 def f():
1652 x = 1
1653 [out]
1654 def f() -> None: ...
1655
1656 [case testFunctionReturnNoReturnType]
1657 def f():
1658 return 1
1659 def g():
1660 return
1661 [out]
1662 def f(): ...
1663 def g() -> None: ...
1664
1665 [case testFunctionEllipsisInfersReturnNone]
1666 def f(): ...
1667 [out]
1668 def f() -> None: ...
1669
1670 [case testFunctionYields]
1671 def f():
1672 yield 123
1673 def g():
1674 x = yield
1675 def h1():
1676 yield
1677 return
1678 def h2():
1679 yield
1680 return "abc"
1681 def h3():
1682 yield
1683 return None
1684 def all():
1685 x = yield 123
1686 return "abc"
1687 [out]
1688 from _typeshed import Incomplete
1689 from collections.abc import Generator
1690
1691 def f() -> Generator[Incomplete]: ...
1692 def g() -> Generator[None, Incomplete]: ...
1693 def h1() -> Generator[None]: ...
1694 def h2() -> Generator[None, None, Incomplete]: ...
1695 def h3() -> Generator[None]: ...
1696 def all() -> Generator[Incomplete, Incomplete, Incomplete]: ...
1697
1698 [case testFunctionYieldsNone]
1699 def f():
1700 yield
1701 def g():
1702 yield None
1703
1704 [out]
1705 from collections.abc import Generator
1706
1707 def f() -> Generator[None]: ...
1708 def g() -> Generator[None]: ...
1709
1710 [case testGeneratorAlreadyDefined]
1711 class Generator:
1712 pass
1713
1714 def f():
1715 yield 123
1716 [out]
1717 from _typeshed import Incomplete
1718 from collections.abc import Generator as _Generator
1719
1720 class Generator: ...
1721
1722 def f() -> _Generator[Incomplete]: ...
1723
1724 [case testGeneratorYieldFrom]
1725 def g1():
1726 yield from x
1727 def g2():
1728 y = yield from x
1729 def g3():
1730 yield from x
1731 return
1732 def g4():
1733 yield from x
1734 return None
1735 def g5():
1736 yield from x
1737 return z
1738
1739 [out]
1740 from _typeshed import Incomplete
1741 from collections.abc import Generator
1742
1743 def g1() -> Generator[Incomplete, Incomplete]: ...
1744 def g2() -> Generator[Incomplete, Incomplete]: ...
1745 def g3() -> Generator[Incomplete, Incomplete]: ...
1746 def g4() -> Generator[Incomplete, Incomplete]: ...
1747 def g5() -> Generator[Incomplete, Incomplete, Incomplete]: ...
1748
1749 [case testGeneratorYieldAndYieldFrom]
1750 def g1():
1751 yield x1
1752 yield from x2
1753 def g2():
1754 yield x1
1755 y = yield from x2
1756 def g3():
1757 y = yield x1
1758 yield from x2
1759 def g4():
1760 yield x1
1761 yield from x2
1762 return
1763 def g5():
1764 yield x1
1765 yield from x2
1766 return None
1767 def g6():
1768 yield x1
1769 yield from x2
1770 return z
1771 def g7():
1772 yield None
1773 yield from x2
1774
1775 [out]
1776 from _typeshed import Incomplete
1777 from collections.abc import Generator
1778
1779 def g1() -> Generator[Incomplete, Incomplete]: ...
1780 def g2() -> Generator[Incomplete, Incomplete]: ...
1781 def g3() -> Generator[Incomplete, Incomplete]: ...
1782 def g4() -> Generator[Incomplete, Incomplete]: ...
1783 def g5() -> Generator[Incomplete, Incomplete]: ...
1784 def g6() -> Generator[Incomplete, Incomplete, Incomplete]: ...
1785 def g7() -> Generator[Incomplete, Incomplete]: ...
1786
1787 [case testCallable]
1788 from typing import Callable
1789
1790 x: Callable[[int, int], int]
1791 [out]
1792 from typing import Callable
1793
1794 x: Callable[[int, int], int]
1795
1796 [case testAwaitDef]
1797 class F:
1798 async def f(self):
1799 return 1
1800
1801 async def g():
1802 return 2
1803 [out]
1804 class F:
1805 async def f(self): ...
1806
1807 async def g(): ...
1808
1809 [case testCoroutineImportAsyncio]
1810 import asyncio
1811
1812 class F:
1813 @asyncio.coroutine
1814 def f(self):
1815 return 1
1816
1817 @asyncio.coroutine
1818 def g():
1819 return 2
1820
1821 @asyncio.coroutine
1822 def h():
1823 return 3
1824 [out]
1825 import asyncio
1826
1827 class F:
1828 @asyncio.coroutine
1829 def f(self): ...
1830
1831 @asyncio.coroutine
1832 def g(): ...
1833 @asyncio.coroutine
1834 def h(): ...
1835
1836 [case testCoroutineImportAsyncioCoroutines]
1837 import asyncio.coroutines
1838
1839 class F:
1840 @asyncio.coroutines.coroutine
1841 def f(self):
1842 return 1
1843
1844 @asyncio.coroutines.coroutine
1845 def g():
1846 return 2
1847 [out]
1848 import asyncio.coroutines
1849
1850 class F:
1851 @asyncio.coroutines.coroutine
1852 def f(self): ...
1853
1854 @asyncio.coroutines.coroutine
1855 def g(): ...
1856
1857 [case testCoroutineImportAsyncioCoroutinesSub]
1858 import asyncio
1859
1860 class F:
1861 @asyncio.coroutines.coroutine
1862 def f(self):
1863 return 1
1864
1865 @asyncio.coroutines.coroutine
1866 def g():
1867 return 2
1868 [out]
1869 import asyncio
1870
1871 class F:
1872 @asyncio.coroutines.coroutine
1873 def f(self): ...
1874
1875 @asyncio.coroutines.coroutine
1876 def g(): ...
1877
1878 [case testCoroutineImportTypes]
1879 import types
1880
1881 class F:
1882 @types.coroutine
1883 def f(self):
1884 return 1
1885
1886 @types.coroutine
1887 def g():
1888 return 2
1889 [out]
1890 import types
1891
1892 class F:
1893 @types.coroutine
1894 def f(self): ...
1895
1896 @types.coroutine
1897 def g(): ...
1898
1899 [case testCoroutineFromAsyncioImportCoroutine]
1900 from asyncio import coroutine
1901
1902 class F:
1903 @coroutine
1904 def f(self):
1905 return 1
1906
1907 @coroutine
1908 def g():
1909 return 2
1910 [out]
1911 from asyncio import coroutine
1912
1913 class F:
1914 @coroutine
1915 def f(self): ...
1916
1917 @coroutine
1918 def g(): ...
1919
1920 [case testCoroutineFromAsyncioCoroutinesImportCoroutine]
1921 from asyncio.coroutines import coroutine
1922
1923 class F:
1924 @coroutine
1925 def f(self):
1926 return 1
1927
1928 @coroutine
1929 def g():
1930 return 2
1931 [out]
1932 from asyncio.coroutines import coroutine
1933
1934 class F:
1935 @coroutine
1936 def f(self): ...
1937
1938 @coroutine
1939 def g(): ...
1940
1941 [case testCoroutineFromTypesImportCoroutine]
1942 from types import coroutine
1943
1944 class F:
1945 @coroutine
1946 def f(self):
1947 return 1
1948
1949 @coroutine
1950 def g():
1951 return 2
1952 [out]
1953 from types import coroutine
1954
1955 class F:
1956 @coroutine
1957 def f(self): ...
1958
1959 @coroutine
1960 def g(): ...
1961
1962 [case testCoroutineFromAsyncioImportCoroutineAsC]
1963 from asyncio import coroutine as c
1964
1965 class F:
1966 @c
1967 def f(self):
1968 return 1
1969
1970 @c
1971 def g():
1972 return 2
1973 [out]
1974 from asyncio import coroutine as c
1975
1976 class F:
1977 @c
1978 def f(self): ...
1979
1980 @c
1981 def g(): ...
1982
1983 [case testCoroutineFromAsyncioCoroutinesImportCoroutineAsC]
1984 from asyncio.coroutines import coroutine as c
1985
1986 class F:
1987 @c
1988 def f(self):
1989 return 1
1990
1991 @c
1992 def g():
1993 return 2
1994 [out]
1995 from asyncio.coroutines import coroutine as c
1996
1997 class F:
1998 @c
1999 def f(self): ...
2000
2001 @c
2002 def g(): ...
2003
2004 [case testCoroutineFromTypesImportCoroutineAsC]
2005 from types import coroutine as c
2006
2007 class F:
2008 @c
2009 def f(self):
2010 return 1
2011
2012 @c
2013 def g():
2014 return 2
2015 [out]
2016 from types import coroutine as c
2017
2018 class F:
2019 @c
2020 def f(self): ...
2021
2022 @c
2023 def g(): ...
2024
2025 [case testCoroutineImportAsyncioAsA]
2026 import asyncio as a
2027
2028 class F:
2029 @a.coroutine
2030 def f(self):
2031 return 1
2032
2033 @a.coroutine
2034 def g():
2035 return 2
2036 [out]
2037 import asyncio as a
2038
2039 class F:
2040 @a.coroutine
2041 def f(self): ...
2042
2043 @a.coroutine
2044 def g(): ...
2045
2046 [case testCoroutineImportAsyncioCoroutinesAsC]
2047 import asyncio.coroutines as c
2048
2049 class F:
2050 @c.coroutine
2051 def f(self):
2052 return 1
2053
2054 @c.coroutine
2055 def g():
2056 return 2
2057 [out]
2058 import asyncio.coroutines as c
2059
2060 class F:
2061 @c.coroutine
2062 def f(self): ...
2063
2064 @c.coroutine
2065 def g(): ...
2066
2067 [case testCoroutineImportAsyncioCoroutinesSubAsA]
2068 import asyncio as a
2069
2070 class F:
2071 @a.coroutines.coroutine
2072 def f(self):
2073 return 1
2074
2075 @a.coroutines.coroutine
2076 def g():
2077 return 2
2078 [out]
2079 import asyncio as a
2080
2081 class F:
2082 @a.coroutines.coroutine
2083 def f(self): ...
2084
2085 @a.coroutines.coroutine
2086 def g(): ...
2087
2088 [case testCoroutineImportTypesAsT]
2089 import types as t
2090
2091 class F:
2092 @t.coroutine
2093 def f(self):
2094 return 1
2095
2096 @t.coroutine
2097 def g():
2098 return 2
2099 [out]
2100 import types as t
2101
2102 class F:
2103 @t.coroutine
2104 def f(self): ...
2105
2106 @t.coroutine
2107 def g(): ...
2108
2109
2110 -- Tests for stub generation from semantically analyzed trees.
2111 -- These tests are much slower, so use the `_semanal` suffix only when needed.
2112
2113 [case testNestedClass_semanal]
2114 class Outer:
2115 class Inner:
2116 pass
2117
2118 A = Outer.Inner
2119 [out]
2120 class Outer:
2121 class Inner: ...
2122 A = Outer.Inner
2123
2124 -- needs improvement
2125 [case testNestedClass_inspect]
2126 class Outer:
2127 class Inner:
2128 pass
2129
2130 A = Outer.Inner
2131 [out]
2132 class Outer:
2133 class Inner: ...
2134
2135 class A: ...
2136
2137 [case testFunctionAlias_semanal]
2138 from asyncio import coroutine
2139
2140 @coroutine
2141 def start_server():
2142 ...
2143
2144 start = start_server
2145 [out]
2146 from asyncio import coroutine
2147
2148 @coroutine
2149 def start_server() -> None: ...
2150 start = start_server
2151
2152 [case testModuleAlias_semanal]
2153 import a
2154
2155 b = a
2156 [file a.py]
2157 x = 1
2158 [out]
2159 import a
2160
2161 b = a
2162
2163 [case testBadAliasNested_semanal]
2164 import a
2165
2166 x = registry[a.f]
2167 [file a.py]
2168 def f(): ...
2169 [out]
2170 from _typeshed import Incomplete
2171
2172 x: Incomplete
2173
2174 [case testCrossModuleClass_semanal]
2175 import a
2176
2177 class C:
2178 x: A
2179 def f(self) -> A: ...
2180 A = a.A
2181 [file a.py]
2182 class A: ...
2183 [out]
2184 import a
2185
2186 class C:
2187 x: A
2188 def f(self) -> A: ...
2189 A = a.A
2190
2191 [case testCrossModuleFunction_semanal]
2192 import a
2193 g = a.f
2194 [file a.py]
2195 def f(): ...
2196 [out]
2197 import a
2198
2199 g = a.f
2200
2201 [case testPrivateAliasesExcluded_semanal]
2202 import a, _a
2203
2204 class C: ...
2205
2206 A = a._A
2207 B = _a.f
2208 _C = C
2209 [file a.py]
2210 class _A: ...
2211 [file _a.py]
2212 def f(): ...
2213 [out]
2214 from _typeshed import Incomplete
2215
2216 class C: ...
2217
2218 A: Incomplete
2219 B: Incomplete
2220
2221 [case testPrivateAliasesIncluded_semanal]
2222 # flags: --include-private
2223 import a, _a
2224
2225 class C: ...
2226
2227 A = a._A
2228 B = _a.f
2229 _C = C
2230 [file a.py]
2231 class _A: ...
2232 [file _a.py]
2233 def f(): ...
2234 [out]
2235 import _a
2236 import a
2237
2238 class C: ...
2239 A = a._A
2240 B = _a.f
2241 _C = C
2242
2243 [case testFinalWrapped_semanal]
2244 from typing import Final
2245
2246 x: Final = 1
2247 y: Final = x
2248 z: Final[object]
2249 t: Final
2250 [out]
2251 from _typeshed import Incomplete
2252 from typing import Final
2253
2254 x: Final[int]
2255 y: Final[Incomplete]
2256 z: Final[object]
2257 t: Final[Incomplete]
2258
2259 [case testFinalInvalid_semanal]
2260 Final = 'boom'
2261
2262 x: Final = 1
2263 [out]
2264 Final: str
2265 x: Final
2266
2267 [case testNoFunctionNested_semanal]
2268 import a
2269 from typing import Dict, Any
2270
2271 funcs: Dict[Any, Any]
2272 f = funcs[a.f]
2273 [out]
2274 from _typeshed import Incomplete
2275 from typing import Any
2276
2277 funcs: dict[Any, Any]
2278 f: Incomplete
2279
2280 [case testAbstractMethodNameExpr]
2281 from abc import ABCMeta, abstractmethod
2282
2283 class A(metaclass=ABCMeta):
2284 @abstractmethod
2285 def meth(self):
2286 pass
2287 [out]
2288 from abc import ABCMeta, abstractmethod
2289
2290 class A(metaclass=ABCMeta):
2291 @abstractmethod
2292 def meth(self): ...
2293
2294 [case testAbstractMethodMemberExpr]
2295 import abc
2296
2297 class A(metaclass=abc.ABCMeta):
2298 @abc.abstractmethod
2299 def meth(self):
2300 pass
2301 [out]
2302 import abc
2303
2304 class A(metaclass=abc.ABCMeta):
2305 @abc.abstractmethod
2306 def meth(self): ...
2307
2308 [case testAbstractMethodMemberExpr2]
2309 import abc as _abc
2310
2311 class A(metaclass=abc.ABCMeta):
2312 @_abc.abstractmethod
2313 def meth(self):
2314 pass
2315 [out]
2316 import abc as _abc
2317
2318 class A(metaclass=abc.ABCMeta):
2319 @_abc.abstractmethod
2320 def meth(self): ...
2321
2322 [case testABCMeta_semanal]
2323 from base import Base
2324 from abc import abstractmethod
2325
2326 class C(Base):
2327 @abstractmethod
2328 def other(self):
2329 pass
2330
2331 [file base.py]
2332 from abc import abstractmethod, ABCMeta
2333
2334 class Base(metaclass=ABCMeta):
2335 @abstractmethod
2336 def meth(self):
2337 pass
2338 [out]
2339 import abc
2340 from abc import abstractmethod
2341 from base import Base
2342
2343 class C(Base, metaclass=abc.ABCMeta):
2344 @abstractmethod
2345 def other(self): ...
2346
2347 [case testInvalidNumberOfArgsInAnnotation]
2348 def f(x):
2349 # type: () -> int
2350 return ''
2351
2352 [out]
2353 def f(x): ...
2354
2355 [case testFunctionPartiallyAnnotated]
2356 def f(x) -> None:
2357 pass
2358
2359 def g(x, y: str):
2360 pass
2361
2362 class A:
2363 def f(self, x) -> None:
2364 pass
2365
2366 [out]
2367 def f(x) -> None: ...
2368 def g(x, y: str): ...
2369
2370 class A:
2371 def f(self, x) -> None: ...
2372
2373 -- Same as above
2374 [case testFunctionPartiallyAnnotated_inspect]
2375 def f(x) -> None:
2376 pass
2377
2378 def g(x, y: str):
2379 pass
2380
2381 class A:
2382 def f(self, x) -> None:
2383 pass
2384
2385 [out]
2386 def f(x) -> None: ...
2387 def g(x, y: str): ...
2388
2389 class A:
2390 def f(self, x) -> None: ...
2391
2392 [case testExplicitAnyArg]
2393 from typing import Any
2394
2395 def f(x: Any):
2396 pass
2397 def g(x, y: Any) -> str:
2398 pass
2399 def h(x: Any) -> str:
2400 pass
2401
2402 [out]
2403 from typing import Any
2404
2405 def f(x: Any): ...
2406 def g(x, y: Any) -> str: ...
2407 def h(x: Any) -> str: ...
2408
2409 -- Same as above
2410 [case testExplicitAnyArg_inspect]
2411 from typing import Any
2412
2413 def f(x: Any):
2414 pass
2415 def g(x, y: Any) -> str:
2416 pass
2417 def h(x: Any) -> str:
2418 pass
2419
2420 [out]
2421 from typing import Any
2422
2423 def f(x: Any): ...
2424 def g(x, y: Any) -> str: ...
2425 def h(x: Any) -> str: ...
2426
2427 [case testExplicitReturnedAny]
2428 from typing import Any
2429
2430 def f(x: str) -> Any:
2431 pass
2432 def g(x, y: str) -> Any:
2433 pass
2434 def h(x) -> Any:
2435 pass
2436
2437 [out]
2438 from typing import Any
2439
2440 def f(x: str) -> Any: ...
2441 def g(x, y: str) -> Any: ...
2442 def h(x) -> Any: ...
2443
2444 [case testPlacementOfDecorators]
2445 class A:
2446 @property
2447 def x(self):
2448 self.y = 'y'
2449 return 'x'
2450
2451 class B:
2452 @property
2453 def x(self):
2454 return 'x'
2455 @x.setter
2456 def x(self, value):
2457 self.y = 'y'
2458 @x.deleter
2459 def x(self):
2460 del self.y
2461
2462 [out]
2463 class A:
2464 y: str
2465 @property
2466 def x(self): ...
2467
2468 class B:
2469 @property
2470 def x(self): ...
2471 y: str
2472 @x.setter
2473 def x(self, value) -> None: ...
2474 @x.deleter
2475 def x(self) -> None: ...
2476
2477 [case testMisplacedTypeComment]
2478 def f():
2479 x = 0
2480
2481 # type: str
2482 y = ''
2483
2484 [out]
2485 def f() -> None: ...
2486
2487 [case testConditionalImportAll_semanal]
2488 __all__ = ['cookielib']
2489
2490 if object():
2491 from http import cookiejar as cookielib
2492 else:
2493 import cookielib
2494
2495 [out]
2496 import cookielib as cookielib
2497
2498 __all__ = ['cookielib']
2499
2500 [case testCannotCalculateMRO_semanal]
2501 class X: pass
2502
2503 class int(int, X): # Cycle
2504 pass
2505
2506 class A: pass
2507 class B(A): pass
2508 class C(B): pass
2509 class D(A, B): pass # No consistent method resolution order
2510 class E(C, D): pass # Ditto
2511
2512 [out]
2513 class X: ...
2514 class int(int, X): ...
2515 class A: ...
2516 class B(A): ...
2517 class C(B): ...
2518 class D(A, B): ...
2519 class E(C, D): ...
2520
2521 [case testUnreachableCode_semanal]
2522 MYPY = False
2523 class A: pass
2524 if MYPY:
2525 class C(A):
2526 def f(self) -> None: pass
2527 else:
2528 def f(i):
2529 return i
2530
2531 class C(A):
2532 def g(self) -> None: pass
2533 [out]
2534 MYPY: bool
2535
2536 class A: ...
2537
2538 class C(A):
2539 def f(self) -> None: ...
2540
2541 [case testAbstractPropertyImportAlias]
2542 import abc as abc_alias
2543
2544 class A:
2545 @abc_alias.abstractproperty
2546 def x(self): pass
2547
2548 [out]
2549 import abc as abc_alias
2550
2551 class A:
2552 @property
2553 @abc_alias.abstractmethod
2554 def x(self): ...
2555
2556 [case testAbstractPropertyFromImportAlias]
2557 from abc import abstractproperty as ap
2558
2559 class A:
2560 @ap
2561 def x(self): pass
2562
2563 [out]
2564 import abc
2565
2566 class A:
2567 @property
2568 @abc.abstractmethod
2569 def x(self): ...
2570
2571 [case testAbstractProperty1_semanal]
2572 import other
2573 import abc
2574
2575 class A:
2576 @abc.abstractproperty
2577 def x(self): pass
2578
2579 [out]
2580 import abc
2581
2582 class A(metaclass=abc.ABCMeta):
2583 @property
2584 @abc.abstractmethod
2585 def x(self): ...
2586
2587 [case testAbstractProperty2_semanal]
2588 import other
2589 from abc import abstractproperty
2590
2591 class A:
2592 @abstractproperty
2593 def x(self): pass
2594
2595 [out]
2596 import abc
2597
2598 class A(metaclass=abc.ABCMeta):
2599 @property
2600 @abc.abstractmethod
2601 def x(self): ...
2602
2603 [case testAbstractProperty3_semanal]
2604 import other
2605 from abc import abstractproperty as alias_name
2606
2607 class A:
2608 @alias_name
2609 def x(self): pass
2610
2611 [out]
2612 import abc
2613
2614 class A(metaclass=abc.ABCMeta):
2615 @property
2616 @abc.abstractmethod
2617 def x(self): ...
2618
2619 [case testClassWithNameIncomplete]
2620 Y = object()
2621
2622 def g():
2623 yield 1
2624
2625 x = g()
2626
2627 class Incomplete:
2628 pass
2629
2630 [out]
2631 from _typeshed import Incomplete as _Incomplete
2632 from collections.abc import Generator
2633
2634 Y: _Incomplete
2635
2636 def g() -> Generator[_Incomplete]: ...
2637
2638 x: _Incomplete
2639
2640 class Incomplete: ...
2641
2642 [case testExportedNameImported]
2643 # modules: main a b
2644 from a import C
2645
2646 class D(C): pass
2647
2648 [file a.py]
2649 from b import C
2650
2651 [file b.py]
2652 class C: pass
2653
2654 [out]
2655 # main.pyi
2656 from a import C
2657
2658 class D(C): ...
2659 # a.pyi
2660 from b import C as C
2661 # b.pyi
2662 class C: ...
2663
2664 [case testVendoredSix]
2665 from p1.vendored import six
2666 from p1.vendor.six import foobar
2667 from p1.packages.six.moves import http_client
2668 from .packages.six.moves import queue
2669 from p1.vendored.six.moves.http_client import foo
2670 from p1.vendored.six.moves.urllib.parse import bar
2671
2672 class C(http_client.HTTPMessage): pass
2673 class D(six.Iterator): pass
2674
2675 [out]
2676 import six
2677 from six import foobar as foobar
2678 from six.moves import http_client, queue as queue
2679 from six.moves.http_client import foo as foo
2680 from six.moves.urllib.parse import bar as bar
2681
2682 class C(http_client.HTTPMessage): ...
2683 class D(six.Iterator): ...
2684
2685 [case testVendoredPackage]
2686 # modules: main p.vendored.requests p.sub.requests
2687 from p.vendored.requests import Request
2688 from p.sub.requests import Request2
2689
2690 x = Request()
2691 y = Request2()
2692
2693 [file p/__init__.py]
2694
2695 [file p/vendored/__init__.py]
2696
2697 [file p/vendored/requests.py]
2698 class Request:
2699 pass
2700
2701 [file p/sub/__init__.py]
2702
2703 [file p/sub/requests.py]
2704 class Request2:
2705 pass
2706
2707 [out]
2708 # main.pyi
2709 from _typeshed import Incomplete
2710
2711 x: Incomplete
2712 y: Incomplete
2713 <out/p/vendored/requests.pyi was not generated>
2714 # p/sub/requests.pyi
2715 class Request2: ...
2716
2717 [case testTestFiles]
2718 # modules: p p.x p.tests p.tests.test_foo
2719
2720 [file p/__init__.py]
2721 def f(): pass
2722
2723 [file p/x.py]
2724 def g(): pass
2725
2726 [file p/tests/__init__.py]
2727
2728 [file p/tests/test_foo.py]
2729 def test_thing(): pass
2730
2731 [out]
2732 # p/__init__.pyi
2733 def f() -> None: ...
2734 # p/x.pyi
2735 def g() -> None: ...
2736 <out/p/tests.pyi was not generated>
2737 <out/p/tests/test_foo.pyi was not generated>
2738
2739 [case testTestFiles_import]
2740 # modules: p p.x p.tests p.tests.test_foo
2741
2742 [file p/__init__.py]
2743 def f(): pass
2744
2745 [file p/x.py]
2746 def g(): pass
2747
2748 [file p/tests/__init__.py]
2749
2750 [file p/tests/test_foo.py]
2751 def test_thing(): pass
2752
2753 [out]
2754 # p/__init__.pyi
2755 def f() -> None: ...
2756 # p/x.pyi
2757 def g() -> None: ...
2758 <out/p/tests.pyi was not generated>
2759 <out/p/tests/test_foo.pyi was not generated>
2760
2761 [case testTestFiles_inspect]
2762 # modules: p p.x p.tests p.tests.test_foo
2763
2764 [file p/__init__.py]
2765 def f(): pass
2766
2767 [file p/x.py]
2768 def g(): pass
2769
2770 [file p/tests/__init__.py]
2771
2772 [file p/tests/test_foo.py]
2773 def test_thing(): pass
2774
2775 [out]
2776 # p/__init__.pyi
2777 def f(): ...
2778 # p/x.pyi
2779 def g(): ...
2780 <out/p/tests.pyi was not generated>
2781 <out/p/tests/test_foo.pyi was not generated>
2782
2783 [case testVerboseFlag]
2784 # Just test that --verbose does not break anything in a basic test case.
2785 # flags: --verbose
2786
2787 def f(x, y): pass
2788 [out]
2789 def f(x, y) -> None: ...
2790
2791 [case testImportedModuleExits_import]
2792 # modules: a b c
2793
2794 [file a.py]
2795 def g(): pass
2796
2797 [file b.py]
2798 import sys
2799 def f(): pass
2800 sys.exit(1)
2801
2802 [file c.py]
2803 x = 0
2804
2805 [out]
2806 # a.pyi
2807 def g() -> None: ...
2808 # b.pyi
2809 def f() -> None: ...
2810 # c.pyi
2811 x: int
2812
2813 [case testImportedModuleHardExits_import]
2814 # modules: a b c
2815
2816 [file a.py]
2817 def g(): pass
2818
2819 [file b.py]
2820 import os
2821 def f(): pass
2822 os._exit(1) # Kill process
2823
2824 [file c.py]
2825 x = 0
2826
2827 [out]
2828 # a.pyi
2829 def g() -> None: ...
2830 # b.pyi
2831 def f() -> None: ...
2832 # c.pyi
2833 x: int
2834
2835 [case testImportedModuleHardExits2_import]
2836 # modules: p/a p/b p/c
2837
2838 [file p/__init__.py]
2839
2840 [file p/a.py]
2841 def g(): pass
2842
2843 [file p/b.py]
2844 import os
2845 def f(): pass
2846 os._exit(1) # Kill process
2847
2848 [file p/c.py]
2849 x = 0
2850
2851 [out]
2852 # p/a.pyi
2853 def g() -> None: ...
2854 # p/b.pyi
2855 def f() -> None: ...
2856 # p/c.pyi
2857 x: int
2858
2859 [case testImportedModuleHardExits3_import]
2860 # modules: p p/a
2861
2862 [file p/__init__.py]
2863 import os
2864 def f(): pass
2865 os._exit(1) # Kill process
2866
2867 [file p/a.py]
2868 def g(): pass
2869
2870 [out]
2871 # p/__init__.pyi
2872 def f() -> None: ...
2873 # p/a.pyi
2874 def g() -> None: ...
2875
2876 [case testImportedModuleHardExits4_import]
2877 # flags: -p p
2878 # modules: p p/a
2879
2880 [file p/__init__.py]
2881 def ff(): pass
2882
2883 [file p/a.py]
2884 import os
2885 def gg(): pass
2886 os._exit(1) # Kill process
2887
2888 [out]
2889 # p/__init__.pyi
2890 def ff() -> None: ...
2891 # p/a.pyi
2892 def gg() -> None: ...
2893
2894 [case testExportInternalImportsByDefault]
2895 # modules: p p/a
2896
2897 [file p/__init__.py]
2898 from p.a import A, f
2899 from m import C
2900
2901 a: A
2902 c: C
2903 f()
2904
2905 [file p/a.py]
2906 class A: pass
2907 def f(): pass
2908
2909 [file m.py]
2910 class C: pass
2911
2912 [out]
2913 # p/__init__.pyi
2914 from m import C
2915 from p.a import A as A, f as f
2916
2917 a: A
2918 c: C
2919 # p/a.pyi
2920 class A: ...
2921
2922 def f() -> None: ...
2923
2924 [case testNoExportOfInternalImportsIfAll_import]
2925 # modules: p p/a
2926
2927 [file p/__init__.py]
2928 from p.a import A
2929
2930 __all__ = ['a']
2931
2932 a = None # type: A
2933 b = 0 # type: int
2934
2935 [file p/a.py]
2936 class A: pass
2937
2938 [out]
2939 # p/__init__.pyi
2940 from p.a import A
2941
2942 __all__ = ['a']
2943
2944 a: A
2945 # p/a.pyi
2946 class A: ...
2947
2948 [case testExportInternalImportsByDefaultFromUnderscorePackage]
2949 # modules: p
2950
2951 [file p.py]
2952 from _p import A
2953 from _m import B
2954 from _pm import C
2955
2956 a: A
2957 b: B
2958 c: C
2959
2960 [file _p.py]
2961 class A: pass
2962
2963 [file _m.py]
2964 class B: pass
2965
2966 [file _pm.py]
2967 class C: pass
2968
2969 [out]
2970 from _m import B
2971 from _p import A as A
2972 from _pm import C
2973
2974 a: A
2975 b: B
2976 c: C
2977
2978 [case testDisableExportOfInternalImports]
2979 # flags: --export-less
2980 # modules: p p/a
2981
2982 [file p/__init__.py]
2983 from p.a import A, B
2984 from m import C
2985
2986 a: A
2987 c: C
2988
2989 [file p/a.py]
2990 class A: pass
2991 class B: pass
2992
2993 [file m.py]
2994 class C: pass
2995
2996 [out]
2997 # p/__init__.pyi
2998 from m import C
2999 from p.a import A, B as B
3000
3001 a: A
3002 c: C
3003 # p/a.pyi
3004 class A: ...
3005 class B: ...
3006
3007 [case testExportInternalImportsByDefaultUsingRelativeImport]
3008 # modules: p.a
3009
3010 [file p/__init__.py]
3011
3012 [file p/a.py]
3013 from .b import f
3014 f()
3015
3016 [file p/b.py]
3017 def f(): pass
3018
3019 [out]
3020 from .b import f as f
3021
3022 [case testExportInternalImportsByDefaultSkipPrivate]
3023 # modules: p.a
3024
3025 [file p/__init__.py]
3026
3027 [file p/a.py]
3028 from .b import _f, _g as _g, _i
3029 from p.b import _h
3030 _f()
3031 _h()
3032
3033 [file p/b.py]
3034 def _f(): pass
3035 def _g(): pass
3036 def _h(): pass
3037 def _i(): pass
3038 x = 0
3039
3040 [out]
3041
3042 [case testExportInternalImportsByDefaultIncludePrivate]
3043 # flags: --include-private
3044 # modules: p.a
3045
3046 [file p/__init__.py]
3047
3048 [file p/a.py]
3049 from .b import _f
3050 _f()
3051
3052 [file p/b.py]
3053 def _f(): pass
3054
3055 [out]
3056 from .b import _f as _f
3057
3058 [case testHideDunderModuleAttributes]
3059 from m import (
3060 __about__,
3061 __author__,
3062 __copyright__,
3063 __email__,
3064 __license__,
3065 __summary__,
3066 __title__,
3067 __uri__,
3068 __version__
3069 )
3070
3071 class A:
3072 __uri__ = 0
3073
3074 [file m.py]
3075 __about__ = ''
3076 __author__ = ''
3077 __copyright__ = ''
3078 __email__ = ''
3079 __license__ = ''
3080 __summary__ = ''
3081 __title__ = ''
3082 __uri__ = ''
3083 __version__ = ''
3084
3085 [out]
3086 from m import __version__ as __version__
3087
3088 class A: ...
3089
3090 [case testHideDunderModuleAttributesWithAll_import]
3091 from m import (
3092 __about__,
3093 __author__,
3094 __copyright__,
3095 __email__,
3096 __license__,
3097 __summary__,
3098 __title__,
3099 __uri__,
3100 __version__
3101 )
3102
3103 __all__ = ['__about__', '__author__', '__version__']
3104
3105 [file m.py]
3106 __about__ = ''
3107 __author__ = ''
3108 __copyright__ = ''
3109 __email__ = ''
3110 __license__ = ''
3111 __summary__ = ''
3112 __title__ = ''
3113 __uri__ = ''
3114 __version__ = ''
3115
3116 [out]
3117 from m import __about__ as __about__, __author__ as __author__, __version__ as __version__
3118
3119 __all__ = ['__about__', '__author__', '__version__']
3120
3121 [case testAttrsClass_semanal]
3122 import attrs
3123
3124 @attrs.define
3125 class C:
3126 x: int = attrs.field()
3127
3128 [out]
3129 import attrs
3130
3131 @attrs.define
3132 class C:
3133 x: int = attrs.field()
3134
3135 [case testNamedTupleInClass]
3136 from collections import namedtuple
3137
3138 class C:
3139 N = namedtuple('N', ['x', 'y'])
3140 [out]
3141 from _typeshed import Incomplete
3142 from typing import NamedTuple
3143
3144 class C:
3145 class N(NamedTuple):
3146 x: Incomplete
3147 y: Incomplete
3148
3149 [case testImports_directImportsWithAlias]
3150 import p.a as a
3151 import p.b as b
3152
3153 x: a.X
3154 y: b.Y
3155
3156 [out]
3157 import p.a as a
3158 import p.b as b
3159
3160 x: a.X
3161 y: b.Y
3162
3163 [case testImports_directImportsMixed]
3164 import p.a
3165 import p.a as a
3166 import p.b as b
3167
3168 x: a.X
3169 y: b.Y
3170 z: p.a.X
3171
3172 [out]
3173 import p.a
3174 import p.a as a
3175 import p.b as b
3176
3177 x: a.X
3178 y: b.Y
3179 z: p.a.X
3180
3181 [case testImport_overwrites_directWithAlias_from]
3182 import p.a as a
3183 from p import a
3184
3185 x: a.X
3186
3187 [out]
3188 from p import a
3189
3190 x: a.X
3191
3192 [case testImport_overwrites_directWithAlias_fromWithAlias]
3193 import p.a as a
3194 from p import b as a
3195
3196 x: a.X
3197
3198 [out]
3199 from p import b as a
3200
3201 x: a.X
3202
3203 [case testImports_overwrites_direct_from]
3204 import a
3205 from p import a
3206
3207 x: a.X
3208
3209 [out]
3210 from p import a
3211
3212 x: a.X
3213
3214 [case testImports_overwrites_direct_fromWithAlias]
3215 import a
3216 from p import b as a
3217
3218 x: a.X
3219
3220 [out]
3221 from p import b as a
3222
3223 x: a.X
3224
3225 [case testImports_overwrites_from_directWithAlias]
3226 from p import a
3227 import p.a as a
3228
3229 x: a.X
3230
3231 [out]
3232 import p.a as a
3233
3234 x: a.X
3235
3236 [case testImports_overwrites_fromWithAlias_direct]
3237 import a
3238 from p import b as a
3239
3240 x: a.X
3241
3242 [out]
3243 from p import b as a
3244
3245 x: a.X
3246
3247 [case testImports_direct]
3248 import p.a
3249 import pp
3250
3251 x: a.X
3252 y: p.a.Y
3253
3254 [out]
3255 import p.a
3256
3257 x: a.X
3258 y: p.a.Y
3259
3260 [case testNestedImports]
3261 import p
3262 import p.m1
3263 import p.m2
3264
3265 x: p.X
3266 y: p.m1.Y
3267 z: p.m2.Z
3268
3269 [out]
3270 import p
3271 import p.m1
3272 import p.m2
3273
3274 x: p.X
3275 y: p.m1.Y
3276 z: p.m2.Z
3277
3278 [case testNestedImportsAliased]
3279 import p as t
3280 import p.m1 as pm1
3281 import p.m2 as pm2
3282
3283 x: t.X
3284 y: pm1.Y
3285 z: pm2.Z
3286
3287 [out]
3288 import p as t
3289 import p.m1 as pm1
3290 import p.m2 as pm2
3291
3292 x: t.X
3293 y: pm1.Y
3294 z: pm2.Z
3295
3296 [case testNestedFromImports]
3297 from p import m1
3298 from p.m1 import sm1
3299 from p.m2 import sm2
3300
3301 x: m1.X
3302 y: sm1.Y
3303 z: sm2.Z
3304
3305 [out]
3306 from p import m1
3307 from p.m1 import sm1
3308 from p.m2 import sm2
3309
3310 x: m1.X
3311 y: sm1.Y
3312 z: sm2.Z
3313
3314 [case testOverload_fromTypingImport]
3315 from typing import Tuple, Union, overload
3316
3317 class A:
3318 @overload
3319 def f(self, x: int, y: int) -> int:
3320 ...
3321
3322 @overload
3323 def f(self, x: Tuple[int, int]) -> int:
3324 ...
3325
3326 def f(self, *args: Union[int, Tuple[int, int]]) -> int:
3327 pass
3328
3329 @overload
3330 def f(x: int, y: int) -> int:
3331 ...
3332
3333 @overload
3334 def f(x: Tuple[int, int]) -> int:
3335 ...
3336
3337 def f(*args: Union[int, Tuple[int, int]]) -> int:
3338 pass
3339
3340
3341 [out]
3342 from typing import overload
3343
3344 class A:
3345 @overload
3346 def f(self, x: int, y: int) -> int: ...
3347 @overload
3348 def f(self, x: tuple[int, int]) -> int: ...
3349
3350 @overload
3351 def f(x: int, y: int) -> int: ...
3352 @overload
3353 def f(x: tuple[int, int]) -> int: ...
3354
3355 [case testOverload_fromTypingExtensionsImport]
3356 from typing import Tuple, Union
3357 from typing_extensions import overload
3358
3359 class A:
3360 @overload
3361 def f(self, x: int, y: int) -> int:
3362 ...
3363
3364 @overload
3365 def f(self, x: Tuple[int, int]) -> int:
3366 ...
3367
3368 def f(self, *args: Union[int, Tuple[int, int]]) -> int:
3369 pass
3370
3371 @overload
3372 def f(x: int, y: int) -> int:
3373 ...
3374
3375 @overload
3376 def f(x: Tuple[int, int]) -> int:
3377 ...
3378
3379 def f(*args: Union[int, Tuple[int, int]]) -> int:
3380 pass
3381
3382
3383 [out]
3384 from typing_extensions import overload
3385
3386 class A:
3387 @overload
3388 def f(self, x: int, y: int) -> int: ...
3389 @overload
3390 def f(self, x: tuple[int, int]) -> int: ...
3391
3392 @overload
3393 def f(x: int, y: int) -> int: ...
3394 @overload
3395 def f(x: tuple[int, int]) -> int: ...
3396
3397 [case testOverload_importTyping]
3398 import typing
3399 import typing_extensions
3400
3401 class A:
3402 @typing.overload
3403 def f(self, x: int, y: int) -> int:
3404 ...
3405
3406 @typing.overload
3407 def f(self, x: typing.Tuple[int, int]) -> int:
3408 ...
3409
3410 def f(self, *args: typing.Union[int, typing.Tuple[int, int]]) -> int:
3411 pass
3412
3413 @typing.overload
3414 @classmethod
3415 def g(cls, x: int, y: int) -> int:
3416 ...
3417
3418 @typing.overload
3419 @classmethod
3420 def g(cls, x: typing.Tuple[int, int]) -> int:
3421 ...
3422
3423 @classmethod
3424 def g(self, *args: typing.Union[int, typing.Tuple[int, int]]) -> int:
3425 pass
3426
3427 @typing.overload
3428 def f(x: int, y: int) -> int:
3429 ...
3430
3431 @typing.overload
3432 def f(x: typing.Tuple[int, int]) -> int:
3433 ...
3434
3435 def f(*args: typing.Union[int, typing.Tuple[int, int]]) -> int:
3436 pass
3437
3438 @typing_extensions.overload
3439 def g(x: int, y: int) -> int:
3440 ...
3441
3442 @typing_extensions.overload
3443 def g(x: typing.Tuple[int, int]) -> int:
3444 ...
3445
3446 def g(*args: typing.Union[int, typing.Tuple[int, int]]) -> int:
3447 pass
3448
3449
3450 [out]
3451 import typing
3452 import typing_extensions
3453
3454 class A:
3455 @typing.overload
3456 def f(self, x: int, y: int) -> int: ...
3457 @typing.overload
3458 def f(self, x: tuple[int, int]) -> int: ...
3459 @typing.overload
3460 @classmethod
3461 def g(cls, x: int, y: int) -> int: ...
3462 @typing.overload
3463 @classmethod
3464 def g(cls, x: tuple[int, int]) -> int: ...
3465
3466 @typing.overload
3467 def f(x: int, y: int) -> int: ...
3468 @typing.overload
3469 def f(x: tuple[int, int]) -> int: ...
3470 @typing_extensions.overload
3471 def g(x: int, y: int) -> int: ...
3472 @typing_extensions.overload
3473 def g(x: tuple[int, int]) -> int: ...
3474
3475 [case testOverload_importTypingAs]
3476 import typing as t
3477 import typing_extensions as te
3478
3479 class A:
3480 @t.overload
3481 def f(self, x: int, y: int) -> int:
3482 ...
3483
3484 @t.overload
3485 def f(self, x: t.Tuple[int, int]) -> int:
3486 ...
3487
3488 def f(self, *args: typing.Union[int, t.Tuple[int, int]]) -> int:
3489 pass
3490
3491 @t.overload
3492 @classmethod
3493 def g(cls, x: int, y: int) -> int:
3494 ...
3495
3496 @t.overload
3497 @classmethod
3498 def g(cls, x: t.Tuple[int, int]) -> int:
3499 ...
3500
3501 @classmethod
3502 def g(self, *args: t.Union[int, t.Tuple[int, int]]) -> int:
3503 pass
3504
3505 @t.overload
3506 def f(x: int, y: int) -> int:
3507 ...
3508
3509 @t.overload
3510 def f(x: t.Tuple[int, int]) -> int:
3511 ...
3512
3513 def f(*args: t.Union[int, t.Tuple[int, int]]) -> int:
3514 pass
3515
3516
3517 @te.overload
3518 def g(x: int, y: int) -> int:
3519 ...
3520
3521 @te.overload
3522 def g(x: t.Tuple[int, int]) -> int:
3523 ...
3524
3525 def g(*args: t.Union[int, t.Tuple[int, int]]) -> int:
3526 pass
3527
3528 [out]
3529 import typing as t
3530 import typing_extensions as te
3531
3532 class A:
3533 @t.overload
3534 def f(self, x: int, y: int) -> int: ...
3535 @t.overload
3536 def f(self, x: tuple[int, int]) -> int: ...
3537 @t.overload
3538 @classmethod
3539 def g(cls, x: int, y: int) -> int: ...
3540 @t.overload
3541 @classmethod
3542 def g(cls, x: tuple[int, int]) -> int: ...
3543
3544 @t.overload
3545 def f(x: int, y: int) -> int: ...
3546 @t.overload
3547 def f(x: tuple[int, int]) -> int: ...
3548 @te.overload
3549 def g(x: int, y: int) -> int: ...
3550 @te.overload
3551 def g(x: tuple[int, int]) -> int: ...
3552
3553 [case testOverloadFromImportAlias]
3554 from typing import overload as t_overload
3555 from typing_extensions import overload as te_overload
3556
3557 @t_overload
3558 def f(x: int, y: int) -> int:
3559 ...
3560
3561 @te_overload
3562 def g(x: int, y: int) -> int:
3563 ...
3564
3565 [out]
3566 from typing import overload as t_overload
3567 from typing_extensions import overload as te_overload
3568
3569 @t_overload
3570 def f(x: int, y: int) -> int: ...
3571 @te_overload
3572 def g(x: int, y: int) -> int: ...
3573
3574 [case testProtocol_semanal]
3575 from typing import Protocol, TypeVar
3576
3577 class P(Protocol):
3578 def f(self, x: int, y: int) -> str:
3579 ...
3580
3581 T = TypeVar('T')
3582 T2 = TypeVar('T2')
3583 class PT(Protocol[T, T2]):
3584 def f(self, x: T) -> T2:
3585 ...
3586
3587 [out]
3588 from typing import Protocol, TypeVar
3589
3590 class P(Protocol):
3591 def f(self, x: int, y: int) -> str: ...
3592 T = TypeVar('T')
3593 T2 = TypeVar('T2')
3594
3595 class PT(Protocol[T, T2]):
3596 def f(self, x: T) -> T2: ...
3597
3598 [case testProtocolAbstractMethod_semanal]
3599 from abc import abstractmethod
3600 from typing import Protocol
3601
3602 class P(Protocol):
3603 @abstractmethod
3604 def f(self, x: int, y: int) -> str:
3605 ...
3606
3607 [out]
3608 from abc import abstractmethod
3609 from typing import Protocol
3610
3611 class P(Protocol):
3612 @abstractmethod
3613 def f(self, x: int, y: int) -> str: ...
3614
3615 [case testNonDefaultKeywordOnlyArgAfterAsterisk]
3616 def func(*, non_default_kwarg: bool, default_kwarg: bool = True): ...
3617 [out]
3618 def func(*, non_default_kwarg: bool, default_kwarg: bool = True): ...
3619
3620 [case testNestedGenerator]
3621 def f1():
3622 def g():
3623 yield 0
3624 return 0
3625 def f2():
3626 def g():
3627 yield from [0]
3628 return 0
3629 [out]
3630 def f1(): ...
3631 def f2(): ...
3632
3633 [case testIncludeDocstrings]
3634 # flags: --include-docstrings
3635 class A:
3636 """class docstring
3637
3638 a multiline 😊 docstring"""
3639 def func():
3640 """func docstring
3641 don't forget to indent"""
3642 ...
3643 def nodoc():
3644 ...
3645 class B:
3646 def quoteA():
3647 '''func docstring with quotes"""\\n
3648 and an end quote\''''
3649 ...
3650 def quoteB():
3651 '''func docstring with quotes"""
3652 \'\'\'
3653 and an end quote\\"'''
3654 ...
3655 def quoteC():
3656 """func docstring with end quote\\\""""
3657 ...
3658 def quoteD():
3659 r'''raw with quotes\"'''
3660 ...
3661 [out]
3662 class A:
3663 """class docstring
3664
3665 a multiline 😊 docstring"""
3666 def func() -> None:
3667 """func docstring
3668 don't forget to indent"""
3669 def nodoc() -> None: ...
3670
3671 class B:
3672 def quoteA() -> None:
3673 '''func docstring with quotes"""\\n
3674 and an end quote\''''
3675 def quoteB() -> None:
3676 '''func docstring with quotes"""
3677 \'\'\'
3678 and an end quote\\"'''
3679 def quoteC() -> None:
3680 '''func docstring with end quote\\"'''
3681 def quoteD() -> None:
3682 '''raw with quotes\\"'''
3683
3684 [case testIgnoreDocstrings]
3685 class A:
3686 """class docstring
3687
3688 a multiline docstring"""
3689 def func():
3690 """func docstring
3691
3692 don't forget to indent"""
3693 def nodoc():
3694 ...
3695
3696 class B:
3697 def func():
3698 """func docstring"""
3699 ...
3700 def nodoc():
3701 ...
3702
3703 [out]
3704 class A:
3705 def func() -> None: ...
3706 def nodoc() -> None: ...
3707
3708 class B:
3709 def func() -> None: ...
3710 def nodoc() -> None: ...
3711
3712 [case testKnownMagicMethodsReturnTypes]
3713 class Some:
3714 def __len__(self): ...
3715 def __length_hint__(self): ...
3716 def __init__(self): ...
3717 def __del__(self): ...
3718 def __bool__(self): ...
3719 def __bytes__(self): ...
3720 def __format__(self, spec): ...
3721 def __contains__(self, obj): ...
3722 def __complex__(self): ...
3723 def __int__(self): ...
3724 def __float__(self): ...
3725 def __index__(self): ...
3726 [out]
3727 class Some:
3728 def __len__(self) -> int: ...
3729 def __length_hint__(self) -> int: ...
3730 def __init__(self) -> None: ...
3731 def __del__(self) -> None: ...
3732 def __bool__(self) -> bool: ...
3733 def __bytes__(self) -> bytes: ...
3734 def __format__(self, spec) -> str: ...
3735 def __contains__(self, obj) -> bool: ...
3736 def __complex__(self) -> complex: ...
3737 def __int__(self) -> int: ...
3738 def __float__(self) -> float: ...
3739 def __index__(self) -> int: ...
3740
3741 -- Same as above
3742 [case testKnownMagicMethodsReturnTypes_inspect]
3743 class Some:
3744 def __len__(self): ...
3745 def __length_hint__(self): ...
3746 def __init__(self): ...
3747 def __del__(self): ...
3748 def __bool__(self): ...
3749 def __bytes__(self): ...
3750 def __format__(self, spec): ...
3751 def __contains__(self, obj): ...
3752 def __complex__(self): ...
3753 def __int__(self): ...
3754 def __float__(self): ...
3755 def __index__(self): ...
3756 [out]
3757 class Some:
3758 def __len__(self) -> int: ...
3759 def __length_hint__(self) -> int: ...
3760 def __init__(self) -> None: ...
3761 def __del__(self) -> None: ...
3762 def __bool__(self) -> bool: ...
3763 def __bytes__(self) -> bytes: ...
3764 def __format__(self, spec) -> str: ...
3765 def __contains__(self, obj) -> bool: ...
3766 def __complex__(self) -> complex: ...
3767 def __int__(self) -> int: ...
3768 def __float__(self) -> float: ...
3769 def __index__(self) -> int: ...
3770
3771
3772 [case testKnownMagicMethodsArgTypes]
3773 class MismatchNames:
3774 def __exit__(self, tp, val, tb): ...
3775
3776 class MatchNames:
3777 def __exit__(self, type, value, traceback): ...
3778
3779 [out]
3780 import types
3781
3782 class MismatchNames:
3783 def __exit__(self, tp: type[BaseException] | None, val: BaseException | None, tb: types.TracebackType | None) -> None: ...
3784
3785 class MatchNames:
3786 def __exit__(self, type: type[BaseException] | None, value: BaseException | None, traceback: types.TracebackType | None) -> None: ...
3787
3788 -- Same as above (but can generate import statements)
3789 [case testKnownMagicMethodsArgTypes_inspect]
3790 class MismatchNames:
3791 def __exit__(self, tp, val, tb): ...
3792
3793 class MatchNames:
3794 def __exit__(self, type, value, traceback): ...
3795
3796 [out]
3797 import types
3798
3799 class MismatchNames:
3800 def __exit__(self, tp: type[BaseException] | None, val: BaseException | None, tb: types.TracebackType | None): ...
3801
3802 class MatchNames:
3803 def __exit__(self, type: type[BaseException] | None, value: BaseException | None, traceback: types.TracebackType | None): ...
3804
3805 [case testTypeVarPEP604Bound]
3806 from typing import TypeVar
3807 T = TypeVar("T", bound=str | None)
3808 [out]
3809 from typing import TypeVar
3810
3811 T = TypeVar('T', bound=str | None)
3812
3813
3814 [case testPEP604UnionType]
3815 a: str | int
3816
3817 def f(x: str | None) -> None: ...
3818 [out]
3819 a: str | int
3820
3821 def f(x: str | None) -> None: ...
3822
3823 [case testTypeddict]
3824 import typing, x
3825 D1 = typing.TypedDict('D1', {'a': int, 'b': str})
3826 D2 = typing.TypedDict('D2', {'a': int, 'b': str}, total=False)
3827 D3 = typing.TypedDict('D3', {'a': int, 'b': str}, closed=True)
3828 D4 = typing.TypedDict('D4', {'a': int, 'b': str}, closed=True, total=False)
3829 [out]
3830 from typing_extensions import TypedDict
3831
3832 class D1(TypedDict):
3833 a: int
3834 b: str
3835
3836 class D2(TypedDict, total=False):
3837 a: int
3838 b: str
3839
3840 class D3(TypedDict, closed=True):
3841 a: int
3842 b: str
3843
3844 class D4(TypedDict, total=False, closed=True):
3845 a: int
3846 b: str
3847
3848 [case testTypeddictClassWithKeyword]
3849 from typing import TypedDict
3850 class MyDict(TypedDict, total=False):
3851 foo: str
3852 bar: int
3853 [out]
3854 from typing import TypedDict
3855
3856 class MyDict(TypedDict, total=False):
3857 foo: str
3858 bar: int
3859
3860 [case testTypeddictKeywordSyntax]
3861 from typing import TypedDict
3862
3863 X = TypedDict('X', a=int, b=str)
3864 Y = TypedDict('X', a=int, b=str, total=False)
3865 [out]
3866 from typing_extensions import TypedDict
3867
3868 class X(TypedDict):
3869 a: int
3870 b: str
3871
3872 class Y(TypedDict, total=False):
3873 a: int
3874 b: str
3875
3876 [case testTypeddictWithNonIdentifierOrKeywordKeys]
3877 from typing import TypedDict
3878 X = TypedDict('X', {'a-b': int, 'c': str})
3879 Y = TypedDict('X', {'a-b': int, 'c': str}, total=False)
3880 Z = TypedDict('X', {'a': int, 'in': str})
3881 [out]
3882 from typing import TypedDict
3883
3884 X = TypedDict('X', {'a-b': int, 'c': str})
3885 Y = TypedDict('X', {'a-b': int, 'c': str}, total=False)
3886 Z = TypedDict('X', {'a': int, 'in': str})
3887
3888 [case testEmptyTypeddict]
3889 import typing
3890 X = typing.TypedDict('X', {})
3891 Y = typing.TypedDict('Y', {}, total=False)
3892 Z = typing.TypedDict('Z')
3893 W = typing.TypedDict('W', total=False)
3894 [out]
3895 from typing_extensions import TypedDict
3896
3897 class X(TypedDict): ...
3898 class Y(TypedDict, total=False): ...
3899 class Z(TypedDict): ...
3900 class W(TypedDict, total=False): ...
3901
3902 [case testTypeddictAliased]
3903 from typing import TypedDict as t_TypedDict
3904 from typing_extensions import TypedDict as te_TypedDict
3905 def f(): ...
3906 X = t_TypedDict('X', {'a': int, 'b': str})
3907 Y = te_TypedDict('Y', {'a': int, 'b': str})
3908 def g(): ...
3909 [out]
3910 from typing_extensions import TypedDict
3911
3912 def f() -> None: ...
3913
3914 class X(TypedDict):
3915 a: int
3916 b: str
3917
3918 class Y(TypedDict):
3919 a: int
3920 b: str
3921
3922 def g() -> None: ...
3923
3924 [case testTypeddictFromImportAlias]
3925 import typing as t
3926 import typing_extensions as te
3927 X = t.TypedDict('X', {'a': int, 'b': str})
3928 Y = te.TypedDict('Y', {'a': int, 'b': str})
3929 [out]
3930 from typing_extensions import TypedDict
3931
3932 class X(TypedDict):
3933 a: int
3934 b: str
3935
3936 class Y(TypedDict):
3937 a: int
3938 b: str
3939
3940 [case testNotTypeddict]
3941 from x import TypedDict
3942 import y
3943 X = TypedDict('X', {'a': int, 'b': str})
3944 Y = y.TypedDict('Y', {'a': int, 'b': str})
3945 [out]
3946 from _typeshed import Incomplete
3947
3948 X: Incomplete
3949 Y: Incomplete
3950
3951 [case testTypeddictWithWrongAttributesType]
3952 from typing import TypedDict
3953 R = TypedDict("R", {"a": int, **{"b": str, "c": bytes}})
3954 S = TypedDict("S", [("b", str), ("c", bytes)])
3955 T = TypedDict("T", {"a": int}, b=str, total=False)
3956 U = TypedDict("U", {"a": int}, totale=False)
3957 V = TypedDict("V", {"a": int}, {"b": str})
3958 W = TypedDict("W", **{"a": int, "b": str})
3959 [out]
3960 from _typeshed import Incomplete
3961
3962 R: Incomplete
3963 S: Incomplete
3964 T: Incomplete
3965 U: Incomplete
3966 V: Incomplete
3967 W: Incomplete
3968
3969 [case testUseTypingName]
3970 import collections
3971 import typing
3972 from typing import NamedTuple, TypedDict
3973
3974 class Incomplete: ...
3975 class Generator: ...
3976 class NamedTuple: ...
3977 class TypedDict: ...
3978
3979 nt = collections.namedtuple("nt", "a b")
3980 NT = typing.NamedTuple("NT", [("a", int), ("b", str)])
3981 NT1 = typing.NamedTuple("NT1", [("a", int)] + [("b", str)])
3982 NT2 = typing.NamedTuple("NT2", [(xx, int), ("b", str)])
3983 NT3 = typing.NamedTuple(xx, [("a", int), ("b", str)])
3984 TD = typing.TypedDict("TD", {"a": int, "b": str})
3985 TD1 = typing.TypedDict("TD1", {"a": int, "b": str}, totale=False)
3986 TD2 = typing.TypedDict("TD2", {xx: int, "b": str})
3987 TD3 = typing.TypedDict(xx, {"a": int, "b": str})
3988
3989 def gen():
3990 y = yield x
3991 return z
3992
3993 def gen2():
3994 y = yield from x
3995 return z
3996
3997 class X(unknown_call("X", "a b")): ...
3998 class Y(collections.namedtuple("Y", xx)): ...
3999 [out]
4000 from _typeshed import Incomplete as _Incomplete
4001 from collections.abc import Generator as _Generator
4002 from typing import NamedTuple as _NamedTuple
4003 from typing_extensions import TypedDict as _TypedDict
4004
4005 class Incomplete: ...
4006 class Generator: ...
4007 class NamedTuple: ...
4008 class TypedDict: ...
4009
4010 class nt(_NamedTuple):
4011 a: _Incomplete
4012 b: _Incomplete
4013
4014 class NT(_NamedTuple):
4015 a: int
4016 b: str
4017
4018 NT1: _Incomplete
4019 NT2: _Incomplete
4020 NT3: _Incomplete
4021
4022 class TD(_TypedDict):
4023 a: int
4024 b: str
4025
4026 TD1: _Incomplete
4027 TD2: _Incomplete
4028 TD3: _Incomplete
4029
4030 def gen() -> _Generator[_Incomplete, _Incomplete, _Incomplete]: ...
4031 def gen2() -> _Generator[_Incomplete, _Incomplete, _Incomplete]: ...
4032
4033 class X(_Incomplete): ...
4034 class Y(_Incomplete): ...
4035
4036 [case testIgnoreLongDefaults]
4037 def f(x='abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz\
4038 abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz\
4039 abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz\
4040 abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz'): ...
4041
4042 def g(x=b'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz\
4043 abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz\
4044 abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz\
4045 abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz'): ...
4046
4047 def h(x=123456789012345678901234567890123456789012345678901234567890\
4048 123456789012345678901234567890123456789012345678901234567890\
4049 123456789012345678901234567890123456789012345678901234567890\
4050 123456789012345678901234567890123456789012345678901234567890): ...
4051
4052 [out]
4053 def f(x: str = ...) -> None: ...
4054 def g(x: bytes = ...) -> None: ...
4055 def h(x: int = ...) -> None: ...
4056
4057 [case testDefaultsOfBuiltinContainers]
4058 def f(x=(), y=(1,), z=(1, 2)): ...
4059 def g(x=[], y=[1, 2]): ...
4060 def h(x={}, y={1: 2, 3: 4}): ...
4061 def i(x={1, 2, 3}): ...
4062 def j(x=[(1,"a"), (2,"b")]): ...
4063
4064 [out]
4065 def f(x=(), y=(1,), z=(1, 2)) -> None: ...
4066 def g(x=[], y=[1, 2]) -> None: ...
4067 def h(x={}, y={1: 2, 3: 4}) -> None: ...
4068 def i(x={1, 2, 3}) -> None: ...
4069 def j(x=[(1, 'a'), (2, 'b')]) -> None: ...
4070
4071 [case testDefaultsOfBuiltinContainersWithNonTrivialContent]
4072 def f(x=(1, u.v), y=(k(),), z=(w,)): ...
4073 def g(x=[1, u.v], y=[k()], z=[w]): ...
4074 def h(x={1: u.v}, y={k(): 2}, z={m: m}, w={**n}): ...
4075 def i(x={u.v, 2}, y={3, k()}, z={w}): ...
4076
4077 [out]
4078 def f(x=..., y=..., z=...) -> None: ...
4079 def g(x=..., y=..., z=...) -> None: ...
4080 def h(x=..., y=..., z=..., w=...) -> None: ...
4081 def i(x=..., y=..., z=...) -> None: ...
4082
4083 [case testDataclass]
4084 import dataclasses
4085 import dataclasses as dcs
4086 from dataclasses import dataclass, field, Field, InitVar, KW_ONLY
4087 from dataclasses import dataclass as dc
4088 from datetime import datetime
4089 from typing import ClassVar
4090
4091 @dataclasses.dataclass
4092 class X:
4093 a: int
4094 b: str = "hello"
4095 c: ClassVar
4096 d: ClassVar = 200
4097 f: list[int] = field(init=False, default_factory=list)
4098 g: int = field(default=2, kw_only=True)
4099 _: KW_ONLY
4100 h: int = 1
4101 i: InitVar[str]
4102 j: InitVar = 100
4103 # Lambda not supported yet -> marked as Incomplete instead
4104 k: str = Field(
4105 default_factory=lambda: datetime.utcnow().isoformat(" ", timespec="seconds")
4106 )
4107 non_field = None
4108
4109 @dcs.dataclass
4110 class Y: ...
4111
4112 @dataclass
4113 class Z: ...
4114
4115 @dc
4116 class W: ...
4117
4118 @dataclass(init=False, repr=False)
4119 class V: ...
4120
4121 [out]
4122 import dataclasses
4123 import dataclasses as dcs
4124 from _typeshed import Incomplete
4125 from dataclasses import Field, InitVar, KW_ONLY, dataclass, dataclass as dc, field
4126 from typing import ClassVar
4127
4128 @dataclasses.dataclass
4129 class X:
4130 a: int
4131 b: str = ...
4132 c: ClassVar
4133 d: ClassVar = ...
4134 f: list[int] = field(init=False, default_factory=list)
4135 g: int = field(default=2, kw_only=True)
4136 _: KW_ONLY
4137 h: int = ...
4138 i: InitVar[str]
4139 j: InitVar = ...
4140 k: str = Field(default_factory=Incomplete)
4141 non_field = ...
4142
4143 @dcs.dataclass
4144 class Y: ...
4145 @dataclass
4146 class Z: ...
4147 @dc
4148 class W: ...
4149 @dataclass(init=False, repr=False)
4150 class V: ...
4151
4152 [case testDataclass_semanal]
4153 from dataclasses import Field, InitVar, dataclass, field
4154 from typing import ClassVar
4155 from datetime import datetime
4156
4157 @dataclass
4158 class X:
4159 a: int
4160 b: InitVar[str]
4161 c: str = "hello"
4162 d: ClassVar
4163 e: ClassVar = 200
4164 f: list[int] = field(init=False, default_factory=list)
4165 g: int = field(default=2, kw_only=True)
4166 h: int = 1
4167 i: InitVar = 100
4168 j: list[int] = field(default_factory=list)
4169 # Lambda not supported yet -> marked as Incomplete instead
4170 k: str = Field(
4171 default_factory=lambda: datetime.utcnow().isoformat(" ", timespec="seconds")
4172 )
4173 non_field = None
4174
4175 @dataclass(init=False, repr=False, frozen=True)
4176 class Y: ...
4177
4178 [out]
4179 from _typeshed import Incomplete
4180 from dataclasses import Field, InitVar, dataclass, field
4181 from typing import ClassVar
4182
4183 @dataclass
4184 class X:
4185 a: int
4186 b: InitVar[str]
4187 c: str = ...
4188 d: ClassVar
4189 e: ClassVar = ...
4190 f: list[int] = field(init=False, default_factory=list)
4191 g: int = field(default=2, kw_only=True)
4192 h: int = ...
4193 i: InitVar = ...
4194 j: list[int] = field(default_factory=list)
4195 k: str = Field(default_factory=Incomplete)
4196 non_field = ...
4197
4198 @dataclass(init=False, repr=False, frozen=True)
4199 class Y: ...
4200
4201 [case testDataclassWithKwOnlyField_semanal]
4202 # flags: --python-version=3.10
4203 from dataclasses import dataclass, field, InitVar, KW_ONLY
4204 from typing import ClassVar
4205
4206 @dataclass
4207 class X:
4208 a: int
4209 b: str = "hello"
4210 c: ClassVar
4211 d: ClassVar = 200
4212 f: list[int] = field(init=False, default_factory=list)
4213 g: int = field(default=2, kw_only=True)
4214 _: KW_ONLY
4215 h: int = 1
4216 i: InitVar[str]
4217 j: InitVar = 100
4218 non_field = None
4219
4220 @dataclass(init=False, repr=False, frozen=True)
4221 class Y: ...
4222
4223 [out]
4224 from dataclasses import InitVar, KW_ONLY, dataclass, field
4225 from typing import ClassVar
4226
4227 @dataclass
4228 class X:
4229 a: int
4230 b: str = ...
4231 c: ClassVar
4232 d: ClassVar = ...
4233 f: list[int] = field(init=False, default_factory=list)
4234 g: int = field(default=2, kw_only=True)
4235 _: KW_ONLY
4236 h: int = ...
4237 i: InitVar[str]
4238 j: InitVar = ...
4239 non_field = ...
4240
4241 @dataclass(init=False, repr=False, frozen=True)
4242 class Y: ...
4243
4244 [case testDataclassWithExplicitGeneratedMethodsOverrides_semanal]
4245 from dataclasses import dataclass
4246
4247 @dataclass
4248 class X:
4249 a: int
4250 def __init__(self, a: int, b: str = ...) -> None: ...
4251 def __post_init__(self) -> None: ...
4252
4253 [out]
4254 from dataclasses import dataclass
4255
4256 @dataclass
4257 class X:
4258 a: int
4259 def __init__(self, a: int, b: str = ...) -> None: ...
4260 def __post_init__(self) -> None: ...
4261
4262 [case testDataclassInheritsFromAny_semanal]
4263 from dataclasses import dataclass
4264 import missing
4265
4266 @dataclass
4267 class X(missing.Base):
4268 a: int
4269
4270 @dataclass
4271 class Y(missing.Base):
4272 generated_args: str
4273 generated_args_: str
4274 generated_kwargs: float
4275 generated_kwargs_: float
4276
4277 [out]
4278 import missing
4279 from dataclasses import dataclass
4280
4281 @dataclass
4282 class X(missing.Base):
4283 a: int
4284
4285 @dataclass
4286 class Y(missing.Base):
4287 generated_args: str
4288 generated_args_: str
4289 generated_kwargs: float
4290 generated_kwargs_: float
4291
4292 [case testDataclassAliasPrinterVariations_semanal]
4293 from dataclasses import dataclass, field
4294
4295 @dataclass
4296 class X:
4297 a: int = field(default=-1)
4298 b: set[int] = field(default={0})
4299 c: list[int] = field(default=[x for x in range(5)])
4300 d: dict[int, int] = field(default={x: x for x in range(5)})
4301 e: tuple[int, int] = field(default=(1, 2, 3)[1:])
4302 f: tuple[int, int] = field(default=(1, 2, 3)[:2])
4303 g: tuple[int, int] = field(default=(1, 2, 3)[::2])
4304 h: tuple[int] = field(default=(1, 2, 3)[1::2])
4305
4306 [out]
4307 from _typeshed import Incomplete
4308 from dataclasses import dataclass, field
4309
4310 @dataclass
4311 class X:
4312 a: int = field(default=-1)
4313 b: set[int] = field(default={0})
4314 c: list[int] = field(default=Incomplete)
4315 d: dict[int, int] = field(default=Incomplete)
4316 e: tuple[int, int] = field(default=(1, 2, 3)[1:])
4317 f: tuple[int, int] = field(default=(1, 2, 3)[:2])
4318 g: tuple[int, int] = field(default=(1, 2, 3)[::2])
4319 h: tuple[int] = field(default=(1, 2, 3)[1::2])
4320
4321 [case testDataclassTransform]
4322 # dataclass_transform detection only works with semantic analysis.
4323 # Test stubgen doesn't break too badly without it.
4324 from typing_extensions import dataclass_transform
4325
4326 @typing_extensions.dataclass_transform(kw_only_default=True)
4327 def create_model(cls):
4328 return cls
4329
4330 @create_model
4331 class X:
4332 a: int
4333 b: str = "hello"
4334
4335 @typing_extensions.dataclass_transform(kw_only_default=True)
4336 class ModelBase: ...
4337
4338 class Y(ModelBase):
4339 a: int
4340 b: str = "hello"
4341
4342 @typing_extensions.dataclass_transform(kw_only_default=True)
4343 class DCMeta(type): ...
4344
4345 class Z(metaclass=DCMeta):
4346 a: int
4347 b: str = "hello"
4348
4349 [out]
4350 @typing_extensions.dataclass_transform(kw_only_default=True)
4351 def create_model(cls): ...
4352
4353 class X:
4354 a: int
4355 b: str
4356
4357 @typing_extensions.dataclass_transform(kw_only_default=True)
4358 class ModelBase: ...
4359
4360 class Y(ModelBase):
4361 a: int
4362 b: str
4363
4364 @typing_extensions.dataclass_transform(kw_only_default=True)
4365 class DCMeta(type): ...
4366
4367 class Z(metaclass=DCMeta):
4368 a: int
4369 b: str
4370
4371 [case testDataclassTransformDecorator_semanal]
4372 import typing_extensions
4373 from dataclasses import field
4374
4375 @typing_extensions.dataclass_transform(kw_only_default=True)
4376 def create_model(cls):
4377 return cls
4378
4379 @create_model
4380 class X:
4381 a: int
4382 b: str = "hello"
4383 c: bool = field(default=True)
4384
4385 [out]
4386 import typing_extensions
4387 from dataclasses import field
4388
4389 @typing_extensions.dataclass_transform(kw_only_default=True)
4390 def create_model(cls): ...
4391
4392 @create_model
4393 class X:
4394 a: int
4395 b: str = ...
4396 c: bool = field(default=True)
4397
4398 [case testDataclassTransformClass_semanal]
4399 from dataclasses import field
4400 from typing_extensions import dataclass_transform
4401
4402 @dataclass_transform(kw_only_default=True)
4403 class ModelBase: ...
4404
4405 class X(ModelBase):
4406 a: int
4407 b: str = "hello"
4408 c: bool = field(default=True)
4409
4410 [out]
4411 from dataclasses import field
4412 from typing_extensions import dataclass_transform
4413
4414 @dataclass_transform(kw_only_default=True)
4415 class ModelBase: ...
4416
4417 class X(ModelBase):
4418 a: int
4419 b: str = ...
4420 c: bool = field(default=True)
4421
4422 [case testDataclassTransformMetaclass_semanal]
4423 from dataclasses import field
4424 from typing import Any
4425 from typing_extensions import dataclass_transform
4426
4427 def custom_field(*, default: bool, kw_only: bool) -> Any: ...
4428
4429 @dataclass_transform(kw_only_default=True, field_specifiers=(custom_field,))
4430 class DCMeta(type): ...
4431
4432 class X(metaclass=DCMeta):
4433 a: int
4434 b: str = "hello"
4435 c: bool = field(default=True) # should be ignored, not field_specifier here
4436
4437 class Y(X):
4438 d: str = custom_field(default="Hello")
4439
4440 [out]
4441 from typing import Any
4442 from typing_extensions import dataclass_transform
4443
4444 def custom_field(*, default: bool, kw_only: bool) -> Any: ...
4445
4446 @dataclass_transform(kw_only_default=True, field_specifiers=(custom_field,))
4447 class DCMeta(type): ...
4448
4449 class X(metaclass=DCMeta):
4450 a: int
4451 b: str = ...
4452 c: bool = ...
4453
4454 class Y(X):
4455 d: str = custom_field(default='Hello')
4456
4457 [case testAlwaysUsePEP604Union]
4458 import typing
4459 import typing as t
4460 from typing import Optional, Union, Optional as O, Union as U
4461 import x
4462
4463 union = Union[int, str]
4464 bad_union = Union[int]
4465 nested_union = Optional[Union[int, str]]
4466 not_union = x.Union[int, str]
4467 u = U[int, str]
4468 o = O[int]
4469
4470 def f1(a: Union["int", Optional[tuple[int, t.Optional[int]]]]) -> int: ...
4471 def f2(a: typing.Union[int | x.Union[int, int], O[float]]) -> int: ...
4472
4473 [out]
4474 import x
4475 from _typeshed import Incomplete
4476
4477 union = int | str
4478 bad_union = int
4479 nested_union = int | str | None
4480 not_union: Incomplete
4481 u = int | str
4482 o = int | None
4483
4484 def f1(a: int | tuple[int, int | None] | None) -> int: ...
4485 def f2(a: int | x.Union[int, int] | float | None) -> int: ...
4486
4487 [case testTypingBuiltinReplacements]
4488 import typing
4489 import typing as t
4490 from typing import Tuple
4491 import typing_extensions
4492 import typing_extensions as te
4493 from typing_extensions import List, Type
4494
4495 # builtins are not builtins
4496 tuple = int
4497 [list,] = float
4498 dict, set, frozenset = str, float, int
4499
4500 x: Tuple[t.Text, t.FrozenSet[typing.Type[float]]]
4501 y: typing.List[int]
4502 z: t.Dict[str, float]
4503 v: typing.Set[int]
4504 w: List[typing_extensions.Dict[te.FrozenSet[Type[int]], te.Tuple[te.Set[te.Text], ...]]]
4505
4506 x_alias = Tuple[str, ...]
4507 y_alias = typing.List[int]
4508 z_alias = t.Dict[str, float]
4509 v_alias = typing.Set[int]
4510 w_alias = List[typing_extensions.Dict[str, te.Tuple[int, ...]]]
4511
4512 [out]
4513 from _typeshed import Incomplete
4514 from builtins import dict as _dict, frozenset as _frozenset, list as _list, set as _set, tuple as _tuple
4515
4516 tuple = int
4517 list: Incomplete
4518 dict: Incomplete
4519 set: Incomplete
4520 frozenset: Incomplete
4521 x: _tuple[str, _frozenset[type[float]]]
4522 y: _list[int]
4523 z: _dict[str, float]
4524 v: _set[int]
4525 w: _list[_dict[_frozenset[type[int]], _tuple[_set[str], ...]]]
4526 x_alias = _tuple[str, ...]
4527 y_alias = _list[int]
4528 z_alias = _dict[str, float]
4529 v_alias = _set[int]
4530 w_alias = _list[_dict[str, _tuple[int, ...]]]
4531
4532 [case testHandlingNameCollisions]
4533 # flags: --include-private
4534 from typing import Tuple
4535 tuple = int
4536 _tuple = range
4537 __tuple = map
4538 x: Tuple[int, str]
4539 [out]
4540 from builtins import tuple as ___tuple
4541
4542 tuple = int
4543 _tuple = range
4544 __tuple = map
4545 x: ___tuple[int, str]
4546
4547 [case testPEP570PosOnlyParams]
4548 def f(x=0, /): ...
4549 def f1(x: int, /): ...
4550 def f2(x: int, y: float = 1, /): ...
4551 def f3(x: int, /, y: float): ...
4552 def f4(x: int, /, y: float = 1): ...
4553 def f5(x: int, /, *, y: float): ...
4554 def f6(x: int = 0, /, *, y: float): ...
4555 def f7(x: int, /, *, y: float = 1): ...
4556 def f8(x: int = 0, /, *, y: float = 1): ...
4557
4558 [out]
4559 def f(x: int = 0, /) -> None: ...
4560 def f1(x: int, /): ...
4561 def f2(x: int, y: float = 1, /): ...
4562 def f3(x: int, /, y: float): ...
4563 def f4(x: int, /, y: float = 1): ...
4564 def f5(x: int, /, *, y: float): ...
4565 def f6(x: int = 0, /, *, y: float): ...
4566 def f7(x: int, /, *, y: float = 1): ...
4567 def f8(x: int = 0, /, *, y: float = 1): ...
4568
4569 [case testPreserveEmptyTuple]
4570 ann: tuple[()]
4571 alias = tuple[()]
4572 def f(x: tuple[()]): ...
4573 class C(tuple[()]): ...
4574
4575 [out]
4576 ann: tuple[()]
4577 alias = tuple[()]
4578
4579 def f(x: tuple[()]): ...
4580
4581 class C(tuple[()]): ...
4582
4583 [case testPreserveEnumValue_semanal]
4584 from enum import Enum
4585
4586 class Foo(Enum):
4587 A = 1
4588 B = 2
4589 C = 3
4590
4591 class Bar(Enum):
4592 A = object()
4593 B = "a" + "b"
4594
4595 [out]
4596 from enum import Enum
4597
4598 class Foo(Enum):
4599 A = 1
4600 B = 2
4601 C = 3
4602
4603 class Bar(Enum):
4604 A = ...
4605 B = ...
4606
4607 [case testGracefullyHandleInvalidOptionalUsage]
4608 from typing import Optional
4609
4610 x: Optional # invalid
4611 y: Optional[int] # valid
4612 z: Optional[int, str] # invalid
4613 w: Optional[int | str] # valid
4614 r: Optional[type[int | str]]
4615
4616 X = Optional
4617 Y = Optional[int]
4618 Z = Optional[int, str]
4619 W = Optional[int | str]
4620 R = Optional[type[int | str]]
4621
4622 [out]
4623 from _typeshed import Incomplete
4624 from typing import Optional
4625
4626 x: Incomplete
4627 y: int | None
4628 z: Incomplete
4629 w: int | str | None
4630 r: type[int | str] | None
4631 X = Optional
4632 Y = int | None
4633 Z = Incomplete
4634 W = int | str | None
4635 R = type[int | str] | None
4636
4637 [case testClassInheritanceWithKeywordsConstants]
4638 class Test(Whatever, a=1, b='b', c=True, d=1.5, e=None, f=1j, g=b'123'): ...
4639 [out]
4640 class Test(Whatever, a=1, b='b', c=True, d=1.5, e=None, f=1j, g=b'123'): ...
4641
4642 [case testClassInheritanceWithKeywordsDynamic]
4643 class Test(Whatever, keyword=SomeName * 2, attr=SomeName.attr): ...
4644 [out]
4645 class Test(Whatever, keyword=SomeName * 2, attr=SomeName.attr): ...
4646
4647 [case testPEP695GenericClass]
4648 # flags: --python-version=3.12
4649
4650 class C[T]: ...
4651 class C1[T1](int): ...
4652 class C2[T2: int]: ...
4653 class C3[T3: str | bytes]: ...
4654 class C4[T4: (str, bytes)]: ...
4655
4656 class Outer:
4657 class Inner[T]: ...
4658
4659 [out]
4660 class C[T]: ...
4661 class C1[T1](int): ...
4662 class C2[T2: int]: ...
4663 class C3[T3: str | bytes]: ...
4664 class C4[T4: (str, bytes)]: ...
4665
4666 class Outer:
4667 class Inner[T]: ...
4668
4669 [case testPEP695GenericFunction]
4670 # flags: --python-version=3.12
4671
4672 def f1[T1](): ...
4673 def f2[T2: int](): ...
4674 def f3[T3: str | bytes](): ...
4675 def f4[T4: (str, bytes)](): ...
4676
4677 class C:
4678 def m[T](self, x: T) -> T: ...
4679
4680 [out]
4681 def f1[T1]() -> None: ...
4682 def f2[T2: int]() -> None: ...
4683 def f3[T3: str | bytes]() -> None: ...
4684 def f4[T4: (str, bytes)]() -> None: ...
4685
4686 class C:
4687 def m[T](self, x: T) -> T: ...
4688
4689 [case testPEP695TypeAlias]
4690 # flags: --python-version=3.12
4691
4692 type Alias = int | str
4693 type Alias1[T1] = list[T1] | set[T1]
4694 type Alias2[T2: int] = list[T2] | set[T2]
4695 type Alias3[T3: str | bytes] = list[T3] | set[T3]
4696 type Alias4[T4: (str, bytes)] = list[T4] | set[T4]
4697
4698 class C:
4699 type IndentedAlias[T] = list[T]
4700
4701 [out]
4702 type Alias = int | str
4703 type Alias1[T1] = list[T1] | set[T1]
4704 type Alias2[T2: int] = list[T2] | set[T2]
4705 type Alias3[T3: str | bytes] = list[T3] | set[T3]
4706 type Alias4[T4: (str, bytes)] = list[T4] | set[T4]
4707 class C:
4708 type IndentedAlias[T] = list[T]
4709
4710 [case testPEP695Syntax_semanal]
4711 # flags: --python-version=3.12
4712
4713 class C[T]: ...
4714 def f[S](): ...
4715 type A[R] = list[R]
4716
4717 [out]
4718 class C[T]: ...
4719
4720 def f[S]() -> None: ...
4721 type A[R] = list[R]
4722
4723 [case testPEP696Syntax]
4724 # flags: --python-version=3.13
4725
4726 type Alias1[T1 = int] = list[T1] | set[T1]
4727 type Alias2[T2: int | float = int] = list[T2] | set[T2]
4728 class C3[T3 = int]: ...
4729 class C4[T4: int | float = int](list[T4]): ...
4730 def f5[T5 = int](): ...
4731
4732 [out]
4733 type Alias1[T1 = int] = list[T1] | set[T1]
4734 type Alias2[T2: int | float = int] = list[T2] | set[T2]
4735 class C3[T3 = int]: ...
4736 class C4[T4: int | float = int](list[T4]): ...
4737
4738 def f5[T5 = int]() -> None: ...
4739
4740 [case testIgnoreMypyGeneratedMethods_semanal]
4741 # flags: --include-private --python-version=3.13
4742 from typing_extensions import dataclass_transform
4743
4744 @dataclass_transform()
4745 class DCMeta(type): ...
4746 class DC(metaclass=DCMeta):
4747 x: str
4748
4749 [out]
4750 from typing_extensions import dataclass_transform
4751
4752 @dataclass_transform()
4753 class DCMeta(type): ...
4754
4755 class DC(metaclass=DCMeta):
4756 x: str
4757
4758
4759 [case testIncompleteReturn]
4760 from _typeshed import Incomplete
4761
4762 def polar(*args, **kwargs) -> Incomplete:
4763 ...
4764
4765 [out]
4766 from _typeshed import Incomplete
4767
4768 def polar(*args, **kwargs) -> Incomplete: ...