forked from python/mypy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.test
More file actions
4683 lines (3988 loc) · 95.3 KB
/
run.test
File metadata and controls
4683 lines (3988 loc) · 95.3 KB
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
[case testCallTrivialFunction]
def f(x: int) -> int:
return x
[file driver.py]
from native import f
print(f(3))
print(f(-157))
print(f(10**20))
print(f(-10**20))
[out]
3
-157
100000000000000000000
-100000000000000000000
[case testInc]
def inc(x: int) -> int:
return x + 1
[file driver.py]
from native import inc
print(inc(3))
print(inc(-5))
print(inc(10**20))
[out]
4
-4
100000000000000000001
[case testCount]
def count(n: int) -> int:
i = 1
while i <= n:
i = i + 1
return i
[file driver.py]
from native import count
print(count(0))
print(count(1))
print(count(5))
[out]
1
2
6
[case testFor]
from typing import List
def count(n: int) -> None:
for i in range(n):
print(i)
def count_between(n: int, k: int) -> None:
for i in range(n, k):
print(i)
def count_down(n: int, k: int) -> None:
for i in range(n, k, -1):
print(i)
def count_double(n: int, k: int) -> None:
for i in range(n, k, 2):
print(i)
def list_iter(l: List[int]) -> None:
for i in l:
print(i)
def list_rev_iter(l: List[int]) -> None:
for i in reversed(l):
print(i)
def list_rev_iter_lol(l: List[int]) -> None:
for i in reversed(l):
print(i)
if i == 3:
while l:
l.pop()
def count_down_short() -> None:
for i in range(10, 0, -1):
print(i)
[file driver.py]
from native import (
count, list_iter, list_rev_iter, list_rev_iter_lol, count_between, count_down, count_double,
count_down_short
)
count(5)
list_iter(list(reversed(range(5))))
list_rev_iter(list(reversed(range(5))))
count_between(11, 15)
count_between(10**20, 10**20+3)
count_down(20, 10)
count_double(10, 15)
count_down_short()
print('==')
list_rev_iter_lol(list(reversed(range(5))))
[out]
0
1
2
3
4
4
3
2
1
0
0
1
2
3
4
11
12
13
14
100000000000000000000
100000000000000000001
100000000000000000002
20
19
18
17
16
15
14
13
12
11
10
12
14
10
9
8
7
6
5
4
3
2
1
==
0
1
2
3
[case testLoopElse]
from typing import Iterator
def run_for_range(n: int) -> None:
for i in range(n):
if i == 3:
break
print(i)
else:
print(n+1)
def run_for_list(n: int) -> None:
for i in list(range(n)):
if i == 3:
break
print(i)
else:
print(n+1)
def run_for_iter(n: int) -> None:
def identity(x: Iterator[int]) -> Iterator[int]:
return x
for i in identity(range(n)):
if i == 3:
break
print(i)
else:
print(n+1)
def count(n: int) -> int:
i = 1
while i <= n:
i = i + 1
if i == 5:
break
else:
i *= -1
return i
def nested_while() -> int:
while True:
while False:
pass
else:
break
else:
return -1
return 0
def nested_for() -> int:
for x in range(1000):
for y in [1,2,3]:
pass
else:
break
else:
return -1
return 0
[file driver.py]
from native import run_for_range, run_for_list, run_for_iter, count, nested_while, nested_for
assert nested_while() == 0
assert nested_for() == 0
assert count(0) == -1
assert count(1) == -2
assert count(5) == 5
assert count(6) == 5
run_for_range(3)
run_for_range(5)
print('==')
run_for_list(3)
run_for_list(5)
print('==')
run_for_iter(3)
run_for_iter(5)
[out]
0
1
2
4
0
1
2
==
0
1
2
4
0
1
2
==
0
1
2
4
0
1
2
[case testNestedLoopSameIdx]
from typing import List, Generator
def nested_enumerate() -> None:
l1 = [0,1,2]
l2 = [0,1,2]
outer_seen = []
outer = 0
for i, j in enumerate(l1):
assert i == outer
outer_seen.append(i)
inner = 0
for i, k in enumerate(l2):
assert i == inner
inner += 1
outer += 1
assert outer_seen == l1
def nested_range() -> None:
outer = 0
outer_seen = []
for i in range(3):
assert i == outer
outer_seen.append(i)
inner = 0
for i in range(3):
assert i == inner
inner += 1
outer += 1
assert outer_seen == [0,1,2]
def nested_list() -> None:
l1 = [0,1,2]
l2 = [0,1,2]
outer_seen = []
outer = 0
for i in l1:
assert i == outer
outer_seen.append(i)
inner = 0
for i in l2:
assert i == inner
inner += 1
outer += 1
assert outer_seen == l1
def nested_yield() -> Generator:
for i in range(3):
for i in range(3):
yield i
yield i
[file driver.py]
from native import nested_enumerate, nested_range, nested_list, nested_yield
nested_enumerate()
nested_range()
nested_list()
gen = nested_yield()
for k in range(12):
assert next(gen) == k % 4
[out]
[case testAsync]
import asyncio
async def h() -> int:
return 1
async def g() -> int:
await asyncio.sleep(0.01)
return await h()
async def f() -> int:
return await g()
loop = asyncio.get_event_loop()
result = loop.run_until_complete(f())
assert result == 1
[typing fixtures/typing-full.pyi]
[file driver.py]
from native import f
import asyncio
loop = asyncio.get_event_loop()
result = loop.run_until_complete(f())
assert result == 1
[case testRecursiveFibonacci]
def fib(n: int) -> int:
if n <= 1:
return 1
else:
return fib(n - 1) + fib(n - 2)
return 0 # TODO: This should be unnecessary
[file driver.py]
from native import fib
print(fib(0))
print(fib(1))
print(fib(2))
print(fib(6))
[out]
1
1
2
13
[case testListPlusEquals]
from typing import Any
def append(x: Any) -> None:
x += [1]
[file driver.py]
from native import append
x = []
append(x)
assert x == [1]
[case testListSum]
from typing import List
def sum(a: List[int], l: int) -> int:
sum = 0
i = 0
while i < l:
sum = sum + a[i]
i = i + 1
return sum
[file driver.py]
from native import sum
print(sum([], 0))
print(sum([3], 1))
print(sum([5, 6, -4], 3))
print(sum([2**128 + 5, -2**127 - 8], 2))
[out]
0
3
7
170141183460469231731687303715884105725
[case testListSet]
from typing import List
def copy(a: List[int], b: List[int], l: int) -> int:
i = 0
while i < l:
a[i] = b[i]
i = i + 1
return 0
[file driver.py]
from native import copy
a = [0, '']
copy(a, [-1, 5], 2)
print(1, a)
copy(a, [2**128 + 5, -2**127 - 8], 2)
print(2, a)
[out]
1 [-1, 5]
2 [340282366920938463463374607431768211461, -170141183460469231731687303715884105736]
[case testSieve]
from typing import List
def primes(n: int) -> List[int]:
a = [1] * (n + 1)
a[0] = 0
a[1] = 0
i = 0
while i < n:
if a[i] == 1:
j = i * i
while j < n:
a[j] = 0
j = j + i
i = i + 1
return a
[file driver.py]
from native import primes
print(primes(3))
print(primes(13))
[out]
\[0, 0, 1, 1]
\[0, 0, 1, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1]
[case testListPrims]
from typing import List
def append(x: List[int], n: int) -> None:
x.append(n)
def pop_last(x: List[int]) -> int:
return x.pop()
def pop(x: List[int], i: int) -> int:
return x.pop(i)
def count(x: List[int], i: int) -> int:
return x.count(i)
[file driver.py]
from native import append, pop_last, pop, count
l = [1, 2]
append(l, 10)
assert l == [1, 2, 10]
append(l, 3)
append(l, 4)
append(l, 5)
assert l == [1, 2, 10, 3, 4, 5]
pop_last(l)
pop_last(l)
assert l == [1, 2, 10, 3]
pop(l, 2)
assert l == [1, 2, 3]
pop(l, -2)
assert l == [1, 3]
assert count(l, 1) == 1
assert count(l, 2) == 0
[case testTrue]
def f() -> bool:
return True
[file driver.py]
from native import f
print(f())
[out]
True
[case testBoolIf]
def f(x: bool) -> bool:
if x:
return False
else:
return True
[file driver.py]
from native import f
print(f(True))
print(f(False))
[out]
False
True
[case testTuple]
from typing import List, Optional, Tuple
from typing import Tuple
def f(x: Tuple[int, int]) -> Tuple[int,int]:
return x
def lurr(x: List[Optional[Tuple[int, str]]]) -> object:
return x[0]
def asdf(x: Tuple[int, str]) -> None:
pass
[file driver.py]
from testutil import assertRaises
from native import f, lurr, asdf
assert f((1,2)) == (1, 2)
assert lurr([(1, '2')]) == (1, '2')
with assertRaises(TypeError):
print(lurr([(1, 2)]))
with assertRaises(TypeError):
asdf((1, 2))
[case testEmptyTupleFunctionWithTupleType]
from typing import Tuple
def f() -> Tuple[()]:
return ()
[file driver.py]
from native import f
assert f() == ()
[case testEmptyTupleFunctionWithAnyType]
from typing import Any
def f() -> Any:
return ()
[file driver.py]
from native import f
assert f() == ()
[case testTupleGet]
from typing import Tuple
def f(x: Tuple[Tuple[int, bool], int]) -> int:
return x[0][0]
[file driver.py]
from native import f
print(f(((1,True),2)))
[out]
1
[case testTupleGetBoxedInt]
from typing import Tuple
def f(x: Tuple[Tuple[int, bool], int]) -> int:
return x[0][0]
[file driver.py]
from native import f
big_number = pow(2, 80)
print(f(((big_number,True),2)))
[out]
1208925819614629174706176
[case testNewTuple]
def f() -> int:
x = (False, 1)
return x[1]
[file driver.py]
from native import f
print(f())
[out]
1
[case testNewTupleBoxedInt]
def f(y: int) -> int:
x = (False, y)
return x[1]
[file driver.py]
from native import f
big_number = pow(2, 80)
print(f(big_number))
[out]
1208925819614629174706176
[case testSequenceTuple]
from typing import List
def f(x: List[int]) -> int:
return tuple(x)[1]
[file driver.py]
from native import f
print(f([1,2,3,4]))
[out]
2
[case testSequenceTupleLen]
from typing import List
def f(x: List[int]) -> int:
return len(tuple(x))
[file driver.py]
from native import f
print(f([1,2,3,4]))
[out]
4
[case testSequenceTupleArg]
from typing import Tuple
def f(x: Tuple[int, ...]) -> int:
return x[1]
[file driver.py]
from native import f
print(f((1,2,3,4)))
[out]
2
[case testMaybeUninitVar]
class C:
def __init__(self, x: int) -> None:
self.x = x
def f(b: bool) -> None:
u = C(1)
while b:
v = C(2)
if v is not u:
break
print(v.x)
[file driver.py]
from native import f
f(True)
[out]
2
[case testUninitBoom]
def f(a: bool, b: bool) -> None:
if a:
x = 'lol'
if b:
print(x)
def g() -> None:
try:
[0][1]
y = 1
except Exception:
pass
print(y)
[file driver.py]
from native import f, g
from testutil import assertRaises
f(True, True)
f(False, False)
with assertRaises(NameError):
f(False, True)
with assertRaises(NameError):
g()
[out]
lol
[case testFunctionCallWithDefaultArgs]
from typing import Tuple, List, Optional, Callable, Any
def f(x: int, y: int = 3, s: str = "test", z: object = 5) -> Tuple[int, str]:
def inner() -> int:
return x + y
return inner(), s
def g() -> None:
assert f(2) == (5, "test")
assert f(s = "123", x = -2) == (1, "123")
def h(a: Optional[object] = None, b: Optional[str] = None) -> Tuple[object, Optional[str]]:
return (a, b)
def same(x: object = object()) -> object:
return x
a_lambda: Callable[..., Any] = lambda n=20: n
def nested_funcs(n: int) -> List[Callable[..., Any]]:
ls: List[Callable[..., Any]] = []
for i in range(n):
def f(i: int = i) -> int:
return i
ls.append(f)
return ls
[file driver.py]
from native import f, g, h, same, nested_funcs, a_lambda
g()
assert f(2) == (5, "test")
assert f(s = "123", x = -2) == (1, "123")
assert h() == (None, None)
assert h(10) == (10, None)
assert h(b='a') == (None, 'a')
assert h(10, 'a') == (10, 'a')
assert same() == same()
assert [f() for f in nested_funcs(10)] == list(range(10))
assert a_lambda(10) == 10
assert a_lambda() == 20
[case testMethodCallWithDefaultArgs]
from typing import Tuple, List
class A:
def f(self, x: int, y: int = 3, s: str = "test") -> Tuple[int, str]:
def inner() -> int:
return x + y
return inner(), s
def g() -> None:
a = A()
assert a.f(2) == (5, "test")
assert a.f(s = "123", x = -2) == (1, "123")
[file driver.py]
from native import A, g
g()
a = A()
assert a.f(2) == (5, "test")
assert a.f(s = "123", x = -2) == (1, "123")
[case testMethodCallOrdering]
class A:
def __init__(self, s: str) -> None:
print(s)
def f(self, x: 'A', y: 'A') -> None:
pass
def g() -> None:
A('A!').f(A('hello'), A('world'))
[file driver.py]
from native import g
g()
[out]
A!
hello
world
[case testImports]
import testmodule
def f(x: int) -> int:
return testmodule.factorial(5)
def g(x: int) -> int:
from welp import foo
return foo(x)
[file testmodule.py]
def factorial(x: int) -> int:
if x == 0:
return 1
else:
return x * factorial(x-1)
[file welp.py]
def foo(x: int) -> int:
return x
[file driver.py]
from native import f, g
print(f(5))
print(g(5))
[out]
120
5
[case testBuiltins]
y = 10
def f(x: int) -> None:
print(5)
d = globals()
assert d['y'] == 10
d['y'] = 20
assert y == 20
[file driver.py]
from native import f
f(5)
[out]
5
[case testImportMissing]
# The unchecked module is configured by the test harness to not be
# picked up by mypy, so we can test that we do that right thing when
# calling library modules without stubs.
import unchecked # type: ignore
import unchecked as lol # type: ignore
assert unchecked.x == 10
assert lol.x == 10
[file unchecked.py]
x = 10
[file driver.py]
import native
[case testOptional]
from typing import Optional
class A: pass
def f(x: Optional[A]) -> Optional[A]:
return x
def g(x: Optional[A]) -> int:
if x is None:
return 1
if x is not None:
return 2
return 3
def h(x: Optional[int], y: Optional[bool]) -> None:
pass
[file driver.py]
from native import f, g, A
a = A()
assert f(None) is None
assert f(a) is a
assert g(None) == 1
assert g(a) == 2
[case testFromImport]
from testmodule import g
def f(x: int) -> int:
return g(x)
[file testmodule.py]
def g(x: int) -> int:
return x + 1
[file driver.py]
from native import f
assert f(1) == 2
[case testStr]
def f() -> str:
return 'some string'
def g() -> str:
return 'some\a \v \t \x7f " \n \0string 🐍'
def tostr(x: int) -> str:
return str(x)
def concat(x: str, y: str) -> str:
return x + y
def eq(x: str) -> int:
if x == 'foo':
return 0
elif x != 'bar':
return 1
return 2
[file driver.py]
from native import f, g, tostr, concat, eq
assert f() == 'some string'
assert g() == 'some\a \v \t \x7f " \n \0string 🐍'
assert tostr(57) == '57'
assert concat('foo', 'bar') == 'foobar'
assert eq('foo') == 0
assert eq('zar') == 1
assert eq('bar') == 2
[case testFstring]
var = 'mypyc'
num = 20
f1 = f'Hello {var}, this is a test'
f2 = f'Hello {var!r}'
f3 = f'Hello {var!a}'
f4 = f'Hello {var!s}'
f5 = f'Hello {var:>20}'
f6 = f'Hello {var!r:>20}'
f7 = f'Hello {var:>{num}}'
f8 = f'Hello {var!r:>{num}}'
f9 = f'Hello {var}, hello again {var}'
[file driver.py]
from native import f1, f2, f3, f4, f5, f6, f7, f8, f9
assert f1 == "Hello mypyc, this is a test"
assert f2 == "Hello 'mypyc'"
assert f3 == "Hello 'mypyc'"
assert f4 == "Hello mypyc"
assert f5 == "Hello mypyc"
assert f6 == "Hello 'mypyc'"
assert f7 == "Hello mypyc"
assert f8 == "Hello 'mypyc'"
assert f9 == "Hello mypyc, hello again mypyc"
[out]
[case testSets]
from typing import Set, List
def instantiateLiteral() -> Set[int]:
return {1, 2, 3, 5, 8}
def fromIterator() -> List[Set[int]]:
x = set([1, 3, 5])
y = set((1, 3, 5))
z = set({1: '1', 3: '3', 5: '5'})
return [x, y, z]
def addIncrementing(s : Set[int]) -> None:
for a in [1, 2, 3]:
if a not in s:
s.add(a)
return
def replaceWith1(s : Set[int]) -> None:
s.clear()
s.add(1)
def remove1(s : Set[int]) -> None:
s.remove(1)
def discard1(s: Set[int]) -> None:
s.discard(1)
def pop(s : Set[int]) -> int:
return s.pop()
def update(s: Set[int], x: List[int]) -> None:
s.update(x)
[file driver.py]
from native import instantiateLiteral
from testutil import assertRaises
val = instantiateLiteral()
assert 1 in val
assert 2 in val
assert 3 in val
assert 5 in val
assert 8 in val
assert len(val) == 5
assert val == {1, 2, 3, 5, 8}
s = 0
for i in val:
s += i
assert s == 19
from native import fromIterator
sets = fromIterator()
for s in sets:
assert s == {1, 3, 5}
from native import addIncrementing
s = set()
addIncrementing(s)
assert s == {1}
addIncrementing(s)
assert s == {1, 2}
addIncrementing(s)
assert s == {1, 2, 3}
from native import replaceWith1
s = {3, 7, 12}
replaceWith1(s)
assert s == {1}
from native import remove1
import traceback
s = {1, 4, 6}
remove1(s)
assert s == {4, 6}
with assertRaises(KeyError, '1'):
remove1(s)
from native import discard1
s = {1, 4, 6}
discard1(s)
assert s == {4, 6}
discard1(s)
assert s == {4, 6}
from native import pop
s = {1, 2, 3}
x = pop(s)
assert len(s) == 2
assert x in [1, 2, 3]
y = pop(s)
assert len(s) == 1
assert y in [1, 2, 3]
assert x != y
z = pop(s)
assert len(s) == 0
assert z in [1, 2, 3]
assert x != z
assert y != z
with assertRaises(KeyError, 'pop from an empty set'):
pop(s)
from native import update
s = {1, 2, 3}
update(s, [5, 4, 3])
assert s == {1, 2, 3, 4, 5}
[case testDictStuff]
from typing import Dict, Any
from defaultdictwrap import make_dict
def f(x: int) -> int:
dict1 = {} # type: Dict[int, int]
dict1[1] = 1
dict2 = {} # type: Dict[int, int]
dict2[x] = 2
dict1.update(dict2)
l = [(5, 2)] # type: Any
dict1.update(l)
d2 = {6: 4} # type: Any
dict1.update(d2)
return dict1[1]
def g() -> int:
d = make_dict()
d['a'] = 10
d['a'] += 10
d['b'] += 10
l = [('c', 2)] # type: Any
d.update(l)
d2 = {'d': 4} # type: Any
d.update(d2)