Skip to content

Commit

Permalink
Bump asyncio_timeout to 4.0.3.
Browse files Browse the repository at this point in the history
This makes type checking pass again.
  • Loading branch information
aaugustin committed Apr 16, 2024
1 parent 0fdc694 commit d4aabe5
Showing 1 changed file with 28 additions and 14 deletions.
42 changes: 28 additions & 14 deletions src/websockets/legacy/async_timeout.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
from typing import Optional, Type


# From https://github.com/python/typing_extensions/blob/main/src/typing_extensions.py
# Licensed under the Python Software Foundation License (PSF-2.0)

if sys.version_info >= (3, 11):
from typing import final
else:
# From https://github.com/python/typing_extensions/blob/main/src/typing_extensions.py
# Licensed under the Python Software Foundation License (PSF-2.0)

# @final exists in 3.8+, but we backport it for all versions
# before 3.11 to keep support for the __final__ attribute.
# See https://bugs.python.org/issue46342
Expand Down Expand Up @@ -49,10 +49,21 @@ class Other(Leaf): # Error reported by type checker
pass
return f

# End https://github.com/python/typing_extensions/blob/main/src/typing_extensions.py


# End https://github.com/aio-libs/async-timeout/blob/master/async_timeout/__init__.py
if sys.version_info >= (3, 11):

def _uncancel_task(task: "asyncio.Task[object]") -> None:
task.uncancel()

__version__ = "4.0.2"
else:

def _uncancel_task(task: "asyncio.Task[object]") -> None:
pass


__version__ = "4.0.3"


__all__ = ("timeout", "timeout_at", "Timeout")
Expand Down Expand Up @@ -124,14 +135,15 @@ class Timeout:
# The purpose is to time out as soon as possible
# without waiting for the next await expression.

__slots__ = ("_deadline", "_loop", "_state", "_timeout_handler")
__slots__ = ("_deadline", "_loop", "_state", "_timeout_handler", "_task")

def __init__(
self, deadline: Optional[float], loop: asyncio.AbstractEventLoop
) -> None:
self._loop = loop
self._state = _State.INIT

self._task: Optional["asyncio.Task[object]"] = None
self._timeout_handler = None # type: Optional[asyncio.Handle]
if deadline is None:
self._deadline = None # type: Optional[float]
Expand Down Expand Up @@ -187,6 +199,7 @@ def reject(self) -> None:
self._reject()

def _reject(self) -> None:
self._task = None
if self._timeout_handler is not None:
self._timeout_handler.cancel()
self._timeout_handler = None
Expand Down Expand Up @@ -234,11 +247,11 @@ def _reschedule(self) -> None:
if self._timeout_handler is not None:
self._timeout_handler.cancel()

task = asyncio.current_task()
self._task = asyncio.current_task()
if deadline <= now:
self._timeout_handler = self._loop.call_soon(self._on_timeout, task)
self._timeout_handler = self._loop.call_soon(self._on_timeout)
else:
self._timeout_handler = self._loop.call_at(deadline, self._on_timeout, task)
self._timeout_handler = self._loop.call_at(deadline, self._on_timeout)

def _do_enter(self) -> None:
if self._state != _State.INIT:
Expand All @@ -248,18 +261,19 @@ def _do_enter(self) -> None:

def _do_exit(self, exc_type: Optional[Type[BaseException]]) -> None:
if exc_type is asyncio.CancelledError and self._state == _State.TIMEOUT:
assert self._task is not None
_uncancel_task(self._task)
self._timeout_handler = None
self._task = None
raise asyncio.TimeoutError
# timeout has not expired
self._state = _State.EXIT
self._reject()
return None

def _on_timeout(self, task: "asyncio.Task[None]") -> None:
task.cancel()
def _on_timeout(self) -> None:
assert self._task is not None
self._task.cancel()
self._state = _State.TIMEOUT
# drop the reference early
self._timeout_handler = None


# End https://github.com/aio-libs/async-timeout/blob/master/async_timeout/__init__.py

0 comments on commit d4aabe5

Please sign in to comment.