Skip to content

Commit 09de8d7

Browse files
authored
GH-90985: Revert "Deprecate passing a message into cancel()" (#97999)
Reason: we were too hasty in deprecating this. We shouldn't deprecate it before we have a replacement.
1 parent c46a423 commit 09de8d7

File tree

8 files changed

+12
-102
lines changed

8 files changed

+12
-102
lines changed

Doc/library/asyncio-future.rst

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -197,11 +197,6 @@ Future Object
197197
.. versionchanged:: 3.9
198198
Added the *msg* parameter.
199199

200-
.. deprecated-removed:: 3.11 3.14
201-
*msg* parameter is ambiguous when multiple :meth:`cancel`
202-
are called with different cancellation messages.
203-
The argument will be removed.
204-
205200
.. method:: exception()
206201

207202
Return the exception that was set on this Future.
@@ -282,8 +277,3 @@ the Future has a result::
282277

283278
- :meth:`asyncio.Future.cancel` accepts an optional ``msg`` argument,
284279
but :func:`concurrent.futures.cancel` does not.
285-
286-
.. deprecated-removed:: 3.11 3.14
287-
*msg* parameter is ambiguous when multiple :meth:`cancel`
288-
are called with different cancellation messages.
289-
The argument will be removed.

Doc/library/asyncio-task.rst

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1144,10 +1144,8 @@ Task Object
11441144
.. versionchanged:: 3.9
11451145
Added the *msg* parameter.
11461146

1147-
.. deprecated-removed:: 3.11 3.14
1148-
*msg* parameter is ambiguous when multiple :meth:`cancel`
1149-
are called with different cancellation messages.
1150-
The argument will be removed.
1147+
.. versionchanged:: 3.11
1148+
The ``msg`` parameter is propagated from cancelled task to its awaiter.
11511149

11521150
.. _asyncio_example_task_cancel:
11531151

Lib/asyncio/futures.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import contextvars
99
import logging
1010
import sys
11-
import warnings
1211
from types import GenericAlias
1312

1413
from . import base_futures
@@ -151,11 +150,6 @@ def cancel(self, msg=None):
151150
change the future's state to cancelled, schedule the callbacks and
152151
return True.
153152
"""
154-
if msg is not None:
155-
warnings.warn("Passing 'msg' argument to Future.cancel() "
156-
"is deprecated since Python 3.11, and "
157-
"scheduled for removal in Python 3.14.",
158-
DeprecationWarning, stacklevel=2)
159153
self.__log_traceback = False
160154
if self._state != _PENDING:
161155
return False

Lib/asyncio/tasks.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -210,11 +210,6 @@ def cancel(self, msg=None):
210210
211211
This also increases the task's count of cancellation requests.
212212
"""
213-
if msg is not None:
214-
warnings.warn("Passing 'msg' argument to Task.cancel() "
215-
"is deprecated since Python 3.11, and "
216-
"scheduled for removal in Python 3.14.",
217-
DeprecationWarning, stacklevel=2)
218213
self._log_traceback = False
219214
if self.done():
220215
return False

Lib/test/test_asyncio/test_futures.py

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -229,22 +229,14 @@ def test_future_cancel_message_getter(self):
229229
self.assertTrue(hasattr(f, '_cancel_message'))
230230
self.assertEqual(f._cancel_message, None)
231231

232-
with self.assertWarnsRegex(
233-
DeprecationWarning,
234-
"Passing 'msg' argument"
235-
):
236-
f.cancel('my message')
232+
f.cancel('my message')
237233
with self.assertRaises(asyncio.CancelledError):
238234
self.loop.run_until_complete(f)
239235
self.assertEqual(f._cancel_message, 'my message')
240236

241237
def test_future_cancel_message_setter(self):
242238
f = self._new_future(loop=self.loop)
243-
with self.assertWarnsRegex(
244-
DeprecationWarning,
245-
"Passing 'msg' argument"
246-
):
247-
f.cancel('my message')
239+
f.cancel('my message')
248240
f._cancel_message = 'my new message'
249241
self.assertEqual(f._cancel_message, 'my new message')
250242

Lib/test/test_asyncio/test_tasks.py

Lines changed: 7 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,7 @@ async def coro():
109109
self.assertTrue(hasattr(t, '_cancel_message'))
110110
self.assertEqual(t._cancel_message, None)
111111

112-
with self.assertWarnsRegex(
113-
DeprecationWarning,
114-
"Passing 'msg' argument"
115-
):
116-
t.cancel('my message')
112+
t.cancel('my message')
117113
self.assertEqual(t._cancel_message, 'my message')
118114

119115
with self.assertRaises(asyncio.CancelledError) as cm:
@@ -125,11 +121,7 @@ def test_task_cancel_message_setter(self):
125121
async def coro():
126122
pass
127123
t = self.new_task(self.loop, coro())
128-
with self.assertWarnsRegex(
129-
DeprecationWarning,
130-
"Passing 'msg' argument"
131-
):
132-
t.cancel('my message')
124+
t.cancel('my message')
133125
t._cancel_message = 'my new message'
134126
self.assertEqual(t._cancel_message, 'my new message')
135127

@@ -706,14 +698,7 @@ async def sleep():
706698
async def coro():
707699
task = self.new_task(loop, sleep())
708700
await asyncio.sleep(0)
709-
if cancel_args not in ((), (None,)):
710-
with self.assertWarnsRegex(
711-
DeprecationWarning,
712-
"Passing 'msg' argument"
713-
):
714-
task.cancel(*cancel_args)
715-
else:
716-
task.cancel(*cancel_args)
701+
task.cancel(*cancel_args)
717702
done, pending = await asyncio.wait([task])
718703
task.result()
719704

@@ -747,14 +732,7 @@ async def sleep():
747732
async def coro():
748733
task = self.new_task(loop, sleep())
749734
await asyncio.sleep(0)
750-
if cancel_args not in ((), (None,)):
751-
with self.assertWarnsRegex(
752-
DeprecationWarning,
753-
"Passing 'msg' argument"
754-
):
755-
task.cancel(*cancel_args)
756-
else:
757-
task.cancel(*cancel_args)
735+
task.cancel(*cancel_args)
758736
done, pending = await asyncio.wait([task])
759737
task.exception()
760738

@@ -777,17 +755,10 @@ async def sleep():
777755
fut.set_result(None)
778756
await asyncio.sleep(10)
779757

780-
def cancel(task, msg):
781-
with self.assertWarnsRegex(
782-
DeprecationWarning,
783-
"Passing 'msg' argument"
784-
):
785-
task.cancel(msg)
786-
787758
async def coro():
788759
inner_task = self.new_task(loop, sleep())
789760
await fut
790-
loop.call_soon(cancel, inner_task, 'msg')
761+
loop.call_soon(inner_task.cancel, 'msg')
791762
try:
792763
await inner_task
793764
except asyncio.CancelledError as ex:
@@ -813,11 +784,7 @@ async def sleep():
813784
async def coro():
814785
task = self.new_task(loop, sleep())
815786
# We deliberately leave out the sleep here.
816-
with self.assertWarnsRegex(
817-
DeprecationWarning,
818-
"Passing 'msg' argument"
819-
):
820-
task.cancel('my message')
787+
task.cancel('my message')
821788
done, pending = await asyncio.wait([task])
822789
task.exception()
823790

@@ -2179,14 +2146,7 @@ async def test():
21792146
async def main():
21802147
qwe = self.new_task(loop, test())
21812148
await asyncio.sleep(0.2)
2182-
if cancel_args not in ((), (None,)):
2183-
with self.assertWarnsRegex(
2184-
DeprecationWarning,
2185-
"Passing 'msg' argument"
2186-
):
2187-
qwe.cancel(*cancel_args)
2188-
else:
2189-
qwe.cancel(*cancel_args)
2149+
qwe.cancel(*cancel_args)
21902150
await qwe
21912151

21922152
try:
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Earlier in 3.11 we deprecated ``asyncio.Task.cancel("message")``. We realized we were too harsh, and have undeprecated it.

Modules/_asynciomodule.c

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1116,16 +1116,6 @@ static PyObject *
11161116
_asyncio_Future_cancel_impl(FutureObj *self, PyObject *msg)
11171117
/*[clinic end generated code: output=3edebbc668e5aba3 input=925eb545251f2c5a]*/
11181118
{
1119-
if (msg != Py_None) {
1120-
if (PyErr_WarnEx(PyExc_DeprecationWarning,
1121-
"Passing 'msg' argument to Future.cancel() "
1122-
"is deprecated since Python 3.11, and "
1123-
"scheduled for removal in Python 3.14.",
1124-
2))
1125-
{
1126-
return NULL;
1127-
}
1128-
}
11291119
ENSURE_FUTURE_ALIVE(self)
11301120
return future_cancel(self, msg);
11311121
}
@@ -2214,16 +2204,6 @@ static PyObject *
22142204
_asyncio_Task_cancel_impl(TaskObj *self, PyObject *msg)
22152205
/*[clinic end generated code: output=c66b60d41c74f9f1 input=7bb51bf25974c783]*/
22162206
{
2217-
if (msg != Py_None) {
2218-
if (PyErr_WarnEx(PyExc_DeprecationWarning,
2219-
"Passing 'msg' argument to Task.cancel() "
2220-
"is deprecated since Python 3.11, and "
2221-
"scheduled for removal in Python 3.14.",
2222-
2))
2223-
{
2224-
return NULL;
2225-
}
2226-
}
22272207
self->task_log_tb = 0;
22282208

22292209
if (self->task_state != STATE_PENDING) {

0 commit comments

Comments
 (0)