bpo-46829: Deprecate passing a message into Future.cancel() and Task.cancel() (GH-31840)

After a long deliberation we ended up feeling that the message argument for Future.cancel(), added in 3.9, was a bad idea, so we're deprecating it in 3.11 and plan to remove it in 3.13.
This commit is contained in:
Andrew Svetlov 2022-03-23 17:43:05 +02:00 committed by GitHub
parent 624e3986fb
commit 0360e9f346
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 106 additions and 17 deletions

View file

@ -113,7 +113,11 @@ class BaseTaskTests:
self.assertTrue(hasattr(t, '_cancel_message'))
self.assertEqual(t._cancel_message, None)
t.cancel('my message')
with self.assertWarnsRegex(
DeprecationWarning,
"Passing 'msg' argument"
):
t.cancel('my message')
self.assertEqual(t._cancel_message, 'my message')
with self.assertRaises(asyncio.CancelledError) as cm:
@ -125,7 +129,11 @@ class BaseTaskTests:
async def coro():
pass
t = self.new_task(self.loop, coro())
t.cancel('my message')
with self.assertWarnsRegex(
DeprecationWarning,
"Passing 'msg' argument"
):
t.cancel('my message')
t._cancel_message = 'my new message'
self.assertEqual(t._cancel_message, 'my new message')
@ -582,7 +590,14 @@ class BaseTaskTests:
async def coro():
task = self.new_task(loop, sleep())
await asyncio.sleep(0)
task.cancel(*cancel_args)
if cancel_args not in ((), (None,)):
with self.assertWarnsRegex(
DeprecationWarning,
"Passing 'msg' argument"
):
task.cancel(*cancel_args)
else:
task.cancel(*cancel_args)
done, pending = await asyncio.wait([task])
task.result()
@ -616,7 +631,14 @@ class BaseTaskTests:
async def coro():
task = self.new_task(loop, sleep())
await asyncio.sleep(0)
task.cancel(*cancel_args)
if cancel_args not in ((), (None,)):
with self.assertWarnsRegex(
DeprecationWarning,
"Passing 'msg' argument"
):
task.cancel(*cancel_args)
else:
task.cancel(*cancel_args)
done, pending = await asyncio.wait([task])
task.exception()
@ -639,10 +661,17 @@ class BaseTaskTests:
fut.set_result(None)
await asyncio.sleep(10)
def cancel(task, msg):
with self.assertWarnsRegex(
DeprecationWarning,
"Passing 'msg' argument"
):
task.cancel(msg)
async def coro():
inner_task = self.new_task(loop, sleep())
await fut
loop.call_soon(inner_task.cancel, 'msg')
loop.call_soon(cancel, inner_task, 'msg')
try:
await inner_task
except asyncio.CancelledError as ex:
@ -668,7 +697,11 @@ class BaseTaskTests:
async def coro():
task = self.new_task(loop, sleep())
# We deliberately leave out the sleep here.
task.cancel('my message')
with self.assertWarnsRegex(
DeprecationWarning,
"Passing 'msg' argument"
):
task.cancel('my message')
done, pending = await asyncio.wait([task])
task.exception()
@ -2029,7 +2062,14 @@ class BaseTaskTests:
async def main():
qwe = self.new_task(loop, test())
await asyncio.sleep(0.2)
qwe.cancel(*cancel_args)
if cancel_args not in ((), (None,)):
with self.assertWarnsRegex(
DeprecationWarning,
"Passing 'msg' argument"
):
qwe.cancel(*cancel_args)
else:
qwe.cancel(*cancel_args)
await qwe
try: