Skip to content

Help With Decorator Parameter Hints #4839

Answered by erictraut
Logon27 asked this question in Q&A
Discussion options

You must be logged in to vote

If you want to preserve the return type as well, you'll need to return it from within your new_example inner function.

P = ParamSpec("P")
R = TypeVar("R")

def example_decorator(example_debug: Callable[P, R]) -> Callable[P, R]:
    @functools.wraps(example_debug)
    def new_example(*args: P.args, **kwargs: P.kwargs) -> R:
        print(f"my number: {args[0]}")
        return example_debug(*args, **kwargs)

    return new_example

Replies: 1 comment 11 replies

Comment options

You must be logged in to vote
11 replies
@erictraut
Comment options

@Logon27
Comment options

@erictraut
Comment options

@erictraut
Comment options

Answer selected by Logon27
@erictraut
Comment options

@Logon27
Comment options

@Logon27
Comment options

@Logon27
Comment options

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