[3.14] GH-135171: Revert async generator expressions behavior (#135352)

This commit is contained in:
Mikhail Efimov 2025-06-16 17:45:42 +03:00 committed by GitHub
parent 0eec8ddac3
commit 2ef78a85e4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 48 additions and 16 deletions

View file

@ -1835,6 +1835,20 @@ async def run():
res = self.loop.run_until_complete(run())
self.assertEqual(res, [i * 2 for i in range(1, 10)])
def test_async_gen_expression_incorrect(self):
async def ag():
yield 42
async def run(arg):
(x async for x in arg)
err_msg_async = "'async for' requires an object with " \
"__aiter__ method, got .*"
self.loop.run_until_complete(run(ag()))
with self.assertRaisesRegex(TypeError, err_msg_async):
self.loop.run_until_complete(run(None))
def test_asyncgen_nonstarted_hooks_are_cancellable(self):
# See https://bugs.python.org/issue38013
messages = []

View file

@ -2267,7 +2267,7 @@ def c():
def test_call_aiter_once_in_comprehension(self):
class Iterator:
class AsyncIterator:
def __init__(self):
self.val = 0
@ -2283,12 +2283,17 @@ async def __anext__(self):
class C:
def __aiter__(self):
return Iterator()
return AsyncIterator()
async def run():
async def run_listcomp():
return [i async for i in C()]
self.assertEqual(run_async(run()), ([], [1,2]))
async def run_asyncgen():
ag = (i async for i in C())
return [i async for i in ag]
self.assertEqual(run_async(run_listcomp()), ([], [1, 2]))
self.assertEqual(run_async(run_asyncgen()), ([], [1, 2]))
@unittest.skipIf(

View file

@ -288,7 +288,7 @@ class C:
def __iter__(self):
return Iterator()
self.assertEqual([1,2], list(i for i in C()))
self.assertEqual([1, 2], list(i for i in C()))
class ModifyUnderlyingIterableTest(unittest.TestCase):

View file

@ -770,7 +770,7 @@ class C:
def __iter__(self):
return Iterator()
self.assertEqual([1,2], [i for i in C()])
self.assertEqual([1, 2], [i for i in C()])
__test__ = {'doctests' : doctests}