/
check-expressions.test
2325 lines (1923 loc) · 59.5 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
if int():
b = 1 # E: Incompatible types in assignment (expression has type "int", variable has type "A")
if int():
a = 1
class A:
pass
[case testStrLiteral]
a = ''
b = None # type: A
if int():
b = 'x' # E: Incompatible types in assignment (expression has type "str", variable has type "A")
if int():
a = 'x'
if int():
a = r"x"
if int():
a = """foo"""
class A:
pass
[case testFloatLiteral]
a = 0.0
b = None # type: A
if str():
b = 1.1 # E: Incompatible types in assignment (expression has type "float", variable has type "A")
if str():
a = 1.1
class A:
pass
[builtins fixtures/dict.pyi]
[case testComplexLiteral]
a = 0.0j
b = None # type: A
if str():
b = 1.1j # E: Incompatible types in assignment (expression has type "complex", variable has type "A")
if str():
a = 1.1j
class A:
pass
[builtins fixtures/dict.pyi]
[case testBytesLiteral]
b, a = None, None # type: (bytes, A)
if str():
b = b'foo'
if str():
b = br"foo"
if str():
b = b'''foo'''
if str():
a = b'foo' # E: Incompatible types in assignment (expression has type "bytes", variable has type "A")
class A: pass
[builtins fixtures/dict.pyi]
[case testUnicodeLiteralInPython3]
s = None # type: str
if int():
s = u'foo'
b = None # type: bytes
if int():
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)
if int():
c = a + c # E: Unsupported operand types for + ("A" and "C")
if int():
a = a + b # E: Incompatible types in assignment (expression has type "C", variable has type "A")
if int():
c = b + a # E: Unsupported left operand type for + ("B")
if int():
c = a + b
class A:
def __add__(self, x: 'B') -> 'C':
pass
class B:
pass
class C:
pass
[builtins fixtures/tuple.pyi]
[case testSub]
a, b, c = None, None, None # type: (A, B, C)
if int():
c = a - c # E: Unsupported operand types for - ("A" and "C")
if int():
a = a - b # E: Incompatible types in assignment (expression has type "C", variable has type "A")
if int():
c = b - a # E: Unsupported left operand type for - ("B")
if int():
c = a - b
class A:
def __sub__(self, x: 'B') -> 'C':
pass
class B:
pass
class C:
pass
[builtins fixtures/tuple.pyi]
[case testMul]
a, b, c = None, None, None # type: (A, B, C)
if int():
c = a * c # E: Unsupported operand types for * ("A" and "C")
if int():
a = a * b # E: Incompatible types in assignment (expression has type "C", variable has type "A")
if int():
c = b * a # E: Unsupported left operand type for * ("B")
if int():
c = a * b
class A:
def __mul__(self, x: 'B') -> 'C':
pass
class B:
pass
class C:
pass
[builtins fixtures/tuple.pyi]
[case testMatMul]
a, b, c = None, None, None # type: (A, B, C)
if int():
c = a @ c # E: Unsupported operand types for @ ("A" and "C")
if int():
a = a @ b # E: Incompatible types in assignment (expression has type "C", variable has type "A")
if int():
c = b @ a # E: Unsupported left operand type for @ ("B")
if int():
c = a @ b
class A:
def __matmul__(self, x: 'B') -> 'C':
pass
class B:
pass
class C:
pass
[builtins fixtures/tuple.pyi]
[case testDiv]
a, b, c = None, None, None # type: (A, B, C)
if int():
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")
if int():
c = b / a # E: Unsupported left operand type for / ("B")
if int():
c = a / b
class A:
def __truediv__(self, x: 'B') -> 'C':
pass
class B:
pass
class C:
pass
[builtins fixtures/tuple.pyi]
[case testDivPython2]
# flags: --python-version 2.7
class A(object):
def __div__(self, other):
# type: (A, str) -> str
return 'a'
a = A()
reveal_type(a / 'b') # N: Revealed type is "builtins.str"
a / 1 # E: Unsupported operand types for / ("A" and "int")
[builtins fixtures/bool.pyi]
[case testDivPython2FutureImport]
# flags: --python-version 2.7
from __future__ import division
class A(object):
def __truediv__(self, other):
# type: (A, str) -> str
return 'a'
a = A()
reveal_type(a / 'b') # N: Revealed type is "builtins.str"
a / 1 # E: Unsupported operand types for / ("A" and "int")
[builtins fixtures/bool.pyi]
[case testDivPython2FutureImportNotLeaking]
# flags: --python-version 2.7
import m1
[file m1.py]
import m2
class A(object):
def __div__(self, other):
# type: (A, str) -> str
return 'a'
a = A()
reveal_type(a / 'b') # N: Revealed type is "builtins.str"
a / 1 # E: Unsupported operand types for / ("A" and "int")
[file m2.py]
from __future__ import division
[builtins fixtures/bool.pyi]
[case testDivPython2FutureImportNotLeaking2]
# flags: --python-version 2.7
import m1
[file m1.py]
import m2
class A(object):
def __div__(self, other):
# type: (A, str) -> str
return 'a'
a = A()
reveal_type(a / 'b') # N: Revealed type is "builtins.str"
a / 1 # E: Unsupported operand types for / ("A" and "int")
[file m2.py]
# empty
[builtins fixtures/bool.pyi]
[case testIntDiv]
a, b, c = None, None, None # type: (A, B, C)
if int():
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")
if int():
c = b // a # E: Unsupported left operand type for // ("B")
if int():
c = a // b
class A:
def __floordiv__(self, x: 'B') -> 'C':
pass
class B:
pass
class C:
pass
[builtins fixtures/tuple.pyi]
[case testMod]
a, b, c = None, None, None # type: (A, B, C)
if int():
c = a % c # E: Unsupported operand types for % ("A" and "C")
if int():
a = a % b # E: Incompatible types in assignment (expression has type "C", variable has type "A")
if int():
c = b % a # E: Unsupported left operand type for % ("B")
if int():
c = a % b
class A:
def __mod__(self, x: 'B') -> 'C':
pass
class B:
pass
class C:
pass
[builtins fixtures/tuple.pyi]
[case testPow]
a, b, c = None, None, None # type: (A, B, C)
if int():
c = a ** c # E: Unsupported operand types for ** ("A" and "C")
if int():
a = a ** b # E: Incompatible types in assignment (expression has type "C", variable has type "A")
if int():
c = b ** a # E: Unsupported left operand type for ** ("B")
if int():
c = a ** b
class A:
def __pow__(self, x: 'B') -> 'C':
pass
class B:
pass
class C:
pass
[builtins fixtures/tuple.pyi]
[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
[builtins fixtures/tuple.pyi]
[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)
if int():
b = b and b
if int():
b = b or b
if int():
b = b and a # E: Incompatible types in assignment (expression has type "Union[Literal[False], A]", variable has type "bool")
if int():
b = a and b # E: Incompatible types in assignment (expression has type "Union[A, bool]", variable has type "bool")
if int():
b = b or a # E: Incompatible types in assignment (expression has type "Union[Literal[True], A]", variable has type "bool")
if int():
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) # N: 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) # N: Revealed type is "builtins.str"
[builtins fixtures/bool.pyi]
[case testAndOr]
s = ""
b = bool()
reveal_type(s and b or b) # N: Revealed type is "builtins.bool"
[builtins fixtures/bool.pyi]
[case testRestrictedBoolAndOrWithGenerics]
from typing import List
def f(a: List[str], b: bool) -> bool:
x = a and b
y: bool
return reveal_type(x or y) # N: Revealed type is "builtins.bool"
[builtins fixtures/list.pyi]
[case testNonBooleanOr]
c, d, b = None, None, None # type: (C, D, bool)
if int():
c = c or c
if int():
c = c or d
if int():
c = d or c
if int():
b = c or c # E: Incompatible types in assignment (expression has type "C", variable has type "bool")
if int():
d = c or d # E: Incompatible types in assignment (expression has type "C", variable has type "D")
if int():
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)
if int():
c = c in a # E: Unsupported operand types for in ("bool" and "A")
if int():
a = b in a # E: Incompatible types in assignment (expression has type "bool", variable has type "A")
if int():
c = a in b # E: Unsupported right operand type for in ("B")
if int():
c = b in d # E: Unsupported operand types for in ("B" and "D")
if int():
c = b in a
if int():
c = a in d
if int():
c = e in d
if int():
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]
[case testNotInOperator]
from typing import Iterator, Iterable, Any
a, b, c, d, e = None, None, None, None, None # type: (A, B, bool, D, Any)
if int():
c = c not in a # E: Unsupported operand types for in ("bool" and "A")
if int():
a = b not in a # E: Incompatible types in assignment (expression has type "bool", variable has type "A")
if int():
c = a not in b # E: Unsupported right operand type for in ("B")
if int():
c = b not in d # E: Unsupported operand types for in ("B" and "D")
if int():
c = b not in a
if int():
c = a not in d
if int():
c = e in d
if int():
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]
[case testNonBooleanContainsReturnValue]
a, b, c = None, None, None # type: (A, bool, str)
if int():
b = a not in a
if int():
b = a in a
if int():
c = a not in a # E: Incompatible types in assignment (expression has type "bool", variable has type "str")
if int():
c = a in a # E: Incompatible types in assignment (expression has type "bool", variable has type "str")
class A:
def __contains__(self, x: 'A') -> str: pass
[builtins fixtures/bool.pyi]
[case testInWithInvalidArgs]
a = 1 in ([1] + ['x']) # E: List item 0 has incompatible type "str"; expected "int"
[builtins fixtures/list.pyi]
[case testEq]
a, b = None, None # type: (A, bool)
if int():
a = a == b # E: Incompatible types in assignment (expression has type "bool", variable has type "A")
if int():
a = a != b # E: Incompatible types in assignment (expression has type "bool", variable has type "A")
if int():
b = a == b
if int():
b = a != b
class A:
def __eq__(self, o: object) -> bool: pass
def __ne__(self, o: object) -> bool: pass
[builtins fixtures/bool.pyi]
[case testLtAndGt]
a, b, bo = None, None, None # type: (A, B, bool)
if int():
a = a < b # E: Incompatible types in assignment (expression has type "bool", variable has type "A")
if int():
a = a > b # E: Incompatible types in assignment (expression has type "bool", variable has type "A")
if int():
bo = a < b
if int():
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]
[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: Unsupported operand types for comparison ("A" and "A")
bo = a < b
bo = a > b
bo = b <= b
bo = b <= c
bo = b >= c # E: Unsupported operand types for comparison ("C" and "B")
bo = a >= b
bo = c >= b
bo = c <= b # E: Unsupported operand types for comparison ("B" and "C")
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_py2.pyi]
[case testDiv_python2]
10 / 'no' # E: Unsupported operand types for / ("int" and "str")
'no' / 10 # E: Unsupported operand types for / ("str" and "int")
[builtins_py2 fixtures/ops.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)
if int():
a = a <= b # E: Incompatible types in assignment (expression has type "bool", variable has type "A")
if int():
a = a >= b # E: Incompatible types in assignment (expression has type "bool", variable has type "A")
if int():
bo = a <= b
if int():
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]
[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)
if int():
bo = a < b < b
if int():
a = a < b < b # E: Incompatible types in assignment (expression has type "bool", variable has type "A")
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]
[case testChainedCompResTyp]
x, y = None, None # type: (X, Y)
a, b, p, bo = None, None, None, None # type: (A, B, P, bool)
if int():
b = y == y == y
if int():
bo = y == y == y # E: Incompatible types in assignment (expression has type "B", variable has type "bool")
if int():
a = x < y
if int():
a = x < y == y # E: Incompatible types in assignment (expression has type "P", variable has type "A")
if int():
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 # type: ignore
[builtins fixtures/bool.pyi]
[case testIs]
a, b = None, None # type: (A, bool)
if int():
a = a is b # E: Incompatible types in assignment (expression has type "bool", variable has type "A")
if int():
b = a is b
if int():
b = b is a
if int():
b = a is None
class A: pass
[builtins fixtures/bool.pyi]
[case testIsNot]
a, b = None, None # type: (A, bool)
if int():
a = a is not b # E: Incompatible types in assignment (expression has type "bool", variable has type "A")
if int():
b = a is not b
if int():
b = b is not a
if int():
b = a is not None
class A: pass
[builtins fixtures/bool.pyi]
[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
if int():
n = A() + 1
if int():
s = A() + B()
if int():
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
if 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
if int():
n = A() < C()
s = A() < B()
if int():
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)) # N: Revealed type is "Tuple[builtins.int, builtins.int]"
reveal_type(divmod(f, i)) # N: Revealed type is "Tuple[builtins.float, builtins.float]"
reveal_type(divmod(d, i)) # N: Revealed type is "Tuple[__main__.Decimal, __main__.Decimal]"
reveal_type(divmod(i, f)) # N: Revealed type is "Tuple[builtins.float, builtins.float]"
reveal_type(divmod(f, f)) # N: 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)) # N: 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)) # N: Revealed type is "Tuple[__main__.Decimal, __main__.Decimal]"
# Now some bad calls
divmod() # E: "divmod" expects 2 arguments \
# E: Missing positional arguments "_x", "_y" in call to "divmod"
divmod(7) # E: "divmod" expects 2 arguments \
# E: Missing positional argument "_y" in call to "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-medium.pyi]
-- Unary operators
-- ---------------
[case testUnaryMinus]
a, b = None, None # type: (A, B)
if int():
a = -a # E: Incompatible types in assignment (expression has type "B", variable has type "A")
if int():
b = -b # E: Unsupported operand type for unary - ("B")
if int():
b = -a
class A:
def __neg__(self) -> 'B':
pass
class B:
pass
[builtins fixtures/tuple.pyi]
[case testUnaryPlus]
a, b = None, None # type: (A, B)
if int():
a = +a # E: Incompatible types in assignment (expression has type "B", variable has type "A")
if int():
b = +b # E: Unsupported operand type for unary + ("B")
if int():
b = +a
class A:
def __pos__(self) -> 'B':
pass
class B:
pass
[builtins fixtures/tuple.pyi]
[case testUnaryNot]
a, b = None, None # type: (A, bool)
if int():
a = not b # E: Incompatible types in assignment (expression has type "bool", variable has type "A")
if int():
b = not a
if int():
b = not b
class A:
pass
[builtins fixtures/bool.pyi]
[case testUnaryBitwiseNeg]
a, b = None, None # type: (A, B)
if int():
a = ~a # E: Incompatible types in assignment (expression has type "B", variable has type "A")
if int():
b = ~b # E: Unsupported operand type for ~ ("B")
if int():
b = ~a
class A:
def __invert__(self) -> 'B':
pass
class B:
pass
-- Indexing
-- --------
[builtins fixtures/tuple.pyi]
[case testIndexing]
a, b, c = None, None, None # type: (A, B, C)
if int():
c = a[c] # E: Invalid index type "C" for "A"; expected type "B"
if int():
a = a[b] # E: Incompatible types in assignment (expression has type "C", variable has type "A")
if int():
c = b[a] # E: Value of type "B" is not indexable
if int():
c = a[b]
class A:
def __getitem__(self, x: 'B') -> 'C':
pass
class B: pass
class C: pass
[builtins fixtures/tuple.pyi]
[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
[builtins fixtures/tuple.pyi]
[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 ("B")
[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)
if int():
i = a[b]
if int():
s = a[b] # E: Incompatible types in assignment (expression has type "int", variable has type "str")
if int():
i = a[c] # E: Incompatible types in assignment (expression has type "str", variable has type "int")
if 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
[builtins fixtures/tuple.pyi]
[out]
-- Cast expression
-- ---------------