Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Oct 5, 2023
1 parent 5ace012 commit a4dca78
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 39 deletions.
8 changes: 4 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"',
Expand Down Expand Up @@ -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
Expand All @@ -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"
2 changes: 1 addition & 1 deletion src/filelock/asyncio/_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
78 changes: 44 additions & 34 deletions src/filelock/vendor/async_timeout.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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


Expand All @@ -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:
Expand All @@ -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().
Expand Down Expand Up @@ -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,
Expand All @@ -121,41 +125,42 @@ 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:
"""Reject scheduled timeout if any."""
# 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:
Expand All @@ -165,19 +170,22 @@ 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.
Raise RuntimeError if shift is called when deadline is not scheduled
"""
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().
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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
Expand Down

0 comments on commit a4dca78

Please sign in to comment.