Skip to content

How to make pyright not raise an error when using decorators that add additional function arguments? #3693

Discussion options

You must be logged in to vote

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 and Concatenate.

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…

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@vamshiaruru-virgodesigns
Comment options

@erictraut
Comment options

@vamshiaruru-virgodesigns
Comment options

Answer selected by vamshiaruru-virgodesigns
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants