Skip to content

Commit 7c6388b

Browse files
Create client_injector for better type hints (#12793)
1 parent 1062ad1 commit 7c6388b

File tree

1 file changed

+25
-3
lines changed

1 file changed

+25
-3
lines changed

src/prefect/client/utilities.py

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,25 @@
66
# circular imports for decorators such as `inject_client` which are widely used.
77

88
from functools import wraps
9-
from typing import TYPE_CHECKING, Any, Callable, Coroutine, Optional, Tuple, cast
10-
11-
from typing_extensions import ParamSpec
9+
from typing import (
10+
TYPE_CHECKING,
11+
Any,
12+
Awaitable,
13+
Callable,
14+
Coroutine,
15+
Optional,
16+
Tuple,
17+
TypeVar,
18+
cast,
19+
)
20+
21+
from typing_extensions import Concatenate, ParamSpec
1222

1323
if TYPE_CHECKING:
1424
from prefect.client.orchestration import PrefectClient
1525

1626
P = ParamSpec("P")
27+
R = TypeVar("R")
1728

1829

1930
def get_or_create_client(
@@ -52,6 +63,17 @@ def get_or_create_client(
5263
return get_httpx_client(), False
5364

5465

66+
def client_injector(
67+
func: Callable[Concatenate["PrefectClient", P], Awaitable[R]],
68+
) -> Callable[P, Awaitable[R]]:
69+
@wraps(func)
70+
async def wrapper(*args: P.args, **kwargs: P.kwargs) -> R:
71+
client, _ = get_or_create_client()
72+
return await func(client, *args, **kwargs)
73+
74+
return wrapper
75+
76+
5577
def inject_client(
5678
fn: Callable[P, Coroutine[Any, Any, Any]],
5779
) -> Callable[P, Coroutine[Any, Any, Any]]:

0 commit comments

Comments
 (0)