Debian Codegraph @

test-data/unit/check-serialize.test

raw
1 -- Serialization test cases (incremental type checking)
2 --
3 -- These test that modules deserialized from cache files behave
4 -- identically to modules that have undergone full type checking.
5 --
6 -- These tests are written using the same syntax as test cases in
7 -- check-incremental.test. Look at the comment at that the top of
8 -- that file for the details of how these tests work.
9 --
10 -- There is probably some overlap with check-incremental.test, but it
11 -- is perhaps not worth trying to simplify these, since a few redundant
12 -- test cases are cheap but accidentally losing test coverage is bad.
13 --
14 -- These are intended to be straightforward, and do not test import
15 -- cycles and other tricky business. Add test cases for complex things
16 -- to check-incremental.test.
17
18 --
19 -- Basic things
20 --
21
22 [case testSerializeModuleAttribute]
23 import a
24 [file a.py]
25 import b
26 [file a.py.2]
27 import b
28 y = b.x # type: int
29 [file b.py]
30 x = ''
31 -- We only do the following two sections once here to avoid repetition.
32 -- Most other test cases are similar.
33 [rechecked a]
34 [stale a]
35 [out2]
36 tmp/a.py:2: error: Incompatible types in assignment (expression has type "str", variable has type "int")
37
38 --
39 -- Functions
40 --
41
42 [case testSerializeAnnotatedFunction]
43 import a
44 [file a.py]
45 import b
46 [file a.py.2]
47 import b
48 b.f(1)
49 x = b.f('') # type: str
50 [file b.py]
51 def f(x: str) -> int: pass
52 [out2]
53 tmp/a.py:2: error: Argument 1 to "f" has incompatible type "int"; expected "str"
54 tmp/a.py:3: error: Incompatible types in assignment (expression has type "int", variable has type "str")
55
56 [case testSerializeUnannotatedFunction]
57 import a
58 [file a.py]
59 import b
60 [file a.py.2]
61 import b
62 b.f(x=1)
63 b.f()
64 [file b.py]
65 def f(x): pass
66 [out2]
67 tmp/a.py:3: error: Missing positional argument "x" in call to "f"
68
69 [case testSerializeGenericFunction]
70 import a
71 [file a.py]
72 import b
73 [file a.py.2]
74 from b import f
75 reveal_type(f(1))
76 reveal_type(f(x=''))
77 [file b.py]
78 from typing import TypeVar
79
80 T = TypeVar('T')
81
82 def f(x: T) -> T: return x
83 [out2]
84 tmp/a.py:2: note: Revealed type is "builtins.int"
85 tmp/a.py:3: note: Revealed type is "builtins.str"
86
87 [case testSerializeFunctionReturningGenericFunction]
88 import a
89 [file a.py]
90 import b
91 [file a.py.2]
92 import b
93 reveal_type(b.f)
94 reveal_type(b.f()(''))
95 [file b.py]
96 from typing import TypeVar, Callable
97
98 T = TypeVar('T')
99
100 def f() -> Callable[[T], T]: pass
101 [out2]
102 tmp/a.py:2: note: Revealed type is "def () -> def [T] (T`-1) -> T`-1"
103 tmp/a.py:3: note: Revealed type is "builtins.str"
104
105 [case testSerializeArgumentKinds]
106 import a
107 [file a.py]
108 import b
109 [file a.py.2]
110 from b import f
111 f(1, z=1)
112 f(1, '', z=1)
113 f(1, y='', z=1)
114 f(1, '', 2, 3, z=1)
115 f(1, '', zz=1, z=1)
116 f(1, '', foo='', z=1)
117 [file b.py]
118 def f(x: int,
119 y: str = '',
120 *args: int,
121 z: int,
122 zz: int = 1,
123 **kw: str) -> None: pass
124 [builtins fixtures/dict.pyi]
125 [out2]
126
127 [case testSerializeCallableWithBoundTypeArguments]
128 import a
129 [file a.py]
130 import b
131 [file a.py.2]
132 import b
133 x = b.f
134 [file b.py]
135 from typing import TypeVar, Generic
136
137 T = TypeVar('T')
138
139 class C(Generic[T]):
140 def f(self, x: T) -> None: pass
141
142 c: C[int]
143 f = c.f
144 [out]
145 [out2]
146
147 [case testSerializePositionalOnlyArgument]
148 import a
149 [file a.py]
150 import b
151 [file a.py.2]
152 import b
153 b.f(1)
154 b.f('')
155 b.f(__x=1)
156 [file b.py]
157 def f(__x: int) -> None: pass
158 [out2]
159 tmp/a.py:3: error: Argument 1 to "f" has incompatible type "str"; expected "int"
160 tmp/a.py:4: error: Unexpected keyword argument "__x" for "f"
161 tmp/a.py:4: note: "f" defined in "b"
162
163 [case testSerializeArgumentKindsErrors]
164 import a
165 [file a.py]
166 import b
167 [file a.py.2]
168 from b import f
169 f('', z=1) # Line 2
170 f(1, 2, z=1) # 3
171 f(1, y=1, z=1) # 4
172 f(1, '', 2, '', z=1) # 5
173 f(1, '', z='') # 6
174 f(1, '', zz='', z=1) # 7
175 f(1, '', z=1, foo=1) # 8
176 [file b.py]
177 def f(x: int,
178 y: str = '',
179 *args: int,
180 z: int,
181 zz: int = 1,
182 **kw: str) -> None: pass
183 [builtins fixtures/dict.pyi]
184 [out2]
185 tmp/a.py:2: error: Argument 1 to "f" has incompatible type "str"; expected "int"
186 tmp/a.py:3: error: Argument 2 to "f" has incompatible type "int"; expected "str"
187 tmp/a.py:4: error: Argument "y" to "f" has incompatible type "int"; expected "str"
188 tmp/a.py:5: error: Argument 4 to "f" has incompatible type "str"; expected "int"
189 tmp/a.py:6: error: Argument "z" to "f" has incompatible type "str"; expected "int"
190 tmp/a.py:7: error: Argument "zz" to "f" has incompatible type "str"; expected "int"
191 tmp/a.py:8: error: Argument "foo" to "f" has incompatible type "int"; expected "str"
192
193 [case testSerializeOverloadedFunction]
194 import a
195 [file a.py]
196 import b
197 [file a.py.2]
198 import b
199 reveal_type(b.f(1))
200 reveal_type(b.f(''))
201 [file b.pyi]
202 from typing import overload
203 @overload
204 def f(x: int) -> int: pass
205 @overload
206 def f(x: str) -> str: pass
207 [out2]
208 tmp/a.py:2: note: Revealed type is "builtins.int"
209 tmp/a.py:3: note: Revealed type is "builtins.str"
210
211 [case testSerializeDecoratedFunction]
212 import a
213 [file a.py]
214 import b
215 [file a.py.2]
216 import b
217 reveal_type(b.f(''))
218 b.f(x=1)
219 [file b.py]
220 from typing import Callable
221 def dec(f: Callable[[int], int]) -> Callable[[str], str]: pass
222 @dec
223 def f(x: int) -> int: pass
224 [out2]
225 tmp/a.py:2: note: Revealed type is "builtins.str"
226 tmp/a.py:3: error: Unexpected keyword argument "x" for "f"
227 tmp/a.py:3: note: "f" defined in "b"
228
229 [case testSerializeTypeGuardFunction]
230 import a
231 [file a.py]
232 import b
233 [file a.py.2]
234 import b
235 reveal_type(b.guard(''))
236 reveal_type(b.guard)
237 [file b.py]
238 from typing_extensions import TypeGuard
239 def guard(a: object) -> TypeGuard[str]: pass
240 [builtins fixtures/tuple.pyi]
241 [out2]
242 tmp/a.py:2: note: Revealed type is "builtins.bool"
243 tmp/a.py:3: note: Revealed type is "def (a: builtins.object) -> TypeGuard[builtins.str]"
244 --
245 -- Classes
246 --
247
248 [case testSerializeClassAttribute]
249 import a
250 [file a.py]
251 import b
252 [file a.py.2]
253 import b
254 b.A().x = ''
255 [file b.py]
256 class A:
257 x = 1
258 [out2]
259 tmp/a.py:2: error: Incompatible types in assignment (expression has type "str", variable has type "int")
260
261 [case testSerializeMethod]
262 import a
263 [file a.py]
264 import b
265 [file a.py.2]
266 import b
267 b.A().f('')
268 [file b.py]
269 class A:
270 def f(self, x: int) -> None: pass
271 [out2]
272 tmp/a.py:2: error: Argument 1 to "f" of "A" has incompatible type "str"; expected "int"
273
274 [case testSerialize__init__]
275 import a
276 [file a.py]
277 import b
278 [file a.py.2]
279 from b import A
280 A('')
281 class B(A):
282 def f(self) -> None:
283 super().__init__('')
284 [file b.py]
285 class A:
286 def __init__(self, x: int) -> None: pass
287 [out2]
288 tmp/a.py:2: error: Argument 1 to "A" has incompatible type "str"; expected "int"
289 tmp/a.py:5: error: Argument 1 to "__init__" of "A" has incompatible type "str"; expected "int"
290
291 [case testSerializeOverloaded__init__]
292 import a
293 [file a.py]
294 import b
295 [file a.py.2]
296 from b import A
297 A(object()) # E
298 A(x='')
299 A(0)
300 class B(A):
301 def f(self) -> None:
302 super().__init__(object()) # E
303 super().__init__('')
304 super().__init__(0)
305 [file b.pyi]
306 from typing import overload
307 class A:
308 @overload
309 def __init__(self, x: int) -> None: pass
310 @overload
311 def __init__(self, x: str) -> None: pass
312 [out2]
313 tmp/a.py:2: error: No overload variant of "A" matches argument type "object"
314 tmp/a.py:2: note: Possible overload variants:
315 tmp/a.py:2: note: def A(x: int) -> A
316 tmp/a.py:2: note: def A(x: str) -> A
317 tmp/a.py:7: error: No overload variant of "__init__" of "A" matches argument type "object"
318 tmp/a.py:7: note: Possible overload variants:
319 tmp/a.py:7: note: def __init__(self, x: int) -> None
320 tmp/a.py:7: note: def __init__(self, x: str) -> None
321
322 [case testSerialize__new__]
323 import a
324 [file a.py]
325 import b
326 [file a.py.2]
327 from b import A
328 A('')
329 [file b.py]
330 class A:
331 def __new__(cls, x: int) -> 'A': pass
332 [out2]
333 tmp/a.py:2: error: Argument 1 to "A" has incompatible type "str"; expected "int"
334
335 [case testSerializeClassVar]
336 import a
337 [file a.py]
338 import b
339 [file a.py.2]
340 from b import A
341 A.x = ''
342 A().x = 1
343 [file b.py]
344 from typing import ClassVar
345 class A:
346 x: ClassVar[int]
347 [out2]
348 tmp/a.py:2: error: Incompatible types in assignment (expression has type "str", variable has type "int")
349 tmp/a.py:3: error: Cannot assign to class variable "x" via instance
350
351 [case testSerializeGenericClass]
352 import a
353 [file a.py]
354 import b
355 [file a.py.2]
356 from b import A
357 a1: A[int, str] = A(1)
358 a2: A[int, str] = A('')
359 reveal_type(a1.y)
360 reveal_type(a1.f())
361 [file b.py]
362 from typing import TypeVar, Generic
363 T = TypeVar('T')
364 S = TypeVar('S')
365 class A(Generic[T, S]):
366 x: T
367 y: S
368 def __init__(self, x: T) -> None:
369 self.x = x
370 def f(self) -> T:
371 return self.x
372 [out2]
373 tmp/a.py:3: error: Argument 1 to "A" has incompatible type "str"; expected "int"
374 tmp/a.py:4: note: Revealed type is "builtins.str"
375 tmp/a.py:5: note: Revealed type is "builtins.int"
376
377 [case testSerializeAbstractClass]
378 import a
379 [file a.py]
380 import b
381 [file a.py.2]
382 from b import A
383 A()
384 class B(A):
385 def f(self) -> None: pass
386 x: int
387 B()
388 a: A
389 a.f()
390 a.x = 1
391 [file b.py]
392 from abc import ABCMeta, abstractmethod, abstractproperty
393 class A(metaclass=ABCMeta):
394 @abstractmethod
395 def f(self) -> None: pass
396 @abstractproperty
397 def x(self) -> int: return 0
398 [typing fixtures/typing-medium.pyi]
399 [out2]
400 tmp/a.py:2: error: Cannot instantiate abstract class "A" with abstract attributes "f" and "x"
401 tmp/a.py:9: error: Property "x" defined in "A" is read-only
402
403 [case testSerializeStaticMethod]
404 import a
405 [file a.py]
406 import b
407 [file a.py.2]
408 from b import A
409 A.f(1)
410 A.f()
411 A().f()
412 [file b.py]
413 class A:
414 @staticmethod
415 def f() -> None: pass
416 [builtins fixtures/staticmethod.pyi]
417 [out2]
418 tmp/a.py:2: error: Too many arguments for "f" of "A"
419
420 [case testSerializeClassMethod]
421 import a
422 [file a.py]
423 import b
424 [file a.py.2]
425 from b import A
426 A.f(1)
427 A.f()
428 A().f()
429 [file b.py]
430 class A:
431 @classmethod
432 def f(cls) -> None: pass
433 [builtins fixtures/classmethod.pyi]
434 [out2]
435 tmp/a.py:2: error: Too many arguments for "f" of "A"
436
437 [case testSerializeReadOnlyProperty]
438 import a
439 [file a.py]
440 import b
441 [file a.py.2]
442 from b import A
443 reveal_type(A().x)
444 A().x = 0
445 [file b.py]
446 class A:
447 @property
448 def x(self) -> int: return 0
449 [builtins fixtures/property.pyi]
450 [out2]
451 tmp/a.py:2: note: Revealed type is "builtins.int"
452 tmp/a.py:3: error: Property "x" defined in "A" is read-only
453
454 [case testSerializeReadWriteProperty]
455 import a
456 [file a.py]
457 import b
458 [file a.py.2]
459 from b import A
460 reveal_type(A().x)
461 A().x = ''
462 A().x = 0
463 [file b.py]
464 class A:
465 @property
466 def x(self) -> int: return 0
467 @x.setter
468 def x(self, v: int) -> None: pass
469 [builtins fixtures/property.pyi]
470 [out2]
471 tmp/a.py:2: note: Revealed type is "builtins.int"
472 tmp/a.py:3: error: Incompatible types in assignment (expression has type "str", variable has type "int")
473
474 [case testSerializeSelfType]
475 import a
476 [file a.py]
477 import b
478 [file a.py.2]
479 from b import A
480 reveal_type(A().f())
481 class B(A): pass
482 reveal_type(B().f())
483 [file b.py]
484 from typing import TypeVar
485 T = TypeVar('T', bound='A')
486 class A:
487 def f(self: T) -> T: return self
488 [out2]
489 tmp/a.py:2: note: Revealed type is "b.A"
490 tmp/a.py:4: note: Revealed type is "a.B"
491
492 [case testSerializeInheritance]
493 import a
494 [file a.py]
495 import b
496 [file a.py.2]
497 from b import A, B, C
498 C().f(1) # E
499 C().g(1) # E
500 reveal_type(C().h())
501 a: A = C()
502 b: B = C()
503 i: int = C() # E
504 [file b.py]
505 class A:
506 def f(self) -> int: pass
507 class B:
508 def g(self) -> str: pass
509 def h(self) -> object: pass
510 class C(A, B):
511 def h(self) -> int: pass
512 [out2]
513 tmp/a.py:2: error: Too many arguments for "f" of "A"
514 tmp/a.py:3: error: Too many arguments for "g" of "B"
515 tmp/a.py:4: note: Revealed type is "builtins.int"
516 tmp/a.py:7: error: Incompatible types in assignment (expression has type "C", variable has type "int")
517
518 [case testSerializeGenericInheritance]
519 import a
520 [file a.py]
521 import b
522 [file a.py.2]
523 from b import B
524 b: B[int]
525 reveal_type(b.f())
526 [file b.py]
527 from typing import TypeVar, Generic
528 T = TypeVar('T')
529 class A(Generic[T]):
530 def f(self) -> T: pass
531 class B(A[A[T]]):
532 pass
533 [out2]
534 tmp/a.py:3: note: Revealed type is "b.A[builtins.int]"
535
536 [case testSerializeFixedLengthTupleBaseClass]
537 import a
538 [file a.py]
539 import b
540 [file a.py.2]
541 from b import A
542 a: A
543 a.f(1)
544 reveal_type((a[0], a[1]))
545 [file b.py]
546 from typing import Tuple
547 class A(Tuple[int, str]):
548 def f(self) -> None: pass
549 [builtins fixtures/tuple.pyi]
550 [out2]
551 tmp/a.py:3: error: Too many arguments for "f" of "A"
552 tmp/a.py:4: note: Revealed type is "tuple[builtins.int, builtins.str]"
553
554 [case testSerializeVariableLengthTupleBaseClass]
555 import a
556 [file a.py]
557 import b
558 [file a.py.2]
559 from b import A
560 a: A
561 a.f(1)
562 reveal_type((a[0], a[1]))
563 [file b.py]
564 from typing import Tuple
565 class A(Tuple[int, ...]):
566 def f(self) -> None: pass
567 [builtins fixtures/tuple.pyi]
568 [out2]
569 tmp/a.py:3: error: Too many arguments for "f" of "A"
570 tmp/a.py:4: note: Revealed type is "tuple[builtins.int, builtins.int]"
571
572 [case testSerializePlainTupleBaseClass]
573 import a
574 [file a.py]
575 import b
576 [file a.py.2]
577 from b import A
578 a: A
579 a.f(1)
580 reveal_type((a[0], a[1]))
581 [file b.py]
582 from typing import Tuple
583 class A(tuple):
584 def f(self) -> None: pass
585 [builtins fixtures/tuple.pyi]
586 [out2]
587 tmp/a.py:3: error: Too many arguments for "f" of "A"
588 tmp/a.py:4: note: Revealed type is "tuple[Any, Any]"
589
590 [case testSerializeNamedTupleBaseClass]
591 import a
592 [file a.py]
593 import b
594 [file a.py.2]
595 from b import A
596 a: A
597 a.f(1)
598 reveal_type((a[0], a[1]))
599 reveal_type((a.x, a.y))
600 [file b.py]
601 from typing import NamedTuple
602 class A(NamedTuple('N', [('x', int), ('y', str)])):
603 def f(self) -> None: pass
604 [builtins fixtures/tuple.pyi]
605 [out2]
606 tmp/a.py:3: error: Too many arguments for "f" of "A"
607 tmp/a.py:4: note: Revealed type is "tuple[builtins.int, builtins.str]"
608 tmp/a.py:5: note: Revealed type is "tuple[builtins.int, builtins.str]"
609
610 [case testSerializeAnyBaseClass]
611 import a
612 [file a.py]
613 import b
614 [file a.py.2]
615 from b import B
616 B().f(1)
617 reveal_type(B().xyz)
618 [file b.py]
619 from typing import Any
620 A: Any
621 class B(A):
622 def f(self) -> None: pass
623 [builtins fixtures/tuple.pyi]
624 [out2]
625 tmp/a.py:2: error: Too many arguments for "f" of "B"
626 tmp/a.py:3: note: Revealed type is "Any"
627
628 [case testSerializeIndirectAnyBaseClass]
629 import a
630 [file a.py]
631 import b
632 [file a.py.2]
633 from b import C
634 C().f(1)
635 C().g(1)
636 reveal_type(C().xyz)
637 [file b.py]
638 from typing import Any
639 A: Any
640 class B(A):
641 def f(self) -> None: pass
642 class C(B):
643 def g(self) -> None: pass
644 [builtins fixtures/tuple.pyi]
645 [out2]
646 tmp/a.py:2: error: Too many arguments for "f" of "B"
647 tmp/a.py:3: error: Too many arguments for "g" of "C"
648 tmp/a.py:4: note: Revealed type is "Any"
649
650 [case testSerializeNestedClass]
651 import a
652 [file a.py]
653 import b
654 [file a.py.2]
655 import b
656 b.A.B().f(1)
657 b.A.B.C().g(1)
658 b.b.f(1)
659 b.c.g(1)
660 [file b.py]
661 class A:
662 class B:
663 def f(self) -> None: pass
664 class C:
665 def g(self) -> None: pass
666 b: A.B
667 c: A.B.C
668 [builtins fixtures/tuple.pyi]
669 [out2]
670 tmp/a.py:2: error: Too many arguments for "f" of "B"
671 tmp/a.py:3: error: Too many arguments for "g" of "C"
672 tmp/a.py:4: error: Too many arguments for "f" of "B"
673 tmp/a.py:5: error: Too many arguments for "g" of "C"
674
675 [case testSerializeCallableVsTypeObjectDistinction]
676 import a
677 [file a.py]
678 import b
679 [file a.py.2]
680 import b
681 t: type
682 t = b.A
683 if int():
684 t = b.f # E
685 [file b.py]
686 class A: pass
687 def f() -> None: pass
688 [builtins fixtures/tuple.pyi]
689 [out2]
690 tmp/a.py:5: error: Incompatible types in assignment (expression has type "Callable[[], None]", variable has type "type")
691
692 [case testSerializeOverloadedVsTypeObjectDistinction]
693 import a
694 [file a.py]
695 import b
696 [file a.py.2]
697 import b
698 t: type
699 t = b.A
700 if int():
701 t = b.f # E
702 [file b.pyi]
703 from typing import overload
704 class A:
705 @overload
706 def __init__(self) -> None: pass
707 @overload
708 def __init__(self, x: int) -> None: pass
709 @overload
710 def f() -> None: pass
711 @overload
712 def f(x: int) -> None: pass
713 [builtins fixtures/tuple.pyi]
714 [out2]
715 tmp/a.py:5: error: Incompatible types in assignment (expression has type overloaded function, variable has type "type")
716
717 [case testSerializeNamedTupleInMethod4]
718 from ntcrash import C
719 reveal_type(C().a)
720 reveal_type(C().b)
721 reveal_type(C().c)
722 [file ntcrash.py]
723 from typing import NamedTuple
724 class C:
725 def __init__(self) -> None:
726 A = NamedTuple('A', [('x', int)])
727 self.a = A(0)
728 self.b = A(0) # type: A
729 self.c = A
730 [builtins fixtures/tuple.pyi]
731 [out1]
732 main:2: note: Revealed type is "tuple[builtins.int, fallback=ntcrash.A@4]"
733 main:3: note: Revealed type is "tuple[builtins.int, fallback=ntcrash.A@4]"
734 main:4: note: Revealed type is "def (x: builtins.int) -> tuple[builtins.int, fallback=ntcrash.A@4]"
735 [out2]
736 main:2: note: Revealed type is "tuple[builtins.int, fallback=ntcrash.A@4]"
737 main:3: note: Revealed type is "tuple[builtins.int, fallback=ntcrash.A@4]"
738 main:4: note: Revealed type is "def (x: builtins.int) -> tuple[builtins.int, fallback=ntcrash.A@4]"
739
740 --
741 -- Strict optional
742 --
743
744 [case testSerializeOptionalType]
745 import a
746 [file a.py]
747 import b
748 [file a.py.2]
749 import b
750 reveal_type(b.x)
751 b.f(b.x)
752 [file b.py]
753 from typing import Optional
754 x: Optional[int]
755 def f(x: int) -> None: pass
756 [out2]
757 tmp/a.py:2: note: Revealed type is "builtins.int | None"
758 tmp/a.py:3: error: Argument 1 to "f" has incompatible type "int | None"; expected "int"
759
760 --
761 -- # type: ignore
762 --
763
764 [case testSerializeIgnoredUndefinedType]
765 import b
766 reveal_type(b.x)
767 [file b.py]
768 x: NonExistent # type: ignore
769 [out1]
770 main:2: note: Revealed type is "Any"
771 [out2]
772 main:2: note: Revealed type is "Any"
773
774 [case testSerializeIgnoredInvalidType]
775 import b
776 reveal_type(b.x)
777 [file b.py]
778 A = 0
779 x: A # type: ignore
780 [out1]
781 main:2: note: Revealed type is "A?"
782 [out2]
783 main:2: note: Revealed type is "A?"
784
785 [case testSerializeIgnoredMissingBaseClass]
786 import b
787 reveal_type(b.B())
788 reveal_type(b.B().x)
789 [file b.py]
790 class B(A): pass # type: ignore
791 [out1]
792 main:2: note: Revealed type is "b.B"
793 main:3: note: Revealed type is "Any"
794 [out2]
795 main:2: note: Revealed type is "b.B"
796 main:3: note: Revealed type is "Any"
797
798 [case testSerializeIgnoredInvalidBaseClass]
799 import b
800 reveal_type(b.B())
801 reveal_type(b.B().x)
802 [file b.py]
803 A = 0
804 class B(A): pass # type: ignore
805 [out1]
806 main:2: note: Revealed type is "b.B"
807 main:3: note: Revealed type is "Any"
808 [out2]
809 main:2: note: Revealed type is "b.B"
810 main:3: note: Revealed type is "Any"
811
812 [case testSerializeIgnoredImport]
813 import a
814 [file a.py]
815 import b
816 [file a.py.2]
817 import b
818 reveal_type(b.m)
819 reveal_type(b.x)
820 [file b.py]
821 import m # type: ignore
822 from m import x # type: ignore
823 [out2]
824 tmp/a.py:2: note: Revealed type is "Any"
825 tmp/a.py:3: note: Revealed type is "Any"
826
827 --
828 -- TypeVar
829 --
830
831 [case testSerializeSimpleTypeVar]
832 import a
833 [file a.py]
834 import b
835 [file a.py.2]
836 import b
837 def f(x: b.T) -> b.T: return x
838 reveal_type(f)
839 [file b.py]
840 from typing import TypeVar
841 T = TypeVar('T')
842 [out2]
843 tmp/a.py:3: note: Revealed type is "def [b.T] (x: b.T`-1) -> b.T`-1"
844
845 [case testSerializeBoundedTypeVar]
846 import a
847 [file a.py]
848 import b
849 [file a.py.2]
850 import b
851 def f(x: b.T) -> b.T: return x
852 reveal_type(f)
853 reveal_type(b.g)
854 [file b.py]
855 from typing import TypeVar
856 T = TypeVar('T', bound=int)
857 def g(x: T) -> T: return x
858 [out2]
859 tmp/a.py:3: note: Revealed type is "def [b.T <: builtins.int] (x: b.T`-1) -> b.T`-1"
860 tmp/a.py:4: note: Revealed type is "def [T <: builtins.int] (x: T`-1) -> T`-1"
861
862 [case testSerializeTypeVarWithValues]
863 import a
864 [file a.py]
865 import b
866 [file a.py.2]
867 import b
868 def f(x: b.T) -> b.T: return x
869 reveal_type(f)
870 reveal_type(b.g)
871 [file b.py]
872 from typing import TypeVar
873 T = TypeVar('T', int, str)
874 def g(x: T) -> T: return x
875 [out2]
876 tmp/a.py:3: note: Revealed type is "def [b.T in (builtins.int, builtins.str)] (x: b.T`-1) -> b.T`-1"
877 tmp/a.py:4: note: Revealed type is "def [T in (builtins.int, builtins.str)] (x: T`-1) -> T`-1"
878
879 [case testSerializeTypeVarInClassBody]
880 import a
881 [file a.py]
882 import b
883 [file a.py.2]
884 from b import A
885 def f(x: A.T) -> A.T: return x
886 reveal_type(f)
887 [file b.py]
888 from typing import TypeVar
889 class A:
890 T = TypeVar('T', int, str)
891 [out2]
892 tmp/a.py:3: note: Revealed type is "def [A.T in (builtins.int, builtins.str)] (x: A.T`-1) -> A.T`-1"
893
894 --
895 -- NewType
896 --
897
898 [case testSerializeNewType]
899 import a
900 [file a.py]
901 import b
902 [file a.py.2]
903 import b
904 y: b.N
905 y = 1
906 i = y
907 b.x = 1
908 b.x = y
909 y = b.N(1)
910 y = b.N('')
911 [file b.py]
912 from typing import NewType
913 N = NewType('N', int)
914 x: N
915 [out2]
916 tmp/a.py:3: error: Incompatible types in assignment (expression has type "int", variable has type "N")
917 tmp/a.py:5: error: Incompatible types in assignment (expression has type "int", variable has type "N")
918 tmp/a.py:8: error: Argument 1 to "N" has incompatible type "str"; expected "int"
919
920 --
921 -- Named tuples
922 --
923
924 [case testSerializeNamedTuple]
925 import a
926 [file a.py]
927 import b
928 [file a.py.2]
929 import b
930 from typing import Tuple
931 y: b.N
932 t: Tuple[int]
933 y = t
934 b.x = t
935 t = y
936 b.x = t
937 reveal_type(b.N(x=1))
938 reveal_type(y[0])
939 b.N(x='')
940 [file b.py]
941 from typing import NamedTuple
942 N = NamedTuple('N', [('x', int)])
943 x: N
944 [builtins fixtures/tuple.pyi]
945 [out2]
946 tmp/a.py:5: error: Incompatible types in assignment (expression has type "tuple[int]", variable has type "N")
947 tmp/a.py:6: error: Incompatible types in assignment (expression has type "tuple[int]", variable has type "N")
948 tmp/a.py:9: note: Revealed type is "tuple[builtins.int, fallback=b.N]"
949 tmp/a.py:10: note: Revealed type is "builtins.int"
950 tmp/a.py:11: error: Argument "x" to "N" has incompatible type "str"; expected "int"
951
952 --
953 -- Types and type aliases
954 --
955
956 [case testSerializeTypeAliases]
957 import a
958 [file a.py]
959 import b
960 [file a.py.2]
961 import b
962 d: b.D
963 a: b.A
964 u: b.U
965 l: b.L
966 t: b.T
967 c: b.C
968 ty: b.Ty
969 reveal_type(d)
970 reveal_type(a)
971 reveal_type(u)
972 reveal_type(l)
973 reveal_type(t)
974 reveal_type(c)
975 reveal_type(ty)
976 c2: b.C2
977 reveal_type(c2)
978 ty2: b.Ty2
979 reveal_type(ty2)
980 [file b.py]
981 from typing import Any, Union, List, Tuple, Callable, Type
982 class DD: pass
983 D = DD
984 A = Any
985 U = Union[int, str]
986 L = List[int]
987 T = Tuple[int, str]
988 C = Callable[[int], str]
989 C2 = Callable[..., str]
990 Ty = Type[int]
991 Ty2 = type
992 [builtins fixtures/list.pyi]
993 [out2]
994 tmp/a.py:9: note: Revealed type is "b.DD"
995 tmp/a.py:10: note: Revealed type is "Any"
996 tmp/a.py:11: note: Revealed type is "builtins.int | builtins.str"
997 tmp/a.py:12: note: Revealed type is "builtins.list[builtins.int]"
998 tmp/a.py:13: note: Revealed type is "tuple[builtins.int, builtins.str]"
999 tmp/a.py:14: note: Revealed type is "def (builtins.int) -> builtins.str"
1000 tmp/a.py:15: note: Revealed type is "type[builtins.int]"
1001 tmp/a.py:17: note: Revealed type is "def (*Any, **Any) -> builtins.str"
1002 tmp/a.py:19: note: Revealed type is "builtins.type"
1003
1004 [case testSerializeGenericTypeAlias]
1005 import b
1006 from b import X # Work around https://github.com/python/mypy/issues/2887
1007 t: b.Y[int]
1008 reveal_type(t)
1009 [file b.py]
1010 from typing import TypeVar, Tuple
1011 X = TypeVar('X')
1012 Y = Tuple[X, str]
1013 [builtins fixtures/tuple.pyi]
1014 [out1]
1015 main:4: note: Revealed type is "tuple[builtins.int, builtins.str]"
1016 [out2]
1017 main:4: note: Revealed type is "tuple[builtins.int, builtins.str]"
1018
1019 [case testSerializeTuple]
1020 # Don't repreat types tested by testSerializeTypeAliases here.
1021 import a
1022 [file a.py]
1023 import b
1024 [file a.py.2]
1025 import b
1026 reveal_type(b.x)
1027 reveal_type(b.y)
1028 [file b.py]
1029 from typing import Tuple
1030 x: Tuple[int, ...]
1031 y: tuple
1032 [builtins fixtures/tuple.pyi]
1033 [out2]
1034 tmp/a.py:2: note: Revealed type is "builtins.tuple[builtins.int, ...]"
1035 tmp/a.py:3: note: Revealed type is "builtins.tuple[Any, ...]"
1036
1037 [case testSerializeNone]
1038 import a
1039 [file a.py]
1040 import b
1041 [file a.py.2]
1042 import b
1043 reveal_type(b.x)
1044 [file b.py]
1045 x: None
1046 [out2]
1047 tmp/a.py:2: note: Revealed type is "None"
1048
1049 --
1050 -- TypedDict
1051 --
1052
1053 [case testSerializeTypedDictInMethod]
1054 from ntcrash import C
1055 reveal_type(C().a)
1056 reveal_type(C().b)
1057 reveal_type(C().c)
1058 [file ntcrash.py]
1059 from typing import TypedDict
1060 class C:
1061 def __init__(self) -> None:
1062 A = TypedDict('A', {'x': int})
1063 self.a = A(x=0)
1064 self.b = A(x=0) # type: A
1065 self.c = A
1066 [builtins fixtures/dict.pyi]
1067 [typing fixtures/typing-typeddict.pyi]
1068 [out1]
1069 main:2: note: Revealed type is "TypedDict('ntcrash.A@4', {'x': builtins.int})"
1070 main:3: note: Revealed type is "TypedDict('ntcrash.A@4', {'x': builtins.int})"
1071 main:4: note: Revealed type is "def (*, x: builtins.int) -> TypedDict('ntcrash.A@4', {'x': builtins.int})"
1072 [out2]
1073 main:2: note: Revealed type is "TypedDict('ntcrash.A@4', {'x': builtins.int})"
1074 main:3: note: Revealed type is "TypedDict('ntcrash.A@4', {'x': builtins.int})"
1075 main:4: note: Revealed type is "def (*, x: builtins.int) -> TypedDict('ntcrash.A@4', {'x': builtins.int})"
1076
1077 [case testSerializeNonTotalTypedDict]
1078 from m import d
1079 reveal_type(d)
1080 [file m.py]
1081 from typing import TypedDict
1082 D = TypedDict('D', {'x': int, 'y': str}, total=False)
1083 d: D
1084 [builtins fixtures/dict.pyi]
1085 [typing fixtures/typing-typeddict.pyi]
1086 [out1]
1087 main:2: note: Revealed type is "TypedDict('m.D', {'x'?: builtins.int, 'y'?: builtins.str})"
1088 [out2]
1089 main:2: note: Revealed type is "TypedDict('m.D', {'x'?: builtins.int, 'y'?: builtins.str})"
1090
1091 [case testSerializeClosedTotalTypedDict]
1092 from m import d
1093 reveal_type(d)
1094 [file m.py]
1095 from typing import TypedDict
1096 D = TypedDict('D', {'x': int, 'y': str}, closed=True)
1097 d: D
1098 [builtins fixtures/dict.pyi]
1099 [typing fixtures/typing-typeddict.pyi]
1100 [out1]
1101 main:2: note: Revealed type is "TypedDict('m.D', {'x': builtins.int, 'y': builtins.str}, closed=True)"
1102 [out2]
1103 main:2: note: Revealed type is "TypedDict('m.D', {'x': builtins.int, 'y': builtins.str}, closed=True)"
1104
1105 --
1106 -- Modules
1107 --
1108
1109 [case testSerializeImport]
1110 import b
1111 b.c.f()
1112 b.c.g()
1113 [file b.py]
1114 import c
1115 [file c.py]
1116 def f() -> None: pass
1117 def g(x: int) -> None: pass
1118 [out1]
1119 main:3: error: Missing positional argument "x" in call to "g"
1120 [out2]
1121 main:3: error: Missing positional argument "x" in call to "g"
1122
1123 [case testSerializeImportAs]
1124 import b
1125 b.d.f()
1126 b.d.g()
1127 [file b.py]
1128 import c as d
1129 [file c.py]
1130 def f() -> None: pass
1131 def g(x: int) -> None: pass
1132 [out1]
1133 main:3: error: Missing positional argument "x" in call to "g"
1134 [out2]
1135 main:3: error: Missing positional argument "x" in call to "g"
1136
1137 [case testSerializeFromImportedClass]
1138 import b
1139 b.A(1)
1140 reveal_type(b.A())
1141 [file b.py]
1142 from c import A
1143 [file c.py]
1144 class A: pass
1145 [out1]
1146 main:2: error: Too many arguments for "A"
1147 main:3: note: Revealed type is "c.A"
1148 [out2]
1149 main:2: error: Too many arguments for "A"
1150 main:3: note: Revealed type is "c.A"
1151
1152 [case testSerializeFromImportedClassAs]
1153 import b
1154 b.B(1)
1155 reveal_type(b.B())
1156 [file b.py]
1157 from c import A as B
1158 [file c.py]
1159 class A: pass
1160 [out1]
1161 main:2: error: Too many arguments for "A"
1162 main:3: note: Revealed type is "c.A"
1163 [out2]
1164 main:2: error: Too many arguments for "A"
1165 main:3: note: Revealed type is "c.A"
1166
1167 [case testSerializeFromImportedModule]
1168 import b
1169 b.d.f()
1170 b.d.g()
1171 [file b.py]
1172 from c import d
1173 [file c/__init__.py]
1174 [file c/d.py]
1175 def f() -> None: pass
1176 def g(x: int) -> None: pass
1177 [out1]
1178 main:3: error: Missing positional argument "x" in call to "g"
1179 [out2]
1180 main:3: error: Missing positional argument "x" in call to "g"
1181
1182 [case testSerializeQualifiedImport]
1183 import b
1184 b.c.d.f()
1185 b.c.d.g()
1186 [file b.py]
1187 import c.d
1188 [file c/__init__.py]
1189 [file c/d.py]
1190 def f() -> None: pass
1191 def g(x: int) -> None: pass
1192 [out1]
1193 main:3: error: Missing positional argument "x" in call to "g"
1194 [out2]
1195 main:3: error: Missing positional argument "x" in call to "g"
1196
1197 [case testSerializeQualifiedImportAs]
1198 import b
1199 b.e.f()
1200 b.e.g()
1201 [file b.py]
1202 import c.d as e
1203 [file c/__init__.py]
1204 [file c/d.py]
1205 def f() -> None: pass
1206 def g(x: int) -> None: pass
1207 [out1]
1208 main:3: error: Missing positional argument "x" in call to "g"
1209 [out2]
1210 main:3: error: Missing positional argument "x" in call to "g"
1211
1212 [case testSerialize__init__ModuleImport]
1213 import b
1214 b.c.f()
1215 b.c.g()
1216 a: b.c.d.A
1217 reveal_type(a)
1218 [file b.py]
1219 import c
1220 [file c/__init__.py]
1221 import d
1222 def f() -> None: pass
1223 def g(x: int) -> None: pass
1224 [file d.py]
1225 class A: pass
1226 [out1]
1227 main:3: error: Missing positional argument "x" in call to "g"
1228 main:5: note: Revealed type is "d.A"
1229 [out2]
1230 main:3: error: Missing positional argument "x" in call to "g"
1231 main:5: note: Revealed type is "d.A"
1232
1233 [case testSerializeImportInClassBody]
1234 import b
1235 b.A.c.f()
1236 b.A.c.g()
1237 [file b.py]
1238 class A:
1239 import c
1240 [file c.py]
1241 def f() -> None: pass
1242 def g(x: int) -> None: pass
1243 [out1]
1244 main:3: error: Missing positional argument "x" in call to "g"
1245 [out2]
1246 main:3: error: Missing positional argument "x" in call to "g"
1247
1248 [case testSerializeImportedTypeAlias]
1249 import b
1250 x: b.B
1251 reveal_type(x)
1252 [file b.py]
1253 from c import B
1254 [file c.py]
1255 from typing import Any
1256 class A: pass
1257 B = A
1258 [out1]
1259 main:3: note: Revealed type is "c.A"
1260 [out2]
1261 main:3: note: Revealed type is "c.A"
1262
1263 [case testSerializeStarImport]
1264 import a
1265 [file a.py]
1266 import b
1267 [file a.py.2]
1268 import b
1269 b.f(1)
1270 x: b.A
1271 reveal_type(x)
1272 [file b.py]
1273 from c import *
1274 [file c.py]
1275 def f() -> None: pass
1276 class A: pass
1277 [out2]
1278 tmp/a.py:2: error: Too many arguments for "f"
1279 tmp/a.py:4: note: Revealed type is "c.A"
1280
1281 [case testSerializeRelativeImport]
1282 import b.c
1283 b.c.f(1)
1284 [file b/__init__.py]
1285 [file b/c.py]
1286 from .d import f
1287 [file b/d.py]
1288 def f() -> None: pass
1289 [out1]
1290 main:2: error: Too many arguments for "f"
1291 [out2]
1292 main:2: error: Too many arguments for "f"
1293
1294 [case testSerializeDummyType]
1295 # flags: --no-strict-optional
1296 import a
1297 [file a.py]
1298 import b
1299 reveal_type(b.Test(None).foo)
1300 [file a.py.2]
1301 import b
1302 reveal_type(b.Test(b.Foo()).foo)
1303 [file b.py]
1304 class Foo(object): pass
1305 class Test:
1306 def __init__(self, o: Foo) -> None:
1307 self.foo = None
1308 if callable(o):
1309 self.foo = o
1310 [builtins fixtures/callable.pyi]
1311 [out1]
1312 tmp/a.py:2: note: Revealed type is "b.<callable subtype of Foo>"
1313 [out2]
1314 tmp/a.py:2: note: Revealed type is "b.<callable subtype of Foo>"
1315
1316 [case testSerializeForwardReferenceToAliasInProperty]
1317 import a
1318 [file a.py]
1319 import b
1320 [file a.py.2]
1321 import b
1322 reveal_type(b.A().p)
1323 [file b.py]
1324
1325 class A:
1326 @property
1327 def p(self) -> C: pass
1328 @p.setter
1329 def p(self, c: C) -> None: pass
1330 @p.deleter
1331 def p(self) -> None: pass
1332
1333 C = str
1334 [builtins fixtures/property.pyi]
1335 [out2]
1336 tmp/a.py:2: note: Revealed type is "builtins.str"
1337
1338 [case testSerializeForwardReferenceToImportedAliasInProperty]
1339
1340 import a
1341 [file a.py]
1342 import b
1343 [file a.py.2]
1344 import b
1345 reveal_type(b.A().p)
1346 [file b.py]
1347 class A:
1348 @property
1349 def p(self) -> C: pass
1350 @p.setter
1351 def p(self, c: C) -> None: pass
1352 @p.deleter
1353 def p(self) -> None: pass
1354
1355 from m import C
1356 [file m.py]
1357 C = str
1358 [builtins fixtures/property.pyi]
1359 [out2]
1360 tmp/a.py:2: note: Revealed type is "builtins.str"
1361
1362 [case testSerializeNestedClassStuff]
1363 # flags: --verbose
1364 import a
1365 [file a.py]
1366 import b
1367 [file a.py.2]
1368 import b
1369 #
1370 [file b.py]
1371 def foo() -> None:
1372 class Foo:
1373 class Bar: pass
1374
1375 [case testSerializeGenericClassMethod]
1376 import a
1377 [file a.py]
1378 import b
1379 from typing import TypeVar
1380
1381 T = TypeVar('T')
1382
1383 class C:
1384 @classmethod
1385 def f(cls, x: T) -> T: ...
1386
1387 x = C.f
1388 [file b.py]
1389 x = 1
1390 [file b.py.2]
1391 x = 'yes'
1392 [builtins fixtures/classmethod.pyi]
1393
1394 [case testSerializeGenericAbstractMethod]
1395 import a
1396 [file a.py]
1397 import b
1398 from typing import TypeVar
1399 from abc import abstractmethod
1400
1401 T = TypeVar('T')
1402
1403 class C:
1404 @abstractmethod
1405 def f(self, x: T) -> T: ...
1406
1407 c: C
1408 x = c.f
1409 [file b.py]
1410 x = 1
1411 [file b.py.2]
1412 x = 'yes'
1413
1414 [case testSerializeGenericNormalMethod]
1415 import a
1416 [file a.py]
1417 import b
1418 from typing import TypeVar
1419 from abc import abstractmethod
1420
1421 T = TypeVar('T')
1422
1423 class C:
1424 def f(self, x: T) -> T: ...
1425
1426 c: C
1427 x = c.f
1428 [file b.py]
1429 x = 1
1430 [file b.py.2]
1431 x = 'yes'