Skip to content

Commit a1f7564

Browse files
Merge pull request #2001 from gitautoai/wes
Add ParamSpec and TypeVar to handle_exceptions decorator for better t…
2 parents 767ea6a + b7bb29a commit a1f7564

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
*private-key.pem
12
/scripts/
23
.coverage*
34
.env*

utils/error/handle_exceptions.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,25 @@
55
import json
66
import logging
77
import time
8-
from typing import Any, Callable, Tuple
8+
from typing import Any, Callable, ParamSpec, TypeVar
99

1010
# Third party imports
1111
import requests
1212

13+
P = ParamSpec("P")
14+
R = TypeVar("R")
15+
1316

1417
def handle_exceptions(
1518
default_return_value: Any = None,
1619
raise_on_error: bool = False,
1720
api_type: str = "github", # "github" or "google"
18-
) -> Callable[[Callable], Callable]:
21+
) -> Callable[[Callable[P, R]], Callable[P, R]]:
1922
"""https://docs.github.com/en/rest/using-the-rest-api/rate-limits-for-the-rest-api?apiVersion=2022-11-28#checking-the-status-of-your-rate-limit"""
2023

21-
def decorator(func: Callable) -> Callable:
24+
def decorator(func: Callable[P, R]) -> Callable[P, R]:
2225
@wraps(wrapped=func)
23-
def wrapper(*args: Tuple[Any, ...], **kwargs: Any):
26+
def wrapper(*args: P.args, **kwargs: P.kwargs) -> R:
2427
log_args = list(args)
2528
log_kwargs = dict(kwargs)
2629

@@ -37,14 +40,16 @@ def wrapper(*args: Tuple[Any, ...], **kwargs: Any):
3740
if err.response is None:
3841
if raise_on_error:
3942
raise
40-
return error_return
43+
# Type system cannot verify default_return_value matches R
44+
return error_return # type: ignore[return-value]
4145
status_code: int = err.response.status_code
4246

4347
# Skip logging for 500 Internal Server Error as it's usually a temporary issue and no meaningful information is available
4448
if status_code == 500:
4549
if raise_on_error:
4650
raise
47-
return error_return
51+
# Type system cannot verify default_return_value matches R
52+
return error_return # type: ignore[return-value]
4853

4954
reason: str | Any = (
5055
str(err.response.reason)
@@ -128,7 +133,8 @@ def wrapper(*args: Tuple[Any, ...], **kwargs: Any):
128133
logging.error(msg=err_msg)
129134
if raise_on_error:
130135
raise
131-
return error_return
136+
# Type system cannot verify default_return_value matches R
137+
return error_return # type: ignore[return-value]
132138

133139
return wrapper
134140

0 commit comments

Comments
 (0)