Use PEP 695 for decorator typing with type aliases (2) (#117663)
This commit is contained in:
parent
d65437e347
commit
907b9c42e5
6 changed files with 41 additions and 55 deletions
|
@ -12,7 +12,7 @@ from itertools import islice
|
|||
import logging
|
||||
import os
|
||||
import time
|
||||
from typing import TYPE_CHECKING, Any, Concatenate, NoReturn, ParamSpec, TypeVar
|
||||
from typing import TYPE_CHECKING, Any, Concatenate, NoReturn
|
||||
|
||||
from awesomeversion import (
|
||||
AwesomeVersion,
|
||||
|
@ -61,9 +61,6 @@ if TYPE_CHECKING:
|
|||
|
||||
from . import Recorder
|
||||
|
||||
_RecorderT = TypeVar("_RecorderT", bound="Recorder")
|
||||
_P = ParamSpec("_P")
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
RETRIES = 3
|
||||
|
@ -628,18 +625,20 @@ def _is_retryable_error(instance: Recorder, err: OperationalError) -> bool:
|
|||
)
|
||||
|
||||
|
||||
_FuncType = Callable[Concatenate[_RecorderT, _P], bool]
|
||||
type _FuncType[_T, **_P, _R] = Callable[Concatenate[_T, _P], _R]
|
||||
|
||||
|
||||
def retryable_database_job(
|
||||
def retryable_database_job[_RecorderT: Recorder, **_P](
|
||||
description: str,
|
||||
) -> Callable[[_FuncType[_RecorderT, _P]], _FuncType[_RecorderT, _P]]:
|
||||
) -> Callable[[_FuncType[_RecorderT, _P, bool]], _FuncType[_RecorderT, _P, bool]]:
|
||||
"""Try to execute a database job.
|
||||
|
||||
The job should return True if it finished, and False if it needs to be rescheduled.
|
||||
"""
|
||||
|
||||
def decorator(job: _FuncType[_RecorderT, _P]) -> _FuncType[_RecorderT, _P]:
|
||||
def decorator(
|
||||
job: _FuncType[_RecorderT, _P, bool],
|
||||
) -> _FuncType[_RecorderT, _P, bool]:
|
||||
@functools.wraps(job)
|
||||
def wrapper(instance: _RecorderT, *args: _P.args, **kwargs: _P.kwargs) -> bool:
|
||||
try:
|
||||
|
@ -664,12 +663,9 @@ def retryable_database_job(
|
|||
return decorator
|
||||
|
||||
|
||||
_WrappedFuncType = Callable[Concatenate[_RecorderT, _P], None]
|
||||
|
||||
|
||||
def database_job_retry_wrapper(
|
||||
def database_job_retry_wrapper[_RecorderT: Recorder, **_P](
|
||||
description: str, attempts: int = 5
|
||||
) -> Callable[[_WrappedFuncType[_RecorderT, _P]], _WrappedFuncType[_RecorderT, _P]]:
|
||||
) -> Callable[[_FuncType[_RecorderT, _P, None]], _FuncType[_RecorderT, _P, None]]:
|
||||
"""Try to execute a database job multiple times.
|
||||
|
||||
This wrapper handles InnoDB deadlocks and lock timeouts.
|
||||
|
@ -679,8 +675,8 @@ def database_job_retry_wrapper(
|
|||
"""
|
||||
|
||||
def decorator(
|
||||
job: _WrappedFuncType[_RecorderT, _P],
|
||||
) -> _WrappedFuncType[_RecorderT, _P]:
|
||||
job: _FuncType[_RecorderT, _P, None],
|
||||
) -> _FuncType[_RecorderT, _P, None]:
|
||||
@functools.wraps(job)
|
||||
def wrapper(instance: _RecorderT, *args: _P.args, **kwargs: _P.kwargs) -> None:
|
||||
for attempt in range(attempts):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue