From a4dca78dd9dad00eed592d3d453902fb9e7f82f8 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 5 Oct 2023 00:31:58 +0000 Subject: [PATCH] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- pyproject.toml | 8 +-- src/filelock/asyncio/_api.py | 2 +- src/filelock/vendor/async_timeout.py | 78 ++++++++++++++++------------ 3 files changed, 49 insertions(+), 39 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 257a08ce..4eb059a7 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -48,10 +48,10 @@ optional-dependencies.testing = [ "coverage>=7.3", "diff-cover>=7.7", "pytest>=7.4", + "pytest-asyncio>=0.21.1", "pytest-cov>=4.1", "pytest-mock>=3.11.1", "pytest-timeout>=2.1", - "pytest-asyncio>=0.21.1", ] optional-dependencies.typing = [ 'typing-extensions>=4.7.1; python_version < "3.11"', @@ -93,6 +93,9 @@ ignore = [ "PLR2004", # Magic value used in comparison, consider replacing with a constant variable ] +[tool.pytest.ini_options] +asyncio_mode = "auto" + [tool.coverage] html.show_contexts = true html.skip_covered = false @@ -107,6 +110,3 @@ python_version = "3.11" show_error_codes = true strict = true overrides = [{ module = ["appdirs.*", "jnius.*"], ignore_missing_imports = true }] - -[tool.pytest.ini_options] -asyncio_mode = "auto" diff --git a/src/filelock/asyncio/_api.py b/src/filelock/asyncio/_api.py index 85241c6f..73df64ba 100644 --- a/src/filelock/asyncio/_api.py +++ b/src/filelock/asyncio/_api.py @@ -17,7 +17,7 @@ if sys.version_info >= (3, 11): from asyncio import timeout as atimeout else: - from ..vendor.async_timeout import timeout as atimeout + from filelock.vendor.async_timeout import timeout as atimeout @asynccontextmanager diff --git a/src/filelock/vendor/async_timeout.py b/src/filelock/vendor/async_timeout.py index 9c667d5d..3205134d 100644 --- a/src/filelock/vendor/async_timeout.py +++ b/src/filelock/vendor/async_timeout.py @@ -1,12 +1,14 @@ -""" -Vendored from https://github.com/aio-libs/async-timeout/blob/master/async_timeout/__init__.py -""" +"""Vendored from https://github.com/aio-libs/async-timeout/blob/master/async_timeout/__init__.py.""" +from __future__ import annotations + import asyncio import enum import sys import warnings -from types import TracebackType -from typing import Optional, Type +from typing import TYPE_CHECKING + +if TYPE_CHECKING: + from types import TracebackType if sys.version_info >= (3, 8): from typing import final @@ -16,12 +18,12 @@ if sys.version_info >= (3, 11): - def _uncancel_task(task: "asyncio.Task[object]") -> None: + def _uncancel_task(task: asyncio.Task[object]) -> None: task.uncancel() else: - def _uncancel_task(task: "asyncio.Task[object]") -> None: + def _uncancel_task(task: asyncio.Task[object]) -> None: pass @@ -31,8 +33,9 @@ def _uncancel_task(task: "asyncio.Task[object]") -> None: __all__ = ("timeout", "timeout_at", "Timeout") -def timeout(delay: Optional[float]) -> "Timeout": - """timeout context manager. +def timeout(delay: float | None) -> Timeout: + """ + timeout context manager. Useful in cases when you want to apply timeout logic around block of code or in cases when asyncio.wait_for is not suitable. For example: @@ -52,8 +55,9 @@ def timeout(delay: Optional[float]) -> "Timeout": return Timeout(deadline, loop) -def timeout_at(deadline: Optional[float]) -> "Timeout": - """Schedule the timeout at absolute time. +def timeout_at(deadline: float | None) -> Timeout: + """ + Schedule the timeout at absolute time. deadline argument points on the time in the same clock system as loop.time(). @@ -99,18 +103,18 @@ class Timeout: __slots__ = ("_deadline", "_loop", "_state", "_timeout_handler", "_task") - def __init__(self, deadline: Optional[float], loop: asyncio.AbstractEventLoop) -> None: + def __init__(self, deadline: float | None, loop: asyncio.AbstractEventLoop) -> None: self._loop = loop self._state = _State.INIT - self._task: Optional["asyncio.Task[object]"] = None + self._task: asyncio.Task[object] | None = None self._timeout_handler = None # type: Optional[asyncio.Handle] if deadline is None: self._deadline = None # type: Optional[float] else: self.update(deadline) - def __enter__(self) -> "Timeout": + def __enter__(self) -> Timeout: warnings.warn( "with timeout() is deprecated, use async with timeout() instead", DeprecationWarning, @@ -121,33 +125,33 @@ def __enter__(self) -> "Timeout": def __exit__( self, - exc_type: Optional[Type[BaseException]], - exc_val: Optional[BaseException], - exc_tb: Optional[TracebackType], - ) -> Optional[bool]: + exc_type: type[BaseException] | None, + exc_val: BaseException | None, + exc_tb: TracebackType | None, + ) -> bool | None: self._do_exit(exc_type) return None - async def __aenter__(self) -> "Timeout": + async def __aenter__(self) -> Timeout: self._do_enter() return self async def __aexit__( self, - exc_type: Optional[Type[BaseException]], - exc_val: Optional[BaseException], - exc_tb: Optional[TracebackType], - ) -> Optional[bool]: + exc_type: type[BaseException] | None, + exc_val: BaseException | None, + exc_tb: TracebackType | None, + ) -> bool | None: self._do_exit(exc_type) return None @property def expired(self) -> bool: - """Is timeout expired during execution?""" + """Is timeout expired during execution?.""" return self._state == _State.TIMEOUT @property - def deadline(self) -> Optional[float]: + def deadline(self) -> float | None: return self._deadline def reject(self) -> None: @@ -155,7 +159,8 @@ def reject(self) -> None: # cancel is maybe better name but # task.cancel() raises CancelledError in asyncio world. if self._state not in (_State.INIT, _State.ENTER): - raise RuntimeError(f"invalid state {self._state.value}") + msg = f"invalid state {self._state.value}" + raise RuntimeError(msg) self._reject() def _reject(self) -> None: @@ -165,7 +170,8 @@ def _reject(self) -> None: self._timeout_handler = None def shift(self, delay: float) -> None: - """Advance timeout on delay seconds. + """ + Advance timeout on delay seconds. The delay can be negative. @@ -173,11 +179,13 @@ def shift(self, delay: float) -> None: """ deadline = self._deadline if deadline is None: - raise RuntimeError("cannot shift timeout if deadline is not scheduled") + msg = "cannot shift timeout if deadline is not scheduled" + raise RuntimeError(msg) self.update(deadline + delay) def update(self, deadline: float) -> None: - """Set deadline to absolute value. + """ + Set deadline to absolute value. deadline argument points on the time in the same clock system as loop.time(). @@ -188,9 +196,11 @@ def update(self, deadline: float) -> None: undefined starting base, e.g. the time of the system power on. """ if self._state == _State.EXIT: - raise RuntimeError("cannot reschedule after exit from context manager") + msg = "cannot reschedule after exit from context manager" + raise RuntimeError(msg) if self._state == _State.TIMEOUT: - raise RuntimeError("cannot reschedule expired timeout") + msg = "cannot reschedule expired timeout" + raise RuntimeError(msg) if self._timeout_handler is not None: self._timeout_handler.cancel() self._deadline = deadline @@ -215,11 +225,12 @@ def _reschedule(self) -> None: def _do_enter(self) -> None: if self._state != _State.INIT: - raise RuntimeError(f"invalid state {self._state.value}") + msg = f"invalid state {self._state.value}" + raise RuntimeError(msg) self._state = _State.ENTER self._reschedule() - def _do_exit(self, exc_type: Optional[Type[BaseException]]) -> None: + def _do_exit(self, exc_type: type[BaseException] | None) -> None: if exc_type is asyncio.CancelledError and self._state == _State.TIMEOUT: assert self._task is not None _uncancel_task(self._task) @@ -229,7 +240,6 @@ def _do_exit(self, exc_type: Optional[Type[BaseException]]) -> None: # timeout has not expired self._state = _State.EXIT self._reject() - return None def _on_timeout(self) -> None: assert self._task is not None