forked from python/mypy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck-expressions.test
1891 lines (1553 loc) · 49.4 KB
/
check-expressions.test
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
-- Test cases for simple expressions.
--
-- See also:
-- * check-functions.test contains test cases for calls.
-- * check-varargs.test contains test cases for *args.
-- * check-dynamic.test contains test cases related to 'Any' type.
-- * check-generics.test contains test cases for generic values.
-- None expression
-- ---------------
[case testNoneAsRvalue]
import typing
a = None # type: A
class A: pass
[out]
[case testNoneAsArgument]
import typing
def f(x: 'A', y: 'B') -> None: pass
f(None, None)
class A: pass
class B(A): pass
[out]
-- Simple expressions
-- ------------------
[case testIntLiteral]
a = 0
b = None # type: A
b = 1 # E: Incompatible types in assignment (expression has type "int", variable has type "A")
a = 1
class A:
pass
[case testStrLiteral]
a = ''
b = None # type: A
b = 'x' # E: Incompatible types in assignment (expression has type "str", variable has type "A")
a = 'x'
a = r"x"
a = """foo"""
class A:
pass
[case testFloatLiteral]
a = 0.0
b = None # type: A
b = 1.1 # E: Incompatible types in assignment (expression has type "float", variable has type "A")
a = 1.1
class A:
pass
[file builtins.py]
class object:
def __init__(self): pass
class type: pass
class function: pass
class float: pass
class str: pass
[case testComplexLiteral]
a = 0.0j
b = None # type: A
b = 1.1j # E: Incompatible types in assignment (expression has type "complex", variable has type "A")
a = 1.1j
class A:
pass
[file builtins.py]
class object:
def __init__(self): pass
class type: pass
class function: pass
class complex: pass
class str: pass
[case testBytesLiteral]
b, a = None, None # type: (bytes, A)
b = b'foo'
b = br"foo"
b = b'''foo'''
a = b'foo' # E: Incompatible types in assignment (expression has type "bytes", variable has type "A")
class A: pass
[file builtins.py]
class object:
def __init__(self): pass
class type: pass
class tuple: pass
class function: pass
class bytes: pass
class str: pass
[case testUnicodeLiteralInPython3]
s = None # type: str
s = u'foo'
b = None # type: bytes
b = u'foo' # E: Incompatible types in assignment (expression has type "str", variable has type "bytes")
[builtins fixtures/primitives.pyi]
-- Binary operators
-- ----------------
[case testAdd]
a, b, c = None, None, None # type: (A, B, C)
c = a + c # Fail
a = a + b # Fail
c = b + a # Fail
c = a + b
class A:
def __add__(self, x: 'B') -> 'C': pass
class B: pass
class C: pass
[out]
main:3: error: Unsupported operand types for + ("A" and "C")
main:4: error: Incompatible types in assignment (expression has type "C", variable has type "A")
main:5: error: Unsupported left operand type for + ("B")
[case testAdd]
a, b, c = None, None, None # type: (A, B, C)
c = a + c # Fail
a = a + b # Fail
c = b + a # Fail
c = a + b
class A:
def __add__(self, x: 'B') -> 'C':
pass
class B:
pass
class C:
pass
[out]
main:3: error: Unsupported operand types for + ("A" and "C")
main:4: error: Incompatible types in assignment (expression has type "C", variable has type "A")
main:5: error: Unsupported left operand type for + ("B")
[case testSub]
a, b, c = None, None, None # type: (A, B, C)
c = a - c # Fail
a = a - b # Fail
c = b - a # Fail
c = a - b
class A:
def __sub__(self, x: 'B') -> 'C':
pass
class B:
pass
class C:
pass
[out]
main:3: error: Unsupported operand types for - ("A" and "C")
main:4: error: Incompatible types in assignment (expression has type "C", variable has type "A")
main:5: error: Unsupported left operand type for - ("B")
[case testMul]
a, b, c = None, None, None # type: (A, B, C)
c = a * c # Fail
a = a * b # Fail
c = b * a # Fail
c = a * b
class A:
def __mul__(self, x: 'B') -> 'C':
pass
class B:
pass
class C:
pass
[out]
main:3: error: Unsupported operand types for * ("A" and "C")
main:4: error: Incompatible types in assignment (expression has type "C", variable has type "A")
main:5: error: Unsupported left operand type for * ("B")
[case testMatMul]
a, b, c = None, None, None # type: (A, B, C)
c = a @ c # E: Unsupported operand types for @ ("A" and "C")
a = a @ b # E: Incompatible types in assignment (expression has type "C", variable has type "A")
c = b @ a # E: Unsupported left operand type for @ ("B")
c = a @ b
class A:
def __matmul__(self, x: 'B') -> 'C':
pass
class B:
pass
class C:
pass
[case testDiv]
a, b, c = None, None, None # type: (A, B, C)
c = a / c # Fail
a = a / b # Fail
c = b / a # Fail
c = a / b
class A:
def __truediv__(self, x: 'B') -> 'C':
pass
class B:
pass
class C:
pass
[out]
main:3: error: Unsupported operand types for / ("A" and "C")
main:4: error: Incompatible types in assignment (expression has type "C", variable has type "A")
main:5: error: Unsupported left operand type for / ("B")
[case testIntDiv]
a, b, c = None, None, None # type: (A, B, C)
c = a // c # Fail
a = a // b # Fail
c = b // a # Fail
c = a // b
class A:
def __floordiv__(self, x: 'B') -> 'C':
pass
class B:
pass
class C:
pass
[out]
main:3: error: Unsupported operand types for // ("A" and "C")
main:4: error: Incompatible types in assignment (expression has type "C", variable has type "A")
main:5: error: Unsupported left operand type for // ("B")
[case testMod]
a, b, c = None, None, None # type: (A, B, C)
c = a % c # Fail
a = a % b # Fail
c = b % a # Fail
c = a % b
class A:
def __mod__(self, x: 'B') -> 'C':
pass
class B:
pass
class C:
pass
[out]
main:3: error: Unsupported operand types for % ("A" and "C")
main:4: error: Incompatible types in assignment (expression has type "C", variable has type "A")
main:5: error: Unsupported left operand type for % ("B")
[case testPow]
a, b, c = None, None, None # type: (A, B, C)
c = a ** c # Fail
a = a ** b # Fail
c = b ** a # Fail
c = a ** b
class A:
def __pow__(self, x: 'B') -> 'C':
pass
class B:
pass
class C:
pass
[out]
main:3: error: Unsupported operand types for ** ("A" and "C")
main:4: error: Incompatible types in assignment (expression has type "C", variable has type "A")
main:5: error: Unsupported left operand type for ** ("B")
[case testMiscBinaryOperators]
a, b = None, None # type: (A, B)
b = a & a # Fail
b = a | b # Fail
b = a ^ a # Fail
b = a << b # Fail
b = a >> a # Fail
b = a & b
b = a | a
b = a ^ b
b = a << a
b = a >> b
class A:
def __and__(self, x: 'B') -> 'B': pass
def __or__(self, x: 'A') -> 'B': pass
def __xor__(self, x: 'B') -> 'B': pass
def __lshift__(self, x: 'A') -> 'B': pass
def __rshift__(self, x: 'B') -> 'B': pass
class B: pass
[out]
main:3: error: Unsupported operand types for & ("A" and "A")
main:4: error: Unsupported operand types for | ("A" and "B")
main:5: error: Unsupported operand types for ^ ("A" and "A")
main:6: error: Unsupported operand types for << ("A" and "B")
main:7: error: Unsupported operand types for >> ("A" and "A")
[case testBooleanAndOr]
a, b = None, None # type: (A, bool)
b = b and b
b = b or b
b = b and a # E: Incompatible types in assignment (expression has type "Union[bool, A]", variable has type "bool")
b = a and b # E: Incompatible types in assignment (expression has type "Union[A, bool]", variable has type "bool")
b = b or a # E: Incompatible types in assignment (expression has type "Union[bool, A]", variable has type "bool")
b = a or b # E: Incompatible types in assignment (expression has type "Union[A, bool]", variable has type "bool")
class A: pass
[builtins fixtures/bool.pyi]
[case testRestrictedTypeAnd]
b = None # type: bool
i = None # type: str
j = not b and i
if j:
reveal_type(j) # E: Revealed type is 'builtins.str'
[builtins fixtures/bool.pyi]
[case testRestrictedTypeOr]
b = None # type: bool
i = None # type: str
j = b or i
if not j:
reveal_type(j) # E: Revealed type is 'builtins.str'
[builtins fixtures/bool.pyi]
[case testAndOr]
s = ""
b = bool()
reveal_type(s and b or b) # E: Revealed type is 'builtins.bool'
[builtins fixtures/bool.pyi]
[case testNonBooleanOr]
c, d, b = None, None, None # type: (C, D, bool)
c = c or c
c = c or d
c = d or c
b = c or c # E: Incompatible types in assignment (expression has type "C", variable has type "bool")
d = c or d # E: Incompatible types in assignment (expression has type "C", variable has type "D")
d = d or c # E: Incompatible types in assignment (expression has type "C", variable has type "D")
class C: pass
class D(C): pass
[builtins fixtures/bool.pyi]
[case testInOperator]
from typing import Iterator, Iterable, Any
a, b, c, d, e = None, None, None, None, None # type: (A, B, bool, D, Any)
c = c in a # Fail
a = b in a # Fail
c = a in b # Fail
c = b in d # Fail
c = b in a
c = a in d
c = e in d
c = a in e
class A:
def __contains__(self, x: 'B') -> bool: pass
class B: pass
class D(Iterable[A]):
def __iter__(self) -> Iterator[A]: pass
[builtins fixtures/bool.pyi]
[out]
main:3: error: Unsupported operand types for in ("bool" and "A")
main:4: error: Incompatible types in assignment (expression has type "bool", variable has type "A")
main:5: error: Unsupported right operand type for in ("B")
main:6: error: Unsupported operand types for in ("B" and "D")
[case testNotInOperator]
from typing import Iterator, Iterable, Any
a, b, c, d, e = None, None, None, None, None # type: (A, B, bool, D, Any)
c = c not in a # Fail
a = b not in a # Fail
c = a not in b # Fail
c = b not in d # Fail
c = b not in a
c = a not in d
c = e in d
c = a in e
class A:
def __contains__(self, x: 'B') -> bool: pass
class B: pass
class D(Iterable[A]):
def __iter__(self) -> Iterator[A]: pass
[builtins fixtures/bool.pyi]
[out]
main:3: error: Unsupported operand types for in ("bool" and "A")
main:4: error: Incompatible types in assignment (expression has type "bool", variable has type "A")
main:5: error: Unsupported right operand type for in ("B")
main:6: error: Unsupported operand types for in ("B" and "D")
[case testNonBooleanContainsReturnValue]
a, b = None, None # type: (A, bool)
b = a not in a
b = a in a
class A:
def __contains__(self, x: 'A') -> object: pass
[builtins fixtures/bool.pyi]
[out]
main:4: error: Incompatible types in assignment (expression has type "object", variable has type "bool")
[case testEq]
a, b = None, None # type: (A, bool)
a = a == b # Fail
a = a != b # Fail
b = a == b
b = a != b
class A:
def __eq__(self, o: object) -> bool: pass
def __ne__(self, o: object) -> bool: pass
[builtins fixtures/bool.pyi]
[out]
main:3: error: Incompatible types in assignment (expression has type "bool", variable has type "A")
main:4: error: Incompatible types in assignment (expression has type "bool", variable has type "A")
[case testLtAndGt]
a, b, bo = None, None, None # type: (A, B, bool)
a = a < b # Fail
a = a > b # Fail
bo = a < b
bo = a > b
class A:
def __lt__(self, o: 'B') -> bool: pass
def __gt__(self, o: 'B') -> bool: pass
class B:
def __lt__(self, o: 'B') -> bool: pass
def __gt__(self, o: 'B') -> bool: pass
[builtins fixtures/bool.pyi]
[out]
main:3: error: Incompatible types in assignment (expression has type "bool", variable has type "A")
main:4: error: Incompatible types in assignment (expression has type "bool", variable has type "A")
[case testCmp_python2]
a, b, c, bo = None, None, None, None # type: (A, B, C, bool)
bo = a == a # E: Unsupported operand types for == ("A" and "A")
bo = a != a # E: Argument 1 to "__cmp__" of "A" has incompatible type "A"; expected "B"
bo = a < b
bo = a > b
bo = b <= b
bo = b <= c
bo = b >= c # E: Argument 1 to "__cmp__" of "B" has incompatible type "C"; expected "B"
bo = a >= b
bo = c >= b
bo = c <= b # E: Argument 1 to "__cmp__" of "C" has incompatible type "B"; expected "A"
bo = a == c
bo = b == c # E: Unsupported operand types for == ("C" and "B")
class A:
def __cmp__(self, o):
# type: ('B') -> bool
pass
def __eq__(self, o):
# type: ('int') -> bool
pass
class B:
def __cmp__(self, o):
# type: ('B') -> bool
pass
def __le__(self, o):
# type: ('C') -> bool
pass
class C:
def __cmp__(self, o):
# type: ('A') -> bool
pass
def __eq__(self, o):
# type: ('int') -> bool
pass
[builtins_py2 fixtures/bool.pyi]
[case cmpIgnoredPy3]
a, b, bo = None, None, None # type: (A, B, bool)
bo = a <= b # E: Unsupported left operand type for <= ("A")
class A:
def __cmp__(self, o: 'B') -> bool: pass
class B:
pass
[builtins fixtures/bool.pyi]
[case testLeAndGe]
a, b, bo = None, None, None # type: (A, B, bool)
a = a <= b # Fail
a = a >= b # Fail
bo = a <= b
bo = a >= b
class A:
def __le__(self, o: 'B') -> bool: pass
def __ge__(self, o: 'B') -> bool: pass
class B:
def __le__(self, o: 'B') -> bool: pass
def __ge__(self, o: 'B') -> bool: pass
[builtins fixtures/bool.pyi]
[out]
main:3: error: Incompatible types in assignment (expression has type "bool", variable has type "A")
main:4: error: Incompatible types in assignment (expression has type "bool", variable has type "A")
[case testChainedComp]
a, b, bo = None, None, None # type: (A, B, bool)
a < a < b < b # Fail
a < b < b < b
a < a > a < b # Fail
class A:
def __lt__(self, o: 'B') -> bool: pass
def __gt__(self, o: 'B') -> bool: pass
class B:
def __lt__(self, o: 'B') -> bool: pass
def __gt__(self, o: 'B') -> bool: pass
[builtins fixtures/bool.pyi]
[out]
main:3: error: Unsupported operand types for < ("A" and "A")
main:5: error: Unsupported operand types for < ("A" and "A")
main:5: error: Unsupported operand types for > ("A" and "A")
[case testChainedCompBoolRes]
a, b, bo = None, None, None # type: (A, B, bool)
bo = a < b < b
a = a < b < b # Fail
class A:
def __lt__(self, o: 'B') -> bool: pass
def __gt__(self, o: 'B') -> bool: pass
class B:
def __lt__(self, o: 'B') -> bool: pass
def __gt__(self, o: 'B') -> bool: pass
[builtins fixtures/bool.pyi]
[out]
main:4: error: Incompatible types in assignment (expression has type "bool", variable has type "A")
[case testChainedCompResTyp]
x, y = None, None # type: (X, Y)
a, b, p, bo = None, None, None, None # type: (A, B, P, bool)
b = y == y == y
bo = y == y == y # Fail
a = x < y
a = x < y == y # Fail
p = x < y == y
class P:
pass
class A(P):
pass
class B(P):
pass
class X:
def __lt__(self, o: 'Y') -> A: pass
def __gt__(self, o: 'Y') -> A: pass
class Y:
def __lt__(self, o: 'Y') -> A: pass
def __gt__(self, o: 'Y') -> A: pass
def __eq__(self, o: 'Y') -> B: pass
[builtins fixtures/bool.pyi]
[out]
main:5: error: Incompatible types in assignment (expression has type "B", variable has type "bool")
main:7: error: Incompatible types in assignment (expression has type "P", variable has type "A")
[case testIs]
a, b = None, None # type: (A, bool)
a = a is b # Fail
b = a is b
b = b is a
b = a is None
class A: pass
[builtins fixtures/bool.pyi]
[out]
main:3: error: Incompatible types in assignment (expression has type "bool", variable has type "A")
[case testIsNot]
a, b = None, None # type: (A, bool)
a = a is not b # Fail
b = a is not b
b = b is not a
b = a is not None
class A: pass
[builtins fixtures/bool.pyi]
[out]
main:3: error: Incompatible types in assignment (expression has type "bool", variable has type "A")
[case testIsRightOperand]
1 is 1()
[builtins fixtures/bool.pyi]
[out]
main:2: error: "int" not callable
[case testReverseBinaryOperator]
class A:
def __add__(self, x: int) -> int: pass
class B:
def __radd__(self, x: A) -> str: pass
s = None # type: str
n = None # type: int
n = A() + 1
s = A() + B()
n = A() + B() # E: Incompatible types in assignment (expression has type "str", variable has type "int")
[case testReverseBinaryOperator2]
class A:
def __add__(self, x: 'A') -> object: pass
class B:
def __radd__(self, x: A) -> str: pass
s = None # type: str
n = None # type: int
s = A() + B()
n = A() + B() # E: Incompatible types in assignment (expression has type "str", variable has type "int")
[case testReverseBinaryOperator3]
class N:
def __add__(self, x: 'N') -> object: pass
class A:
def __add__(self, x: N) -> int: pass
class B:
def __radd__(self, x: N) -> str: pass
s = None # type: str
s = A() + B() # E: Unsupported operand types for + ("A" and "B")
[case testBinaryOperatorWithAnyRightOperand]
from typing import Any, cast
class A: pass
A() + cast(Any, 1)
[case testReverseComparisonOperator]
class C:
def __gt__(self, x: 'A') -> object: pass
class A:
def __lt__(self, x: C) -> int: pass # E: Signatures of "__lt__" of "A" and "__gt__" of "C" are unsafely overlapping
class B:
def __gt__(self, x: A) -> str: pass
s = None # type: str
n = None # type: int
n = A() < C()
s = A() < B()
n = A() < B() # E: Incompatible types in assignment (expression has type "str", variable has type "int")
s = object() < B() # E: Unsupported operand types for > ("B" and "object")
[case testReversibleComparisonWithExtraArgument]
class C:
def __lt__(self, o: object, x: str = "") -> int: ...
[case testErrorContextAndBinaryOperators]
import typing
class A:
def __getitem__(self, i: str) -> int: pass
def f() -> None:
A()[1] # Error
class B:
A()[1] # Error
A()[1] # Error
[out]
main:5: error: Invalid index type "int" for "A"; expected type "str"
main:7: error: Invalid index type "int" for "A"; expected type "str"
main:8: error: Invalid index type "int" for "A"; expected type "str"
[case testErrorContextAndBinaryOperators2]
import m
[file m.py]
import typing
class A:
def __getitem__(self, i: str) -> int: pass
def f() -> None:
A()[1] # Error
class B:
A()[1] # Error
A()[1] # Error
[out]
tmp/m.py:5: error: Invalid index type "int" for "A"; expected type "str"
tmp/m.py:7: error: Invalid index type "int" for "A"; expected type "str"
tmp/m.py:8: error: Invalid index type "int" for "A"; expected type "str"
[case testDivmod]
from typing import Tuple, Union, SupportsInt
_Decimal = Union[Decimal, int]
class Decimal(SupportsInt):
def __init__(self, int) -> None: ...
def __divmod__(self, other: _Decimal) -> Tuple[Decimal, Decimal]: ...
def __rdivmod__(self, other: _Decimal) -> Tuple[Decimal, Decimal]: ...
i = 8
f = 8.0
d = Decimal(8)
reveal_type(divmod(i, i)) # E: Revealed type is 'Tuple[builtins.int, builtins.int]'
reveal_type(divmod(f, i)) # E: Revealed type is 'Tuple[builtins.float, builtins.float]'
reveal_type(divmod(d, i)) # E: Revealed type is 'Tuple[__main__.Decimal, __main__.Decimal]'
reveal_type(divmod(i, f)) # E: Revealed type is 'Tuple[builtins.float, builtins.float]'
reveal_type(divmod(f, f)) # E: Revealed type is 'Tuple[builtins.float, builtins.float]'
divmod(d, f) # E: Unsupported operand types for divmod ("Decimal" and "float")
reveal_type(divmod(i, d)) # E: Revealed type is 'Tuple[__main__.Decimal, __main__.Decimal]'
divmod(f, d) # E: Unsupported operand types for divmod ("float" and "Decimal")
reveal_type(divmod(d, d)) # E: Revealed type is 'Tuple[__main__.Decimal, __main__.Decimal]'
# Now some bad calls
divmod() # E: 'divmod' expects 2 arguments \
# E: Too few arguments for "divmod"
divmod(7) # E: 'divmod' expects 2 arguments \
# E: Too few arguments for "divmod"
divmod(7, 8, 9) # E: 'divmod' expects 2 arguments \
# E: Too many arguments for "divmod"
divmod(_x=7, _y=9) # E: 'divmod' must be called with 2 positional arguments
divmod('foo', 'foo') # E: Unsupported left operand type for divmod ("str")
divmod(i, 'foo') # E: Unsupported operand types for divmod ("int" and "str")
divmod(f, 'foo') # E: Unsupported operand types for divmod ("float" and "str")
divmod(d, 'foo') # E: Unsupported operand types for divmod ("Decimal" and "str")
divmod('foo', i) # E: Unsupported operand types for divmod ("str" and "int")
divmod('foo', f) # E: Unsupported operand types for divmod ("str" and "float")
divmod('foo', d) # E: Unsupported operand types for divmod ("str" and "Decimal")
[builtins fixtures/divmod.pyi]
[typing fixtures/typing-full.pyi]
-- Unary operators
-- ---------------
[case testUnaryMinus]
a, b = None, None # type: (A, B)
a = -a # Fail
b = -b # Fail
b = -a
class A:
def __neg__(self) -> 'B':
pass
class B:
pass
[out]
main:3: error: Incompatible types in assignment (expression has type "B", variable has type "A")
main:4: error: Unsupported operand type for unary - ("B")
[case testUnaryPlus]
a, b = None, None # type: (A, B)
a = +a # Fail
b = +b # Fail
b = +a
class A:
def __pos__(self) -> 'B':
pass
class B:
pass
[out]
main:3: error: Incompatible types in assignment (expression has type "B", variable has type "A")
main:4: error: Unsupported operand type for unary + ("B")
[case testUnaryNot]
a, b = None, None # type: (A, bool)
a = not b # Fail
b = not a
b = not b
class A:
pass
[builtins fixtures/bool.pyi]
[out]
main:3: error: Incompatible types in assignment (expression has type "bool", variable has type "A")
[case testUnaryBitwiseNeg]
a, b = None, None # type: (A, B)
a = ~a # Fail
b = ~b # Fail
b = ~a
class A:
def __invert__(self) -> 'B':
pass
class B:
pass
[out]
main:3: error: Incompatible types in assignment (expression has type "B", variable has type "A")
main:4: error: Unsupported operand type for ~ ("B")
-- Indexing
-- --------
[case testIndexing]
a, b, c = None, None, None # type: (A, B, C)
c = a[c] # Fail
a = a[b] # Fail
c = b[a] # Fail
c = a[b]
class A:
def __getitem__(self, x: 'B') -> 'C':
pass
class B: pass
class C: pass
[out]
main:3: error: Invalid index type "C" for "A"; expected type "B"
main:4: error: Incompatible types in assignment (expression has type "C", variable has type "A")
main:5: error: Value of type "B" is not indexable
[case testIndexingAsLvalue]
a, b, c = None, None, None # type: (A, B, C)
a[c] = c # Fail
a[b] = a # Fail
b[a] = c # Fail
a[b] = c
class A:
def __setitem__(self, x: 'B', y: 'C') -> None:
pass
class B:
pass
class C:
pass
[out]
main:3: error: Invalid index type "C" for "A"; expected type "B"
main:4: error: Incompatible types in assignment (expression has type "A", target has type "C")
main:5: error: Unsupported target for indexed assignment
[case testOverloadedIndexing]
from foo import *
[file foo.pyi]
from typing import overload
a, b, c = None, None, None # type: (A, B, C)
a[b]
a[c]
a[1] # E: No overload variant of "__getitem__" of "A" matches argument type "int" \
# N: Possible overload variants: \
# N: def __getitem__(self, B) -> int \
# N: def __getitem__(self, C) -> str
i, s = None, None # type: (int, str)
i = a[b]
s = a[b] # E: Incompatible types in assignment (expression has type "int", variable has type "str")
i = a[c] # E: Incompatible types in assignment (expression has type "str", variable has type "int")
s = a[c]
class A:
@overload
def __getitem__(self, x: 'B') -> int:
pass
@overload
def __getitem__(self, x: 'C') -> str:
pass
class B: pass
class C: pass
[out]
-- Cast expression
-- ---------------
[case testCastExpressions]
from typing import cast, Any
class A: pass
class B: pass
class C(A): pass
a, b, c = None, None, None # type: (A, B, C)
a = cast(A, a()) # E: "A" not callable
a = cast(Any, a()) # E: "A" not callable
b = cast(A, a) # E: Incompatible types in assignment (expression has type "A", variable has type "B")
a = cast(A, b)
a = cast(A, a)
c = cast(C, a)
a = cast(A, c)
a = cast(Any, b)
b = cast(Any, a)
[out]
[case testAnyCast]
from typing import cast, Any
a, b = None, None # type: (A, B)
a = cast(Any, a()) # Fail
a = cast(Any, b)
b = cast(Any, a)
class A: pass
class B: pass
[out]
main:3: error: "A" not callable
-- None return type
-- ----------------
[case testNoneReturnTypeBasics]
a, o = None, None # type: (A, object)
a = f() # E: "f" does not return a value
o = a() # E: Function does not return a value
o = A().g(a) # E: "g" of "A" does not return a value
o = A.g(a, a) # E: "g" of "A" does not return a value
A().g(f()) # E: "f" does not return a value
x: A = f() # E: "f" does not return a value
f()
A().g(a)
def f() -> None:
pass
class A:
def g(self, x: object) -> None:
pass
def __call__(self) -> None:
pass
[case testNoneReturnTypeWithStatements]
import typing
if f(): # Fail
pass
elif f(): # Fail
pass
while f(): # Fail
pass
def g() -> object:
return f() # Fail
raise f() # Fail
def f() -> None: pass
[builtins fixtures/exception.pyi]
[out]
main:2: error: "f" does not return a value
main:4: error: "f" does not return a value
main:6: error: "f" does not return a value
main:9: error: "f" does not return a value
main:10: error: "f" does not return a value
[case testNoneReturnTypeWithExpressions]
from typing import cast
a = None # type: A
[f()] # E: "f" does not return a value
f() + a # E: "f" does not return a value
a + f() # E: "f" does not return a value
f() == a # E: "f" does not return a value
a != f() # E: "f" does not return a value
cast(A, f())
f().foo # E: "f" does not return a value
def f() -> None: pass
class A:
def __add__(self, x: 'A') -> 'A': pass
[builtins fixtures/list.pyi]
[case testNoneReturnTypeWithExpressions2]
import typing
a, b = None, None # type: (A, bool)
f() in a # E: "f" does not return a value # E: Unsupported right operand type for in ("A")
a < f() # E: "f" does not return a value
f() <= a # E: "f" does not return a value
a in f() # E: "f" does not return a value