Skip to content

Commit da4b214

Browse files
bpo-42413: Replace concurrent.futures.TimeoutError and asyncio.TimeoutError with builtin TimeoutError (GH-30197)
Co-authored-by: Andrew Svetlov <[email protected]>
1 parent 9b52920 commit da4b214

File tree

7 files changed

+26
-25
lines changed

7 files changed

+26
-25
lines changed

Doc/library/asyncio-api-index.rst

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -203,11 +203,6 @@ Exceptions
203203
:class: full-width-table
204204

205205

206-
* - :exc:`asyncio.TimeoutError`
207-
- Raised on timeout by functions like :func:`wait_for`.
208-
Keep in mind that ``asyncio.TimeoutError`` is **unrelated**
209-
to the built-in :exc:`TimeoutError` exception.
210-
211206
* - :exc:`asyncio.CancelledError`
212207
- Raised when a Task is cancelled. See also :meth:`Task.cancel`.
213208

Doc/library/asyncio-exceptions.rst

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,12 @@ Exceptions
1313

1414
.. exception:: TimeoutError
1515

16-
The operation has exceeded the given deadline.
16+
A deprecated alias of :exc:`TimeoutError`,
17+
raised when the operation has exceeded the given deadline.
1718

18-
.. important::
19-
This exception is different from the builtin :exc:`TimeoutError`
20-
exception.
19+
.. versionchanged:: 3.11
20+
21+
This class was made an alias of :exc:`TimeoutError`.
2122

2223

2324
.. exception:: CancelledError

Doc/library/asyncio-task.rst

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@ Timeouts
490490
completes.
491491

492492
If a timeout occurs, it cancels the task and raises
493-
:exc:`asyncio.TimeoutError`.
493+
:exc:`TimeoutError`.
494494

495495
To avoid the task :meth:`cancellation <Task.cancel>`,
496496
wrap it in :func:`shield`.
@@ -520,7 +520,7 @@ Timeouts
520520
# Wait for at most 1 second
521521
try:
522522
await asyncio.wait_for(eternity(), timeout=1.0)
523-
except asyncio.TimeoutError:
523+
except TimeoutError:
524524
print('timeout!')
525525

526526
asyncio.run(main())
@@ -532,7 +532,7 @@ Timeouts
532532
.. versionchanged:: 3.7
533533
When *aw* is cancelled due to a timeout, ``wait_for`` waits
534534
for *aw* to be cancelled. Previously, it raised
535-
:exc:`asyncio.TimeoutError` immediately.
535+
:exc:`TimeoutError` immediately.
536536

537537
.. deprecated-removed:: 3.8 3.10
538538
The ``loop`` parameter. This function has been implicitly getting the
@@ -561,7 +561,7 @@ Waiting Primitives
561561
*timeout* (a float or int), if specified, can be used to control
562562
the maximum number of seconds to wait before returning.
563563

564-
Note that this function does not raise :exc:`asyncio.TimeoutError`.
564+
Note that this function does not raise :exc:`TimeoutError`.
565565
Futures or Tasks that aren't done when the timeout occurs are simply
566566
returned in the second set.
567567

@@ -649,7 +649,7 @@ Waiting Primitives
649649
Each coroutine returned can be awaited to get the earliest next
650650
result from the iterable of the remaining awaitables.
651651

652-
Raises :exc:`asyncio.TimeoutError` if the timeout occurs before
652+
Raises :exc:`TimeoutError` if the timeout occurs before
653653
all Futures are done.
654654

655655
.. deprecated-removed:: 3.8 3.10
@@ -762,7 +762,7 @@ Scheduling From Other Threads
762762

763763
try:
764764
result = future.result(timeout)
765-
except concurrent.futures.TimeoutError:
765+
except TimeoutError:
766766
print('The coroutine took too long, cancelling the task...')
767767
future.cancel()
768768
except Exception as exc:

Doc/library/concurrent.futures.rst

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ Executor Objects
4747
* *func* is executed asynchronously and several calls to
4848
*func* may be made concurrently.
4949

50-
The returned iterator raises a :exc:`concurrent.futures.TimeoutError`
50+
The returned iterator raises a :exc:`TimeoutError`
5151
if :meth:`~iterator.__next__` is called and the result isn't available
5252
after *timeout* seconds from the original call to :meth:`Executor.map`.
5353
*timeout* can be an int or a float. If *timeout* is not specified or
@@ -352,7 +352,7 @@ The :class:`Future` class encapsulates the asynchronous execution of a callable.
352352
Return the value returned by the call. If the call hasn't yet completed
353353
then this method will wait up to *timeout* seconds. If the call hasn't
354354
completed in *timeout* seconds, then a
355-
:exc:`concurrent.futures.TimeoutError` will be raised. *timeout* can be
355+
:exc:`TimeoutError` will be raised. *timeout* can be
356356
an int or float. If *timeout* is not specified or ``None``, there is no
357357
limit to the wait time.
358358

@@ -366,7 +366,7 @@ The :class:`Future` class encapsulates the asynchronous execution of a callable.
366366
Return the exception raised by the call. If the call hasn't yet
367367
completed then this method will wait up to *timeout* seconds. If the
368368
call hasn't completed in *timeout* seconds, then a
369-
:exc:`concurrent.futures.TimeoutError` will be raised. *timeout* can be
369+
:exc:`TimeoutError` will be raised. *timeout* can be
370370
an int or float. If *timeout* is not specified or ``None``, there is no
371371
limit to the wait time.
372372

@@ -482,7 +482,7 @@ Module Functions
482482
they complete (finished or cancelled futures). Any futures given by *fs* that
483483
are duplicated will be returned once. Any futures that completed before
484484
:func:`as_completed` is called will be yielded first. The returned iterator
485-
raises a :exc:`concurrent.futures.TimeoutError` if :meth:`~iterator.__next__`
485+
raises a :exc:`TimeoutError` if :meth:`~iterator.__next__`
486486
is called and the result isn't available after *timeout* seconds from the
487487
original call to :func:`as_completed`. *timeout* can be an int or float. If
488488
*timeout* is not specified or ``None``, there is no limit to the wait time.
@@ -506,7 +506,13 @@ Exception classes
506506

507507
.. exception:: TimeoutError
508508

509-
Raised when a future operation exceeds the given timeout.
509+
A deprecated alias of :exc:`TimeoutError`,
510+
raised when a future operation exceeds the given timeout.
511+
512+
.. versionchanged:: 3.11
513+
514+
This class was made an alias of :exc:`TimeoutError`.
515+
510516

511517
.. exception:: BrokenExecutor
512518

Lib/asyncio/exceptions.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ class CancelledError(BaseException):
1010
"""The Future or Task was cancelled."""
1111

1212

13-
class TimeoutError(Exception):
14-
"""The operation exceeded the given deadline."""
13+
TimeoutError = TimeoutError # make local alias for the standard exception
1514

1615

1716
class InvalidStateError(Exception):

Lib/concurrent/futures/_base.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,7 @@ class CancelledError(Error):
5050
"""The Future was cancelled."""
5151
pass
5252

53-
class TimeoutError(Error):
54-
"""The operation exceeded the given deadline."""
55-
pass
53+
TimeoutError = TimeoutError # make local alias for the standard exception
5654

5755
class InvalidStateError(Error):
5856
"""The operation is not allowed in this state."""
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Replace ``concurrent.futures.TimeoutError`` and ``asyncio.TimeoutError``
2+
with builtin :exc:`TimeoutError`, keep these names as deprecated aliases.

0 commit comments

Comments
 (0)