GH-89091: raise RuntimeWarning for unawaited async generator methods (#104611)

This commit is contained in:
Kumar Aditya 2023-05-26 16:53:29 +05:30 committed by GitHub
parent 46857d0b2a
commit 7fc542c88d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 96 additions and 2 deletions

View file

@ -415,8 +415,9 @@ class AsyncGenTest(unittest.TestCase):
self.assertIsInstance(g.ag_frame, types.FrameType)
self.assertFalse(g.ag_running)
self.assertIsInstance(g.ag_code, types.CodeType)
self.assertTrue(inspect.isawaitable(g.aclose()))
aclose = g.aclose()
self.assertTrue(inspect.isawaitable(aclose))
aclose.close()
class AsyncGenAsyncioTest(unittest.TestCase):
@ -1693,5 +1694,38 @@ class AsyncGenAsyncioTest(unittest.TestCase):
self.loop.run_until_complete(run())
class TestUnawaitedWarnings(unittest.TestCase):
def test_asend(self):
async def gen():
yield 1
msg = f"coroutine method 'asend' of '{gen.__qualname__}' was never awaited"
with self.assertWarnsRegex(RuntimeWarning, msg):
g = gen()
g.asend(None)
gc_collect()
def test_athrow(self):
async def gen():
yield 1
msg = f"coroutine method 'athrow' of '{gen.__qualname__}' was never awaited"
with self.assertWarnsRegex(RuntimeWarning, msg):
g = gen()
g.athrow(RuntimeError)
gc_collect()
def test_aclose(self):
async def gen():
yield 1
msg = f"coroutine method 'aclose' of '{gen.__qualname__}' was never awaited"
with self.assertWarnsRegex(RuntimeWarning, msg):
g = gen()
g.aclose()
gc_collect()
if __name__ == "__main__":
unittest.main()