Skip to content

Commit 7d28f24

Browse files
committed
gh-155894: Fix wait_for() docs claiming a coroutine is wrapped in a Task
1 parent c144799 commit 7d28f24

2 files changed

Lines changed: 14 additions & 10 deletions

File tree

Doc/library/asyncio-task.rst

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -843,17 +843,16 @@ Timeouts
843843
Wait for the *fut* :ref:`awaitable <asyncio-awaitables>`
844844
to complete with a timeout.
845845

846-
If *fut* is a coroutine it is automatically scheduled as a Task.
846+
If *fut* is a coroutine, it is awaited directly rather than being wrapped
847+
in a :class:`Task`, unless *timeout* is zero or negative.
847848

848849
*timeout* can either be ``None`` or a float or int number of seconds
849850
to wait for. If *timeout* is ``None``, block until the future
850851
completes.
851852

852-
If a timeout occurs, it cancels the task and raises
853-
:exc:`TimeoutError`.
853+
If a timeout occurs, it cancels *fut* and raises :exc:`TimeoutError`.
854854

855-
To avoid the task :meth:`cancellation <Task.cancel>`,
856-
wrap it in :func:`shield`.
855+
To prevent *fut* from being cancelled, wrap it in :func:`shield`.
857856

858857
The function will wait until the future is actually cancelled,
859858
so the total wait time may exceed the *timeout*. If an exception
@@ -894,6 +893,10 @@ Timeouts
894893
.. versionchanged:: 3.11
895894
Raises :exc:`TimeoutError` instead of :exc:`asyncio.TimeoutError`.
896895

896+
.. versionchanged:: 3.12
897+
Implemented using :func:`asyncio.timeout`, a coroutine passed as *fut*
898+
is no longer wrapped in a :class:`Task` when *timeout* is positive.
899+
897900

898901
Waiting primitives
899902
==================

Lib/asyncio/tasks.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -440,15 +440,16 @@ def _release_waiter(waiter, *args):
440440
async def wait_for(fut, timeout):
441441
"""Wait for the single Future or coroutine to complete, with timeout.
442442
443-
Coroutine will be wrapped in Task.
443+
A coroutine is awaited directly rather than being wrapped in a Task,
444+
unless timeout is zero or negative.
444445
445446
Returns result of the Future or coroutine. When a timeout occurs,
446-
it cancels the task and raises TimeoutError. To avoid the task
447-
cancellation, wrap it in shield().
447+
it cancels fut and raises TimeoutError. To prevent fut from being
448+
cancelled, wrap it in shield().
448449
449-
If the wait is cancelled, the task is also cancelled.
450+
If the wait is cancelled, fut is also cancelled.
450451
451-
If the task suppresses the cancellation and returns a value instead,
452+
If fut suppresses the cancellation and returns a value instead,
452453
that value is returned.
453454
454455
This function is a coroutine.

0 commit comments

Comments
 (0)