diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2021-07-13 21:27:50 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-07-13 21:27:50 (GMT) |
commit | 054e9c84ac7c394941bba3ea1829d14dce1243fc (patch) | |
tree | edd3cbe6ed6083909280d8aaed50c8d2679b4fde /Lib/test/test_coroutines.py | |
parent | 0ee0a740e12ec8568aafa033aa6bb08b265afe26 (diff) | |
download | cpython-054e9c84ac7c394941bba3ea1829d14dce1243fc.zip cpython-054e9c84ac7c394941bba3ea1829d14dce1243fc.tar.gz cpython-054e9c84ac7c394941bba3ea1829d14dce1243fc.tar.bz2 |
bpo-33346: Allow async comprehensions inside implicit async comprehensions (GH-6766)
Co-authored-by: Pablo Galindo <pablogsal@gmail.com>
Diffstat (limited to 'Lib/test/test_coroutines.py')
-rw-r--r-- | Lib/test/test_coroutines.py | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/Lib/test/test_coroutines.py b/Lib/test/test_coroutines.py index 9b83244..4350e18 100644 --- a/Lib/test/test_coroutines.py +++ b/Lib/test/test_coroutines.py @@ -28,6 +28,12 @@ class AsyncYield: yield self.value +async def asynciter(iterable): + """Convert an iterable to an asynchronous iterator.""" + for x in iterable: + yield x + + def run_async(coro): assert coro.__class__ in {types.GeneratorType, types.CoroutineType} @@ -127,6 +133,11 @@ class AsyncBadSyntaxTest(unittest.TestCase): """async def foo(): def bar(): + [[async for i in b] for b in els] + """, + + """async def foo(): + def bar(): [i for i in els for b in await els] """, @@ -200,6 +211,13 @@ class AsyncBadSyntaxTest(unittest.TestCase): [i for i in els if await i] """, + """def bar(): + [[i async for i in a] for a in elts] + """, + + """[[i async for i in a] for a in elts] + """, + """async def foo(): await """, @@ -2011,6 +2029,60 @@ class CoroutineTest(unittest.TestCase): run_async(f()), ([], {1: 1, 2: 2, 3: 3})) + def test_nested_comp(self): + async def run_list_inside_list(): + return [[i + j async for i in asynciter([1, 2])] for j in [10, 20]] + self.assertEqual( + run_async(run_list_inside_list()), + ([], [[11, 12], [21, 22]])) + + async def run_set_inside_list(): + return [{i + j async for i in asynciter([1, 2])} for j in [10, 20]] + self.assertEqual( + run_async(run_set_inside_list()), + ([], [{11, 12}, {21, 22}])) + + async def run_list_inside_set(): + return {sum([i async for i in asynciter(range(j))]) for j in [3, 5]} + self.assertEqual( + run_async(run_list_inside_set()), + ([], {3, 10})) + + async def run_dict_inside_dict(): + return {j: {i: i + j async for i in asynciter([1, 2])} for j in [10, 20]} + self.assertEqual( + run_async(run_dict_inside_dict()), + ([], {10: {1: 11, 2: 12}, 20: {1: 21, 2: 22}})) + + async def run_list_inside_gen(): + gen = ([i + j async for i in asynciter([1, 2])] for j in [10, 20]) + return [x async for x in gen] + self.assertEqual( + run_async(run_list_inside_gen()), + ([], [[11, 12], [21, 22]])) + + async def run_gen_inside_list(): + gens = [(i async for i in asynciter(range(j))) for j in [3, 5]] + return [x for g in gens async for x in g] + self.assertEqual( + run_async(run_gen_inside_list()), + ([], [0, 1, 2, 0, 1, 2, 3, 4])) + + async def run_gen_inside_gen(): + gens = ((i async for i in asynciter(range(j))) for j in [3, 5]) + return [x for g in gens async for x in g] + self.assertEqual( + run_async(run_gen_inside_gen()), + ([], [0, 1, 2, 0, 1, 2, 3, 4])) + + async def run_list_inside_list_inside_list(): + return [[[i + j + k async for i in asynciter([1, 2])] + for j in [10, 20]] + for k in [100, 200]] + self.assertEqual( + run_async(run_list_inside_list_inside_list()), + ([], [[[111, 112], [121, 122]], [[211, 212], [221, 222]]])) + def test_copy(self): async def func(): pass coro = func() |