Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
fixes:
- |
The ``@retry`` decorator's type overloads for the ``sleep=`` parameter
(e.g. ``sleep=trio.sleep``) have been improved. Previously, the
async-sleep overload used ``R | Awaitable[R]`` as the return type
bound, which was ambiguous: for ``async def f() -> T``, pyright could
infer ``R = Coroutine[Any, Any, T]`` instead of ``R = T``, producing
false-positive type errors in downstream code.
22 changes: 19 additions & 3 deletions tenacity/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import sys
import threading
import time
import types
import typing as t
import warnings
from abc import ABC, abstractmethod
Expand Down Expand Up @@ -87,8 +88,6 @@
tornado = None

if t.TYPE_CHECKING:
import types

from typing_extensions import Self

from . import asyncio as tasyncio
Expand Down Expand Up @@ -585,6 +584,23 @@ def __repr__(self) -> str:
return f"<{clsname} {id(self)}: attempt #{self.attempt_number}; slept for {slept}; last result: {result}>"


class _AsyncRetryDecorator(t.Protocol):
@t.overload
def __call__(
self, fn: "t.Callable[P, types.CoroutineType[t.Any, t.Any, R]]"
) -> "t.Callable[P, types.CoroutineType[t.Any, t.Any, R]]": ...
@t.overload
def __call__(
self, fn: t.Callable[P, t.Coroutine[t.Any, t.Any, R]]
) -> t.Callable[P, t.Coroutine[t.Any, t.Any, R]]: ...
@t.overload
def __call__(
self, fn: t.Callable[P, t.Awaitable[R]]
) -> t.Callable[P, t.Awaitable[R]]: ...
@t.overload
def __call__(self, fn: t.Callable[P, R]) -> t.Callable[P, t.Awaitable[R]]: ...
Comment on lines +600 to +601

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and then this is the "if wasn't a coroutine function or otherwise returning an awaitable, it is now" annotation



@t.overload
def retry(func: WrappedFn) -> WrappedFn: ...

Expand All @@ -606,7 +622,7 @@ def retry(
retry_error_callback: t.Optional[
t.Callable[["RetryCallState"], t.Union[t.Any, t.Awaitable[t.Any]]]
] = ...,
) -> t.Callable[[t.Callable[P, R | t.Awaitable[R]]], t.Callable[P, t.Awaitable[R]]]: ...
) -> _AsyncRetryDecorator: ...


@t.overload
Expand Down