gh-128559: Remove typing import from asyncio.timeouts (#128560)

This commit is contained in:
Michael H 2025-01-06 18:03:27 -05:00 committed by GitHub
parent 61c1a2478e
commit 7363476b64
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 12 additions and 13 deletions

View file

@ -1,7 +1,6 @@
import enum import enum
from types import TracebackType from types import TracebackType
from typing import final, Optional, Type
from . import events from . import events
from . import exceptions from . import exceptions
@ -23,14 +22,13 @@ class _State(enum.Enum):
EXITED = "finished" EXITED = "finished"
@final
class Timeout: class Timeout:
"""Asynchronous context manager for cancelling overdue coroutines. """Asynchronous context manager for cancelling overdue coroutines.
Use `timeout()` or `timeout_at()` rather than instantiating this class directly. Use `timeout()` or `timeout_at()` rather than instantiating this class directly.
""" """
def __init__(self, when: Optional[float]) -> None: def __init__(self, when: float | None) -> None:
"""Schedule a timeout that will trigger at a given loop time. """Schedule a timeout that will trigger at a given loop time.
- If `when` is `None`, the timeout will never trigger. - If `when` is `None`, the timeout will never trigger.
@ -39,15 +37,15 @@ class Timeout:
""" """
self._state = _State.CREATED self._state = _State.CREATED
self._timeout_handler: Optional[events.TimerHandle] = None self._timeout_handler: events.TimerHandle | None = None
self._task: Optional[tasks.Task] = None self._task: tasks.Task | None = None
self._when = when self._when = when
def when(self) -> Optional[float]: def when(self) -> float | None:
"""Return the current deadline.""" """Return the current deadline."""
return self._when return self._when
def reschedule(self, when: Optional[float]) -> None: def reschedule(self, when: float | None) -> None:
"""Reschedule the timeout.""" """Reschedule the timeout."""
if self._state is not _State.ENTERED: if self._state is not _State.ENTERED:
if self._state is _State.CREATED: if self._state is _State.CREATED:
@ -96,10 +94,10 @@ class Timeout:
async def __aexit__( async def __aexit__(
self, self,
exc_type: Optional[Type[BaseException]], exc_type: type[BaseException] | None,
exc_val: Optional[BaseException], exc_val: BaseException | None,
exc_tb: Optional[TracebackType], exc_tb: TracebackType | None,
) -> Optional[bool]: ) -> bool | None:
assert self._state in (_State.ENTERED, _State.EXPIRING) assert self._state in (_State.ENTERED, _State.EXPIRING)
if self._timeout_handler is not None: if self._timeout_handler is not None:
@ -142,7 +140,7 @@ class Timeout:
exc_val = exc_val.__context__ exc_val = exc_val.__context__
def timeout(delay: Optional[float]) -> Timeout: def timeout(delay: float | None) -> Timeout:
"""Timeout async context manager. """Timeout async context manager.
Useful in cases when you want to apply timeout logic around block Useful in cases when you want to apply timeout logic around block
@ -162,7 +160,7 @@ def timeout(delay: Optional[float]) -> Timeout:
return Timeout(loop.time() + delay if delay is not None else None) return Timeout(loop.time() + delay if delay is not None else None)
def timeout_at(when: Optional[float]) -> Timeout: def timeout_at(when: float | None) -> Timeout:
"""Schedule the timeout at absolute time. """Schedule the timeout at absolute time.
Like timeout() but argument gives absolute time in the same clock system Like timeout() but argument gives absolute time in the same clock system

View file

@ -0,0 +1 @@
Improved import time of :mod:`asyncio`.