-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Expand file tree
/
Copy pathrun-async.test
More file actions
1386 lines (1062 loc) · 34.5 KB
/
run-async.test
File metadata and controls
1386 lines (1062 loc) · 34.5 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
# async test cases (compile and run)
[case testRunAsyncBasics]
import asyncio
from typing import Callable, Awaitable
from testutil import assertRaises
async def h() -> int:
return 1
async def g() -> int:
await asyncio.sleep(0)
return await h()
async def f() -> int:
return await g() + 2
async def f2() -> int:
x = 0
for i in range(2):
x += i + await f() + await g()
return x
async def test_simple_call() -> None:
result = await f()
assert result == 3
async def test_multiple_awaits_in_expression() -> None:
result = await f2()
assert result == 9
class MyError(Exception):
pass
async def exc1() -> None:
await asyncio.sleep(0)
raise MyError()
async def exc2() -> None:
await asyncio.sleep(0)
raise MyError()
async def exc3() -> None:
await exc1()
async def exc4() -> None:
await exc2()
async def exc5() -> int:
try:
await exc1()
except MyError:
return 3
return 4
async def exc6() -> int:
try:
await exc4()
except MyError:
return 3
return 4
async def test_exception() -> None:
with assertRaises(MyError):
await exc1()
with assertRaises(MyError):
await exc2()
with assertRaises(MyError):
await exc3()
with assertRaises(MyError):
await exc4()
assert await exc5() == 3
assert await exc6() == 3
async def indirect_call(x: int, c: Callable[[int], Awaitable[int]]) -> int:
return await c(x)
async def indirect_call_2(a: Awaitable[None]) -> None:
await a
async def indirect_call_3(a: Awaitable[float]) -> float:
return (await a) + 1.0
async def inc(x: int) -> int:
await asyncio.sleep(0)
return x + 1
async def ident(x: float, err: bool = False) -> float:
await asyncio.sleep(0.0)
if err:
raise MyError()
return x + float("0.0")
async def test_indirect_call() -> None:
assert await indirect_call(3, inc) == 4
with assertRaises(MyError):
await indirect_call_2(exc1())
assert await indirect_call_3(ident(2.0)) == 3.0
assert await indirect_call_3(ident(-113.0)) == -112.0
assert await indirect_call_3(ident(-114.0)) == -113.0
with assertRaises(MyError):
await indirect_call_3(ident(1.0, True))
with assertRaises(MyError):
await indirect_call_3(ident(-113.0, True))
class C:
def __init__(self, n: int) -> None:
self.n = n
async def add(self, x: int, err: bool = False) -> int:
await asyncio.sleep(0)
if err:
raise MyError()
return x + self.n
async def method_call(x: int) -> int:
c = C(5)
return await c.add(x)
async def method_call_exception() -> int:
c = C(5)
return await c.add(3, err=True)
async def test_async_method_call() -> None:
assert await method_call(3) == 8
with assertRaises(MyError):
await method_call_exception()
[file asyncio/__init__.pyi]
async def sleep(t: float) -> None: ...
[typing fixtures/typing-full.pyi]
[case testRunAsyncAwaitInVariousPositions]
from typing import cast, Any
import asyncio
async def one() -> int:
await asyncio.sleep(0.0)
return int() + 1
async def true() -> bool:
return bool(int() + await one())
async def branch_await() -> int:
if bool(int() + 1) == await true():
return 3
return 2
async def branch_await_not() -> int:
if bool(int() + 1) == (not await true()):
return 3
return 2
async def test_branch() -> None:
assert await branch_await() == 3
assert await branch_await_not() == 2
async def assign_multi() -> int:
_, x = int(), await one()
return x + 1
async def test_assign_multi() -> None:
assert await assign_multi() == 2
class C:
def __init__(self, s: str) -> None:
self.s = s
def concat(self, s: str) -> str:
return self.s + s
async def make_c(s: str) -> C:
await one()
return C(s)
async def concat(s: str, t: str) -> str:
await one()
return s + t
async def set_attr(s: str) -> None:
(await make_c("xyz")).s = await concat(s, "!")
async def test_set_attr() -> None:
await set_attr("foo") # Just check that it compiles and runs
def concat2(x: str, y: str) -> str:
return x + y
async def call1(s: str) -> str:
return concat2(str(int()), await concat(s, "a"))
async def call2(s: str) -> str:
return await concat(str(int()), await concat(s, "b"))
async def test_call() -> None:
assert await call1("foo") == "0fooa"
assert await call2("foo") == "0foob"
async def method_call(s: str) -> str:
return C("<").concat(await concat(s, ">"))
async def test_method_call() -> None:
assert await method_call("foo") == "<foo>"
class D:
def __init__(self, a: str, b: str) -> None:
self.a = a
self.b = b
async def construct(s: str) -> str:
c = D(await concat(s, "!"), await concat(s, "?"))
return c.a + c.b
async def test_construct() -> None:
assert await construct("foo") == "foo!foo?"
[file asyncio/__init__.pyi]
async def sleep(t: float) -> None: ...
[typing fixtures/typing-full.pyi]
[case testAsyncWith]
from testutil import async_val
class async_ctx:
async def __aenter__(self) -> str:
await async_val("enter")
return "test"
async def __aexit__(self, x, y, z) -> None:
await async_val("exit")
async def async_with() -> str:
async with async_ctx() as x:
return await async_val("body")
[file driver.py]
from native import async_with
from testutil import run_generator
yields, val = run_generator(async_with(), [None, 'x', None])
assert yields == ('enter', 'body', 'exit'), yields
assert val == 'x', val
[case testAsyncReturn]
from testutil import async_val
async def async_return() -> str:
try:
return 'test'
finally:
await async_val('foo')
[file driver.py]
from native import async_return
from testutil import run_generator
yields, val = run_generator(async_return())
assert yields == ('foo',)
assert val == 'test', val
[case testAsyncFor]
from typing import AsyncIterable, List, Set, Dict
async def async_iter(xs: AsyncIterable[int]) -> List[int]:
ys = []
async for x in xs:
ys.append(x)
return ys
async def async_comp(xs: AsyncIterable[int]) -> List[int]:
ys = [x async for x in xs]
return ys
async def async_comp_set(xs: AsyncIterable[int]) -> Set[int]:
return {x async for x in xs}
async def async_comp_dict(xs: AsyncIterable[int]) -> Dict[int, str]:
return {x: str(x) async for x in xs}
[typing fixtures/typing-full.pyi]
[file driver.py]
from native import async_iter, async_comp, async_comp_set, async_comp_dict
from testutil import run_generator, async_val
from typing import AsyncIterable, List
# defined here since we couldn't do it inside the test yet...
async def foo() -> AsyncIterable[int]:
for x in range(3):
await async_val(x)
yield x
yields, val = run_generator(async_iter(foo()))
assert val == [0,1,2], val
assert yields == (0,1,2), yields
yields, val = run_generator(async_comp(foo()))
assert val == [0,1,2], val
assert yields == (0,1,2), yields
yields, val = run_generator(async_comp_set(foo()))
assert val == {0,1,2}, val
assert yields == (0,1,2), yields
yields, val = run_generator(async_comp_dict(foo()))
assert val == {0: '0',1: '1', 2: '2'}, val
assert yields == (0,1,2), yields
[case testAsyncFor2]
from typing import AsyncIterable, List
async def async_iter(xs: AsyncIterable[int]) -> List[int]:
ys = []
async for x in xs:
ys.append(x)
return ys
[typing fixtures/typing-full.pyi]
[file driver.py]
from native import async_iter
from testutil import run_generator, async_val
from typing import AsyncIterable, List
# defined here since we couldn't do it inside the test yet...
async def foo() -> AsyncIterable[int]:
for x in range(3):
await async_val(x)
yield x
raise Exception('lol no')
yields, val = run_generator(async_iter(foo()))
assert yields == (0,1,2), yields
assert val == 'lol no', val
[case testAsyncWithVarReuse]
class ConMan:
async def __aenter__(self) -> int:
return 1
async def __aexit__(self, *exc: object):
pass
class ConManB:
async def __aenter__(self) -> int:
return 2
async def __aexit__(self, *exc: object):
pass
async def test_x() -> None:
value = 2
async with ConMan() as f:
value += f
assert value == 3, value
async with ConManB() as f:
value += f
assert value == 5, value
[case testRunAsyncSpecialCases]
import asyncio
async def t() -> tuple[int, str, str]:
return (1, "x", "y")
async def f() -> tuple[int, str, str]:
return await t()
async def test_tuple_return() -> None:
result = await f()
assert result == (1, "x", "y")
async def e() -> ValueError:
return ValueError("foo")
async def g() -> ValueError:
return await e()
async def test_exception_return() -> None:
result = await g()
assert isinstance(result, ValueError)
[file asyncio/__init__.pyi]
async def sleep(t: float) -> None: ...
[typing fixtures/typing-full.pyi]
[case testRunAsyncRefCounting]
import asyncio
import gc
async def assert_no_leaks(fn, max_new):
# Warm-up, in case asyncio allocates something on first use
await fn()
gc.collect()
old_objs = gc.get_objects()
for i in range(10):
await fn()
gc.collect()
new_objs = gc.get_objects()
delta = len(new_objs) - len(old_objs)
# Often a few persistent objects get allocated, which may be unavoidable.
# The main thing we care about is that each iteration does not leak an
# additional object.
assert delta <= max_new, delta
async def concat_one(x: str) -> str:
return x + "1"
async def foo(n: int) -> str:
s = ""
while len(s) < n:
s = await concat_one(s)
return s
async def test_trivial() -> None:
await assert_no_leaks(lambda: foo(1000), 5)
async def make_list(a: list[int]) -> list[int]:
await concat_one("foobar")
return [a[0]]
async def spill() -> list[int]:
a: list[int] = []
for i in range(5):
await asyncio.sleep(0.0001)
a = (await make_list(a + [1])) + a + (await make_list(a + [2]))
return a
async def bar(n: int) -> None:
for i in range(n):
await spill()
async def test_spilled() -> None:
await assert_no_leaks(lambda: bar(80), 2)
async def raise_deep(n: int) -> str:
if n == 0:
await asyncio.sleep(0.0001)
raise TypeError(str(n))
else:
if n == 2:
await asyncio.sleep(0.0001)
return await raise_deep(n - 1)
async def maybe_raise(n: int) -> str:
if n % 3 == 0:
await raise_deep(5)
elif n % 29 == 0:
await asyncio.sleep(0.0001)
return str(n)
async def exc(n: int) -> list[str]:
a = []
for i in range(n):
try:
a.append(str(int()) + await maybe_raise(n))
except TypeError:
a.append(str(int() + 5))
return a
async def test_exception() -> None:
await assert_no_leaks(lambda: exc(50), 2)
class C:
def __init__(self, s: str) -> None:
self.s = s
async def id(c: C) -> C:
return c
async def stolen_helper(c: C, s: str) -> str:
await asyncio.sleep(0.0001)
(await id(c)).s = await concat_one(s)
await asyncio.sleep(0.0001)
return c.s
async def stolen(n: int) -> int:
for i in range(n):
c = C(str(i))
s = await stolen_helper(c, str(i + 2))
assert s == str(i + 2) + "1"
return n
async def test_stolen() -> None:
await assert_no_leaks(lambda: stolen(200), 2)
[file asyncio/__init__.pyi]
async def sleep(t: float) -> None: ...
[case testRunAsyncMiscTypesInEnvironment]
# Here we test that values of various kinds of types can be spilled to the
# environment. In particular, types with "overlapping error values" such as
# i64 can be tricky, since they require extra work to support undefined
# attribute values (which raise AttributeError when accessed). For these,
# the object struct has a bitfield which keeps track of whether certain
# attributes have an assigned value.
#
# In practice we mark these attributes as "always defined", which causes these
# checks to be skipped on attribute access, and thus we don't require the
# bitfield to exist.
#
# See the comment of RType.error_overlap for more information.
import asyncio
from mypy_extensions import i64, i32, i16, u8
async def inc_float(x: float) -> float:
return x + 1.0
async def inc_i64(x: i64) -> i64:
return x + 1
async def inc_i32(x: i32) -> i32:
return x + 1
async def inc_i16(x: i16) -> i16:
return x + 1
async def inc_u8(x: u8) -> u8:
return x + 1
async def inc_tuple(x: tuple[i64, float]) -> tuple[i64, float]:
return x[0] + 1, x[1] + 1.5
async def neg_bool(b: bool) -> bool:
return not b
async def float_ops(x: float) -> float:
n = x
n = await inc_float(n)
n = float("0.5") + await inc_float(n)
return n
async def test_float() -> None:
assert await float_ops(2.5) == 5.0
async def i64_ops(x: i64) -> i64:
n = x
n = await inc_i64(n)
n = i64("1") + await inc_i64(n)
return n
async def test_i64() -> None:
assert await i64_ops(2) == 5
async def i32_ops(x: i32) -> i32:
n = x
n = await inc_i32(n)
n = i32("1") + await inc_i32(n)
return n
async def test_i32() -> None:
assert await i32_ops(3) == 6
async def i16_ops(x: i16) -> i16:
n = x
n = await inc_i16(n)
n = i16("1") + await inc_i16(n)
return n
async def test_i16() -> None:
assert await i16_ops(4) == 7
async def u8_ops(x: u8) -> u8:
n = x
n = await inc_u8(n)
n = u8("1") + await inc_u8(n)
return n
async def test_u8() -> None:
assert await u8_ops(5) == 8
async def tuple_ops(x: tuple[i64, float]) -> tuple[i64, float]:
n = x
n = await inc_tuple(n)
m = ((i64("1"), float("0.5")), await inc_tuple(n))
return m[1]
async def test_tuple() -> None:
assert await tuple_ops((1, 2.5)) == (3, 5.5)
async def bool_ops(x: bool) -> bool:
n = x
n = await neg_bool(n)
m = (bool("1"), await neg_bool(n))
return m[0] and m[1]
async def test_bool() -> None:
assert await bool_ops(True) is True
assert await bool_ops(False) is False
[file asyncio/__init__.pyi]
def run(x: object) -> object: ...
[case testRunAsyncNestedFunctions]
from __future__ import annotations
import asyncio
from typing import cast, Iterator, overload, Awaitable, Any, TypeVar
from testutil import assertRaises
def normal_contains_async_def(x: int) -> int:
async def f(y: int) -> int:
return x + y
return 5 + cast(int, asyncio.run(f(6)))
def test_def_contains_async_def() -> None:
assert normal_contains_async_def(3) == 14
async def inc(x: int) -> int:
return x + 1
async def async_def_contains_normal(x: int) -> int:
def nested(y: int, z: int) -> int:
return x + y + z
a = x
a += nested((await inc(3)), (await inc(4)))
return a
async def test_async_def_contains_normal() -> None:
assert await async_def_contains_normal(2) == (2 + 2 + 4 + 5)
async def async_def_contains_async_def(x: int) -> int:
async def f(y: int) -> int:
return (await inc(x)) + (await inc(y))
return (await f(1)) + (await f(2))
async def test_async_def_contains_async_def() -> None:
assert await async_def_contains_async_def(3) == (3 + 1 + 1 + 1) + (3 + 1 + 2 + 1)
async def async_def_contains_generator(x: int) -> tuple[int, int, int]:
def gen(y: int) -> Iterator[int]:
yield x + 1
yield x + y
it = gen(4)
res = x + 10, next(it), next(it)
with assertRaises(StopIteration):
next(it)
return res
async def test_async_def_contains_generator() -> None:
assert await async_def_contains_generator(3) == (13, 4, 7)
def generator_contains_async_def(x: int) -> Iterator[int]:
async def f(y: int) -> int:
return (await inc(x)) + (await inc(y))
yield cast(int, asyncio.run(f(2)))
yield cast(int, asyncio.run(f(3)))
yield x + 10
def test_generator_contains_async_def() -> None:
assert list(generator_contains_async_def(5)) == [6 + 3, 6 + 4, 15]
async def async_def_contains_two_nested_functions(x: int, y: int) -> tuple[int, int]:
def f(a: int) -> int:
return x + a
def g(b: int, c: int) -> int:
return y + b + c
return (await inc(f(3))), (await inc(g(4, 10)))
async def test_async_def_contains_two_nested_functions() -> None:
assert await async_def_contains_two_nested_functions(5, 7) == (
(5 + 3 + 1), (7 + 4 + 10 + 1)
)
async def async_def_contains_overloaded_async_def(n: int) -> int:
@overload
async def f(x: int) -> int: ...
@overload
async def f(x: str) -> str: ...
async def f(x: int | str) -> Any:
return x
return (await f(n)) + 1
async def test_async_def_contains_overloaded_async_def() -> None:
assert await async_def_contains_overloaded_async_def(5) == 6
T = TypeVar("T")
def deco(f: T) -> T:
return f
async def async_def_contains_decorated_async_def(n: int) -> int:
@deco
async def f(x: int) -> int:
return x + 2
return (await f(n)) + 1
async def test_async_def_contains_decorated_async_def() -> None:
assert await async_def_contains_decorated_async_def(7) == 10
[file asyncio/__init__.pyi]
def run(x: object) -> object: ...
[case testAsyncTryFinallyMixedReturn]
# This used to raise an AttributeError, when:
# - the try block contains multiple paths
# - at least one of those explicitly returns
# - at least one of those does not explicitly return
# - the non-returning path is taken at runtime
async def mixed_return(b: bool) -> bool:
try:
if b:
return b
finally:
pass
return b
async def test_async_try_finally_mixed_return() -> None:
# Test return path
result1 = await mixed_return(True)
assert result1 == True
# Test non-return path
result2 = await mixed_return(False)
assert result2 == False
[case testAsyncWithMixedReturn]
# This used to raise an AttributeError, related to
# testAsyncTryFinallyMixedReturn, this is essentially
# a far more extensive version of that test surfacing
# more edge cases
from typing import Optional, Type, Literal
class AsyncContextManager:
async def __aenter__(self) -> "AsyncContextManager":
return self
async def __aexit__(
self,
t: Optional[Type[BaseException]],
v: Optional[BaseException],
tb: object,
) -> Literal[False]:
return False
# Simple async functions (generator class)
async def gen_1(b: bool) -> bool:
async with AsyncContextManager():
if b:
return b
return b
async def gen_2(b: bool) -> bool:
async with AsyncContextManager():
if b:
return b
else:
return b
async def gen_3(b: bool) -> bool:
async with AsyncContextManager():
if b:
return b
else:
pass
return b
async def gen_4(b: bool) -> bool:
ret: bool
async with AsyncContextManager():
if b:
ret = b
else:
ret = b
return ret
async def gen_5(i: int) -> int:
async with AsyncContextManager():
if i == 1:
return i
elif i == 2:
pass
elif i == 3:
return i
return i
async def gen_6(i: int) -> int:
async with AsyncContextManager():
if i == 1:
return i
elif i == 2:
return i
elif i == 3:
return i
return i
async def gen_7(i: int) -> int:
async with AsyncContextManager():
if i == 1:
return i
elif i == 2:
return i
elif i == 3:
return i
else:
return i
# Async functions with nested functions (environment class)
async def env_1(b: bool) -> bool:
def helper() -> bool:
return True
async with AsyncContextManager():
if b:
return helper()
return b
async def env_2(b: bool) -> bool:
def helper() -> bool:
return True
async with AsyncContextManager():
if b:
return helper()
else:
return b
async def env_3(b: bool) -> bool:
def helper() -> bool:
return True
async with AsyncContextManager():
if b:
return helper()
else:
pass
return b
async def env_4(b: bool) -> bool:
def helper() -> bool:
return True
ret: bool
async with AsyncContextManager():
if b:
ret = helper()
else:
ret = b
return ret
async def env_5(i: int) -> int:
def helper() -> int:
return 1
async with AsyncContextManager():
if i == 1:
return helper()
elif i == 2:
pass
elif i == 3:
return i
return i
async def env_6(i: int) -> int:
def helper() -> int:
return 1
async with AsyncContextManager():
if i == 1:
return helper()
elif i == 2:
return i
elif i == 3:
return i
return i
async def env_7(i: int) -> int:
def helper() -> int:
return 1
async with AsyncContextManager():
if i == 1:
return helper()
elif i == 2:
return i
elif i == 3:
return i
else:
return i
async def test_async_with_mixed_return() -> None:
# Test simple async functions (generator class)
# env_1: mixed return/no-return
assert await gen_1(True) is True
assert await gen_1(False) is False
# gen_2: all branches return
assert await gen_2(True) is True
assert await gen_2(False) is False
# gen_3: mixed return/pass
assert await gen_3(True) is True
assert await gen_3(False) is False
# gen_4: no returns in async with
assert await gen_4(True) is True
assert await gen_4(False) is False
# gen_5: multiple branches, some return
assert await gen_5(0) == 0
assert await gen_5(1) == 1
assert await gen_5(2) == 2
assert await gen_5(3) == 3
# gen_6: all explicit branches return, implicit fallthrough
assert await gen_6(0) == 0
assert await gen_6(1) == 1
assert await gen_6(2) == 2
assert await gen_6(3) == 3
# gen_7: all branches return including else
assert await gen_7(0) == 0
assert await gen_7(1) == 1
assert await gen_7(2) == 2
assert await gen_7(3) == 3
# Test async functions with nested functions (environment class)
# env_1: mixed return/no-return
assert await env_1(True) is True
assert await env_1(False) is False
# env_2: all branches return
assert await env_2(True) is True
assert await env_2(False) is False
# env_3: mixed return/pass
assert await env_3(True) is True
assert await env_3(False) is False
# env_4: no returns in async with
assert await env_4(True) is True
assert await env_4(False) is False
# env_5: multiple branches, some return
assert await env_5(0) == 0
assert await env_5(1) == 1
assert await env_5(2) == 2
assert await env_5(3) == 3
# env_6: all explicit branches return, implicit fallthrough
assert await env_6(0) == 0
assert await env_6(1) == 1
assert await env_6(2) == 2
assert await env_6(3) == 3
# env_7: all branches return including else
assert await env_7(0) == 0
assert await env_7(1) == 1
assert await env_7(2) == 2