@@ -1577,6 +1577,42 @@ async def run_tasks():
15771577
15781578 asyncio .run (run_tasks ())
15791579
1580+ @unittest .skipIf (not sys .flags .context_aware_warnings ,
1581+ "requires context aware warnings" )
1582+ def test_async_task_inherit (self ):
1583+ """Check that a new asyncio task inherits warnings context from the
1584+ coroutine that spawns it.
1585+ """
1586+ import asyncio
1587+
1588+ step1 = asyncio .Event ()
1589+ step2 = asyncio .Event ()
1590+
1591+ async def run_child1 ():
1592+ await step1 .wait ()
1593+ # This should be recorded by the run_parent() catch_warnings
1594+ # context.
1595+ self .module .warn ('child warning' , UserWarning )
1596+ step2 .set ()
1597+
1598+ async def run_child2 ():
1599+ # This establishes a new catch_warnings() context. The
1600+ # run_child1() task should still be using the context from
1601+ # run_parent() if context-aware warnings are enabled.
1602+ with self .module .catch_warnings (record = True ) as w :
1603+ step1 .set ()
1604+ await step2 .wait ()
1605+
1606+ async def run_parent ():
1607+ with self .module .catch_warnings (record = True ) as w :
1608+ child1_task = asyncio .create_task (run_child1 ())
1609+ child2_task = asyncio .create_task (run_child2 ())
1610+ await step2 .wait ()
1611+ self .assertEqual (len (w ), 1 )
1612+ self .assertEqual (w [0 ].message .args [0 ], 'child warning' )
1613+
1614+ asyncio .run (run_parent ())
1615+
15801616
15811617class CAsyncTests (AsyncTests , unittest .TestCase ):
15821618 module = c_warnings
0 commit comments