File: run-functions.test

package info (click to toggle)
mypy 0.812-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 18,596 kB
  • sloc: python: 74,869; cpp: 11,212; ansic: 3,935; makefile: 238; sh: 13
file content (1090 lines) | stat: -rw-r--r-- 25,603 bytes parent folder | download
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
# Test cases for functions and calls (compile and run)

[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 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 testNestedFunctions]
from typing import Callable, List

def a() -> Callable[[], object]:
    def inner() -> object:
        return None
    return inner

def b() -> Callable[[], Callable[[], str]]:
    def first() -> Callable[[], str]:
        def second() -> str:
            return 'b.first.second: nested function'
        return second
    return first

def c(num: float) -> Callable[[str], str]:
    def inner(s: str) -> str:
        return s + '!'
    return inner

def d(num: float) -> str:
    def inner(s: str) -> str:
        return s + '?'
    a = inner('one')
    b = inner('two')
    return a

def e() -> int:
    return 0

def f() -> int:
    def inner() -> int:
        return e()
    return inner()

def g() -> Callable[[], Callable[[], int]]:
    def inner() -> Callable[[], int]:
        return e
    return inner

def h(num: int) -> int:
    def inner() -> int:
        return num
    return inner()

def i() -> int:
    num = 3
    def inner() -> int:
        return num
    return inner()

def j(num: int) -> int:
    x = 1
    y = 2
    def inner() -> int:
        nonlocal x
        x = 3
        return num + x + y
    return inner()

def k() -> int:
    num = 3
    def inner() -> int:
        nonlocal num
        num = 5
        return num
    return inner() + num

def l() -> int:
    num = 3
    def inner() -> int:
        num = 5
        return num
    return inner() + num

def m() -> Callable[[], int]:
    num = 1
    def inner() -> int:
        num += 1
        return num
    num += 1
    return inner

def n() -> int:
    x = 1
    def add_one() -> None:
        x += 1
    def add_two() -> None:
        x += 2
    add_one()
    add_two()
    return x

def triple(a: int) -> Callable[[], Callable[[int], int]]:
    x = 1
    def outer() -> Callable[[int], int]:
        nonlocal x
        x += 1
        x += a
        a += 1
        def inner(b: int) -> int:
            x += b
            return x
        return inner
    return outer

def if_else(flag: int) -> str:
    def dummy_funtion() -> str:
        return 'if_else.dummy_function'

    if flag < 0:
        def inner() -> str:
            return 'if_else.inner: first definition'
    elif flag > 0:
        def inner() -> str:
            return 'if_else.inner: second definition'
    else:
        def inner() -> str:
            return 'if_else.inner: third definition'
    return inner()

def for_loop() -> int:
    def dummy_funtion() -> str:
        return 'for_loop.dummy_function'

    for i in range(5):
        def inner(i: int) -> int:
            return i
        if i == 3:
            return inner(i)
    return 0

def while_loop() -> int:
    def dummy_funtion() -> str:
        return 'while_loop.dummy_function'

    i = 0
    while i < 5:
        def inner(i: int) -> int:
            return i
        if i == 3:
            return inner(i)
        i += 1
    return 0

def free_vars(foo: int, bar: int) -> int:
    x = 1
    y = 2
    def g():  # type: ignore  # missing type annotation for testing
        nonlocal y
        y = 3
        nonlocal bar
        bar += y
    z = 3
    g()
    return bar

def lambdas(x: int, y: int) -> int:
    s = lambda a, b: a + b + x + y
    return s(1, 2)

def outer() -> str:
    return 'outer: normal function'

def inner() -> str:
    return 'inner: normal function'

class A:
    def __init__(self, x: int) -> None:
        self.x = x

    def outer(self, num: int) -> int:
        y = 5
        def inner() -> int:
            return self.x + y + num
        return inner()

def o() -> int:
    a = [0, 0]
    b = 0
    def b_incr() -> List[int]:
        b += 10
        return a
    c = 0
    def c_incr() -> int:
        c += 1
        return c

    # x = 1, y = 1
    x = y = c_incr()

    # a = [2, 2], b = 20
    b_incr()[0] = b_incr()[1] = c_incr()
    # Should return 26.
    return x + y + a[0] + a[1] + b

global_upvar = 20

toplevel_lambda = lambda x: 10 + global_upvar + x

[file driver.py]
from native import (
    a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, triple, if_else, for_loop, while_loop,
    free_vars, lambdas, outer, inner, A, toplevel_lambda
)

assert a()() == None
assert b()()() == 'b.first.second: nested function'
assert c(5.0)('c') == 'c!'
assert d(4.0) == 'one?'
assert e() == 0
assert f() == 0
assert g()()() == 0
assert h(3) == 3
assert i() == 3
assert j(3) == 8
assert k() == 10
assert l() == 8
assert m()() == 3
assert n() == 4
assert o() == 26

triple_outer = triple(2)
triple_inner = triple_outer()

assert triple_inner(4) == 8
assert triple_inner(4) == 12
assert triple_outer()(4) == 20

assert if_else(-1) == 'if_else.inner: first definition'
assert if_else(1) == 'if_else.inner: second definition'
assert if_else(0) == 'if_else.inner: third definition'

assert for_loop() == 3
assert while_loop() == 3

assert free_vars(1, 2) == 5
assert lambdas(3, 4) == 10

assert outer() == 'outer: normal function'
assert inner() == 'inner: normal function'

assert A(3).outer(4) == 12

assert toplevel_lambda(5) == 35

[case testNestedFunctions2]
from typing import Callable

def outer() -> Callable[[], object]:
    def inner() -> object:
        return None
    return inner

def first() -> Callable[[], Callable[[], str]]:
    def second() -> Callable[[], str]:
        def third() -> str:
            return 'third: nested function'
        return third
    return second

def f1() -> int:
        x = 1
        def f2() -> int:
                y = 2
                def f3() -> int:
                        z = 3
                        return y
                return f3()
        return f2()

def outer_func() -> int:
    def inner_func() -> int:
        return x
    x = 1
    return inner_func()

def mutual_recursion(start : int) -> int:
    def f1(k : int) -> int:
        if k <= 0:
            return 0
        k -= 1
        return f2(k)

    def f2(k : int) -> int:
        if k <= 0:
            return 0
        k -= 1
        return f1(k)
    return f1(start)

def topLayer() -> int:
    def middleLayer() -> int:
        def bottomLayer() -> int:
            return x

        return bottomLayer()

    x = 1
    return middleLayer()

def nest1() -> str:
    def nest2() -> str:
        def nest3() -> str:
            def mut1(val: int) -> str:
                if val <= 0:
                    return "bottomed"
                val -= 1
                return mut2(val)
            def mut2(val: int) -> str:
                if val <= 0:
                        return "bottomed"
                val -= 1
                return mut1(val)
            return mut1(start)
        return nest3()
    start = 3
    return nest2()

def uno(num: float) -> Callable[[str], str]:
    def dos(s: str) -> str:
        return s + '!'
    return dos

def eins(num: float) -> str:
    def zwei(s: str) -> str:
        return s + '?'
    a = zwei('eins')
    b = zwei('zwei')
    return a

def call_other_inner_func(a: int) -> int:
    def foo() -> int:
        return a + 1

    def bar() -> int:
        return foo()

    def baz(n: int) -> int:
        if n == 0:
            return 0
        return n + baz(n - 1)

    return bar() + baz(a)

def inner() -> str:
    return 'inner: normal function'

def second() -> str:
    return 'second: normal function'

def third() -> str:
    return 'third: normal function'

[file driver.py]
from native import (outer, inner, first, uno, eins, call_other_inner_func,
second, third, f1, outer_func, mutual_recursion, topLayer, nest1)

assert outer()() == None
assert inner() == 'inner: normal function'
assert first()()() == 'third: nested function'
assert uno(5.0)('uno') == 'uno!'
assert eins(4.0) == 'eins?'
assert call_other_inner_func(5) == 21
assert second() == 'second: normal function'
assert third() == 'third: normal function'
assert f1() == 2
assert outer_func() == 1
assert mutual_recursion(5) == 0
assert topLayer() == 1
assert nest1() == "bottomed"

[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 testPyMethodCall]
from typing import List
def f(x: List[int]) -> int:
    return x.pop()
def g(x: List[int], y: List[int]) -> None:
    x.extend(y)
[file driver.py]
from native import f, g
l = [1, 2]
assert f(l) == 2
g(l, [10])
assert l == [1, 10]
assert f(l) == 10
assert f(l) == 1
g(l, [11, 12])
assert l == [11, 12]

[case testMethodCallWithKeywordArgs]
from typing import Tuple
import testmodule
class A:
    def echo(self, a: int, b: int, c: int) -> Tuple[int, int, int]:
        return a, b, c
def test_native_method_call_with_kwargs() -> None:
    a = A()
    assert a.echo(1, c=3, b=2) == (1, 2, 3)
    assert a.echo(c = 3, a = 1, b = 2) == (1, 2, 3)
def test_module_method_call_with_kwargs() -> None:
    a = testmodule.A()
    assert a.echo(1, c=3, b=2) == (1, 2, 3)
    assert a.echo(c = 3, a = 1, b = 2) == (1, 2, 3)
[file testmodule.py]
from typing import Tuple
class A:
    def echo(self, a: int, b: int, c: int) -> Tuple[int, int, int]:
        return a, b, c
[file driver.py]
import native
native.test_native_method_call_with_kwargs()
native.test_module_method_call_with_kwargs()

[case testAnyCall]
from typing import Any
def call(f: Any) -> Any:
    return f(1, 'x')
[file driver.py]
from native import call
def f(x, y):
    return (x, y)
def g(x): pass

assert call(f) == (1, 'x')
for bad in g, 1:
    try:
        call(bad)
    except TypeError:
        pass
    else:
        assert False, bad

[case testCallableTypes]
from typing import Callable
def absolute_value(x: int) -> int:
    return x if x > 0 else -x

def call_native_function(x: int) -> int:
    return absolute_value(x)

def call_python_function(x: int) -> int:
    return int(x)

def return_float() -> float:
    return 5.0

def return_callable_type() -> Callable[[], float]:
    return return_float

def call_callable_type() -> float:
    f = return_callable_type()
    return f()

def return_passed_in_callable_type(f: Callable[[], float]) -> Callable[[], float]:
    return f

def call_passed_in_callable_type(f: Callable[[], float]) -> float:
    return f()

[file driver.py]
from native import call_native_function, call_python_function, return_float, return_callable_type, call_callable_type, return_passed_in_callable_type, call_passed_in_callable_type
a = call_native_function(1)
b = call_python_function(1)
c = return_callable_type()
d = call_callable_type()
e = return_passed_in_callable_type(return_float)
f = call_passed_in_callable_type(return_float)
assert a == 1
assert b == 1
assert c() == 5.0
assert d == 5.0
assert e() == 5.0
assert f == 5.0

[case testKeywordArgs]
from typing import Tuple
import testmodule

def g(a: int, b: int, c: int) -> Tuple[int, int, int]:
    return a, b, c

def test_call_native_function_with_keyword_args() -> None:
    assert g(1, c = 3, b = 2) == (1, 2, 3)
    assert g(c = 3, a = 1, b = 2) == (1, 2, 3)

def test_call_module_function_with_keyword_args() -> None:
    assert testmodule.g(1, c = 3, b = 2) == (1, 2, 3)
    assert testmodule.g(c = 3, a = 1, b = 2) == (1, 2, 3)

def test_call_python_function_with_keyword_args() -> None:
    assert int("11", base=2) == 3

def test_call_lambda_function_with_keyword_args() -> None:
    g = testmodule.get_lambda_function()
    assert g(1, c = 3, b = 2) == (1, 2, 3)
    assert g(c = 3, a = 1, b = 2) == (1, 2, 3)

[file testmodule.py]
from typing import Tuple

def g(a: int, b: int, c: int) -> Tuple[int, int, int]:
    return a, b, c

def get_lambda_function():
    return (lambda a, b, c: (a, b, c))

[file driver.py]
import native
native.test_call_native_function_with_keyword_args()
native.test_call_module_function_with_keyword_args()
native.test_call_python_function_with_keyword_args()
native.test_call_lambda_function_with_keyword_args()

[case testStarArgs]
from typing import Tuple

def g(a: int, b: int, c: int) -> Tuple[int, int, int]:
    return a, b, c

def test_star_args() -> None:
    assert g(*[1, 2, 3]) == (1, 2, 3)
    assert g(*(1, 2, 3)) == (1, 2, 3)
    assert g(*(1,), *[2, 3]) == (1, 2, 3)
    assert g(*(), *(1,), *(), *(2,), *(3,), *()) == (1, 2, 3)
    assert g(*range(3)) == (0, 1, 2)

[file driver.py]
import native
native.test_star_args()

[case testStar2Args]
from typing import Tuple

def g(a: int, b: int, c: int) -> Tuple[int, int, int]:
    return a, b, c

def test_star2_args() -> None:
    assert g(**{'a': 1, 'b': 2, 'c': 3}) == (1, 2, 3)
    assert g(**{'c': 3, 'a': 1, 'b': 2}) == (1, 2, 3)
    assert g(b=2, **{'a': 1, 'c': 3}) == (1, 2, 3)

def test_star2_args_bad(v: dict) -> bool:
    return g(a=1, b=2, **v) == (1, 2, 3)
[file driver.py]
import native
native.test_star2_args()

# this should raise TypeError due to duplicate kwarg, but currently it doesn't
assert native.test_star2_args_bad({'b': 2, 'c': 3})

[case testStarAndStar2Args]
from typing import Tuple
def g(a: int, b: int, c: int) -> Tuple[int, int, int]:
    return a, b, c

class C:
    def g(self, a: int, b: int, c: int) -> Tuple[int, int, int]:
        return a, b, c

def test_star_and_star2_args() -> None:
    assert g(1, *(2,), **{'c': 3}) == (1, 2, 3)
    assert g(*[1], **{'b': 2, 'c': 3}) == (1, 2, 3)
    c = C()
    assert c.g(1, *(2,), **{'c': 3}) == (1, 2, 3)
    assert c.g(*[1], **{'b': 2, 'c': 3}) == (1, 2, 3)

[file driver.py]
import native
native.test_star_and_star2_args()

[case testAllTheArgCombinations]
from typing import Tuple
def g(a: int, b: int, c: int, d: int = -1) -> Tuple[int, int, int, int]:
    return a, b, c, d

class C:
    def g(self, a: int, b: int, c: int, d: int = -1) -> Tuple[int, int, int, int]:
        return a, b, c, d

def test_all_the_arg_combinations() -> None:
    assert g(1, *(2,), **{'c': 3}) == (1, 2, 3, -1)
    assert g(*[1], **{'b': 2, 'c': 3, 'd': 4}) == (1, 2, 3, 4)
    c = C()
    assert c.g(1, *(2,), **{'c': 3}) == (1, 2, 3, -1)
    assert c.g(*[1], **{'b': 2, 'c': 3, 'd': 4}) == (1, 2, 3, 4)

[file driver.py]
import native
native.test_all_the_arg_combinations()

[case testOverloads]
from typing import overload, Union, Tuple

@overload
def foo(x: int) -> int: ...

@overload
def foo(x: str) -> str: ...

def foo(x: Union[int, str]) -> Union[int, str]:
    return x

class A:
    @overload
    def foo(self, x: int) -> int: ...

    @overload
    def foo(self, x: str) -> str: ...

    def foo(self, x: Union[int, str]) -> Union[int, str]:
        return x

def call1() -> Tuple[int, str]:
    return (foo(10), foo('10'))
def call2() -> Tuple[int, str]:
    x = A()
    return (x.foo(10), x.foo('10'))

[file driver.py]
from native import *
assert call1() == (10, '10')
assert call2() == (10, '10')

[case testDecorators1]
from typing import Generator, Callable, Iterator
from contextlib import contextmanager

def a(f: Callable[[], None]) -> Callable[[], None]:
    def g() -> None:
        print('Entering')
        f()
        print('Exited')
    return g

def b(f: Callable[[], None]) -> Callable[[], None]:
    def g() -> None:
        print('***')
        f()
        print('***')
    return g

@contextmanager
def foo() -> Iterator[int]:
    try:
        print('started')
        yield 0
    finally:
        print('finished')

@contextmanager
def catch() -> Iterator[None]:
    try:
        print('started')
        yield
    except IndexError:
        print('index')
        raise
    except Exception:
        print('lol')

def thing() -> None:
    c()

@a
@b
def c() -> None:
    @a
    @b
    def d() -> None:
        print('d')
    print('c')
    d()

def hm() -> None:
    x = [1]
    with catch():
        x[2]

[file driver.py]
from native import foo, c, thing, hm

with foo() as f:
    print('hello')

c()
thing()
print('==')
try:
    hm()
except IndexError:
    pass
else:
    assert False

[out]
started
hello
finished
Entering
***
c
Entering
***
d
***
Exited
***
Exited
Entering
***
c
Entering
***
d
***
Exited
***
Exited
==
started
index

[case testDecoratorsMethods]
from typing import Any, Callable, Iterator, TypeVar
from contextlib import contextmanager

T = TypeVar('T')
def dec(f: T) -> T:
    return f

def a(f: Callable[[Any], None]) -> Callable[[Any], None]:
    def g(a: Any) -> None:
        print('Entering')
        f(a)
        print('Exited')
    return g

class A:
    @a
    def foo(self) -> None:
        print('foo')

    @contextmanager
    def generator(self) -> Iterator[int]:
        try:
            print('contextmanager: entering')
            yield 0
        finally:
            print('contextmanager: exited')

class Lol:
    @staticmethod
    def foo() -> None:
        Lol.bar()
        Lol.baz()

    @staticmethod
    @dec
    def bar() -> None:
        pass

    @classmethod
    @dec
    def baz(cls) -> None:
        pass

def inside() -> None:
    with A().generator() as g:
        print('hello!')

with A().generator() as g:
    print('hello!')

def lol() -> None:
    with A().generator() as g:
        raise Exception

[file driver.py]
from native import A, lol

A.foo(A())
A().foo()
with A().generator() as g:
    print('hello!')
try:
    lol()
except:
    pass
else:
    assert False

[out]
contextmanager: entering
hello!
contextmanager: exited
Entering
foo
Exited
Entering
foo
Exited
contextmanager: entering
hello!
contextmanager: exited
contextmanager: entering
contextmanager: exited

[case testUnannotatedFunction]
def g(x: int) -> int:
    return x * 2

def f(x):
    return g(x)
[file driver.py]
from native import f
assert f(3) == 6

[case testComplicatedArgs]
from typing import Tuple, Dict

def kwonly1(x: int = 0, *, y: int) -> Tuple[int, int]:
    return x, y

def kwonly2(*, x: int = 0, y: int) -> Tuple[int, int]:
    return x, y

def kwonly3(a: int, b: int = 0, *, y: int, x: int = 1) -> Tuple[int, int, int, int]:
    return a, b, x, y

def kwonly4(*, x: int, y: int) -> Tuple[int, int]:
    return x, y

def varargs1(*args: int) -> Tuple[int, ...]:
    return args

def varargs2(*args: int, **kwargs: int) -> Tuple[Tuple[int, ...], Dict[str, int]]:
    return args, kwargs

def varargs3(**kwargs: int) -> Dict[str, int]:
    return kwargs

def varargs4(a: int, b: int = 0,
             *args: int, y: int, x: int = 1,
             **kwargs: int) -> Tuple[Tuple[int, ...], Dict[str, int]]:
    return (a, b, *args), {'x': x, 'y': y, **kwargs}

class A:
    def f(self, x: int) -> Tuple[int, ...]:
        return (x,)
    def g(self, x: int) -> Tuple[Tuple[int, ...], Dict[str, int]]:
        return (x,), {}

class B(A):
    def f(self, *args: int) -> Tuple[int, ...]:
        return args
    def g(self, *args: int, **kwargs: int) -> Tuple[Tuple[int, ...], Dict[str, int]]:
        return args, kwargs

[file other.py]
# This file is imported in both compiled and interpreted mode in order to
# test both native calls and calls via the C API.

from native import (
    kwonly1, kwonly2, kwonly3, kwonly4,
    varargs1, varargs2, varargs3, varargs4,
    A, B
)

# kwonly arg tests
assert kwonly1(10, y=20) == (10, 20)
assert kwonly1(y=20) == (0, 20)

assert kwonly2(x=10, y=20) == (10, 20)
assert kwonly2(y=20) == (0, 20)

assert kwonly3(10, y=20) == (10, 0, 1, 20)
assert kwonly3(a=10, y=20) == (10, 0, 1, 20)
assert kwonly3(10, 30, y=20) == (10, 30, 1, 20)
assert kwonly3(10, b=30, y=20) == (10, 30, 1, 20)
assert kwonly3(a=10, b=30, y=20) == (10, 30, 1, 20)

assert kwonly3(10, x=40, y=20) == (10, 0, 40, 20)
assert kwonly3(a=10, x=40, y=20) == (10, 0, 40, 20)
assert kwonly3(10, 30, x=40, y=20) == (10, 30, 40, 20)
assert kwonly3(10, b=30, x=40, y=20) == (10, 30, 40, 20)
assert kwonly3(a=10, b=30, x=40, y=20) == (10, 30, 40, 20)

assert kwonly4(x=1, y=2) == (1, 2)
assert kwonly4(y=2, x=1) == (1, 2)

# varargs tests
assert varargs1() == ()
assert varargs1(1, 2, 3) == (1, 2, 3)
assert varargs2(1, 2, 3) == ((1, 2, 3), {})
assert varargs2(1, 2, 3, x=4) == ((1, 2, 3), {'x': 4})
assert varargs2(x=4) == ((), {'x': 4})
assert varargs3() == {}
assert varargs3(x=4) == {'x': 4}
assert varargs3(x=4, y=5) == {'x': 4, 'y': 5}

assert varargs4(-1, y=2) == ((-1, 0), {'x': 1, 'y': 2})
assert varargs4(-1, 2, y=2) == ((-1, 2), {'x': 1, 'y': 2})
assert varargs4(-1, 2, 3, y=2) == ((-1, 2, 3), {'x': 1, 'y': 2})
assert varargs4(-1, 2, 3, x=10, y=2) == ((-1, 2, 3), {'x': 10, 'y': 2})
assert varargs4(-1, x=10, y=2) == ((-1, 0), {'x': 10, 'y': 2})
assert varargs4(-1, y=2, z=20) == ((-1, 0), {'x': 1, 'y': 2, 'z': 20})
assert varargs4(-1, 2, y=2, z=20) == ((-1, 2), {'x': 1, 'y': 2, 'z': 20})
assert varargs4(-1, 2, 3, y=2, z=20) == ((-1, 2, 3), {'x': 1, 'y': 2, 'z': 20})
assert varargs4(-1, 2, 3, x=10, y=2, z=20) == ((-1, 2, 3), {'x': 10, 'y': 2, 'z': 20})
assert varargs4(-1, x=10, y=2, z=20) == ((-1, 0), {'x': 10, 'y': 2, 'z': 20})

x = B()  # type: A
assert x.f(1) == (1,)
assert x.g(1) == ((1,), {})
# This one is really funny! When we make native calls we lose
# track of which arguments are positional or keyword, so the glue
# calls them all positional unless they are keyword only...
# It would be possible to fix this by dynamically tracking which
# arguments were passed by keyword (for example, by passing a bitmask
# to functions indicating this), but paying a speed, size, and complexity
# cost for something so deeply marginal seems like a bad choice.
# assert x.g(x=1) == ((), {'x': 1})

[file driver.py]
from testutil import assertRaises
from native import (
    kwonly1, kwonly2, kwonly3, kwonly4,
    varargs1, varargs2, varargs3, varargs4,
)

# Run the non-exceptional tests in both interpreted and compiled mode
import other
import other_interpreted


# And the tests for errors at the interfaces in interpreted only
with assertRaises(TypeError, "missing required keyword-only argument 'y'"):
    kwonly1()
with assertRaises(TypeError, "takes at most 1 positional argument (2 given)"):
    kwonly1(10, 20)

with assertRaises(TypeError, "missing required keyword-only argument 'y'"):
    kwonly2()
with assertRaises(TypeError, "takes no positional arguments"):
    kwonly2(10, 20)

with assertRaises(TypeError, "missing required argument 'a'"):
    kwonly3(b=30, x=40, y=20)
with assertRaises(TypeError, "missing required keyword-only argument 'y'"):
    kwonly3(10)

with assertRaises(TypeError, "missing required keyword-only argument 'y'"):
    kwonly4(x=1)
with assertRaises(TypeError, "missing required keyword-only argument 'x'"):
    kwonly4(y=1)
with assertRaises(TypeError, "missing required keyword-only argument 'x'"):
    kwonly4()

with assertRaises(TypeError, "'x' is an invalid keyword argument for varargs1()"):
    varargs1(x=10)
with assertRaises(TypeError, "'x' is an invalid keyword argument for varargs1()"):
    varargs1(1, x=10)
with assertRaises(TypeError, "varargs3() takes no positional arguments"):
    varargs3(10)
with assertRaises(TypeError, "varargs3() takes no positional arguments"):
    varargs3(10, x=10)

with assertRaises(TypeError, "varargs4() missing required argument 'a' (pos 1)"):
    varargs4()
with assertRaises(TypeError, "varargs4() missing required keyword-only argument 'y'"):
    varargs4(1, 2)
with assertRaises(TypeError, "varargs4() missing required keyword-only argument 'y'"):
    varargs4(1, 2, x=1)
with assertRaises(TypeError, "varargs4() missing required keyword-only argument 'y'"):
    varargs4(1, 2, 3)
with assertRaises(TypeError, "varargs4() missing required argument 'a' (pos 1)"):
    varargs4(y=20)