How to make pyright not raise an error when using decorators that add additional function arguments? #3693
-
Hi all, I am very new to using type right, so pardon me if it is something obvious. I have a decorator like this: def inject_client(url=None, auth_token=None):
def wrap(function):
@functools.wraps(function)
async def wrapper(*args, **kwargs):
async with Client(url, auth_token) as client:
return await function(client=client, *args, **kwargs)
return wrapper
return wrap and I use it like this: @inject_client()
async def create_resources(
param1: str, param2: str, client: "GraphqlClient"
) -> Any:
pass
await create_resource(param1, param2) and it works.
Is there anyway to let pyright know that a decorator has added that parameter already? Or is there a work around or something? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
There is currently no way in the Python type system to indicate that a decorator removes or adds a keyword argument. There is a way to indicate that positional arguments are prepended to (or removed from) the beginning of a parameter list using PEP 612's from typing import Any, Concatenate, ParamSpec, TypeVar
P = ParamSpec("P")
R = TypeVar("R")
class Client: ...
def inject_client(
url=None, auth_token=None
) -> Callable[[Callable[Concatenate[Client, P], R]], Callable[P, R]]:
...
@inject_client()
async def create_resources(client: Client, param1: str, param2: str) -> Any:
pass
create_resources(param1, param2) (For future inquiries, it's useful if you provide a self-contained and minimal code sample — one that doesn't refer to symbols outside of the sample.) |
Beta Was this translation helpful? Give feedback.
There is currently no way in the Python type system to indicate that a decorator removes or adds a keyword argument.
There is a way to indicate that positional arguments are prepended to (or removed from) the beginning of a parameter list using PEP 612's
ParamSpec
andConcatenate
.(For future inquiries, it's useful if…