-
I have a function that is designed to retry an arbitrary function call some number of times: P = ParamSpec("P")
R = TypeVar("R")
def maybe_retry(
func: Callable[P, R],
retryable_exceptions: tuple[Type[Exception], ...] = tuple([Exception]),
max_attempts: int = 0,
*args: P.args,
**kwargs: P.kwargs,
) -> R:
if max_attempts == 0:
return func(*args, **kwargs) # etc. I have another function that wraps API requests and handles responses: def call_api(func: Callable, *args: object, **kwargs: object) -> Any:
res = maybe_retry(
func,
retryable_exceptions=(requests.ConnectionError, requests.ConnectTimeout),
*args,
**kwargs,
) # etc. While this code works, Pyright gives me this error:
Basically, any |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Pyright is doing the right thing here. For comparison, mypy emits the same error. One workaround is to replace the If you want to retain more type safety, you can do the following: def call_api(
func: Callable[P, R], max_attempts: int = 0, *args: P.args, **kwargs: P.kwargs
) -> R:
return maybe_retry(
func,
retryable_exceptions=(requests.ConnectionError, requests.ConnectTimeout),
max_attempts=max_attempts,
*args,
**kwargs,
) |
Beta Was this translation helpful? Give feedback.
Pyright is doing the right thing here. For comparison, mypy emits the same error.
One workaround is to replace the
object
annotations withAny
in thecall_api
signature.If you want to retain more type safety, you can do the following: