Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support a method of copying function signature #10574

Open
KotlinIsland opened this issue Jun 2, 2021 · 3 comments
Open

Support a method of copying function signature #10574

KotlinIsland opened this issue Jun 2, 2021 · 3 comments
Labels

Comments

@KotlinIsland
Copy link
Contributor

KotlinIsland commented Jun 2, 2021

Feature
Capability to copy a functions signature, related to typing.ParamSpec.

def foo(a: str) -> None:
    ...


# pseudo code
def bar(*args: foo.args, **kwargs: foo.kwargs) -> foo.return_type:
    return foo(*args, **kwargs)

Pitch
If I want to forward a call to another method in a type safe way it requires duplicating type definitions which can rapidly become unwieldy and error prone(especially if the function is in a third party module). I would love a way to just say that one functions signature is the same as another functions signature (with support for typing.Concatenate)

Maybe

extend functionality of ParamSpec to be generic to a Callable?

def bar(*args: ParamSpec[foo].args, **kwargs: ParamSpec[foo].kwargs) -> None:
    return foo(*args, **kwargs)
@erictraut
Copy link

erictraut commented Jun 4, 2021

Perhaps one of these approaches would meet your needs using the existing functionality?

from typing import Callable, TypeVar
from typing_extensions import ParamSpec

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

def foo(a: str) -> None:
    ...

def wrap_func(fn: Callable[P, R]) -> Callable[P, R]:
    def inner(*args: P.args, **kwargs: P.kwargs) -> R:
        # Add code here as desired
        return fn(*args, **kwargs)
    return inner

bar = wrap_func(foo)

Or if you want to externalize the wrapper logic, wrap_func could take a wrapper function as an input parameter:

def wrap_func(
    wrapper: Callable[Concatenate[Callable[P, R], P], R], fn: Callable[P, R]
) -> Callable[P, R]:
    def inner(*args: P.args, **kwargs: P.kwargs) -> R:
        return wrapper(fn, *args, **kwargs)
    return inner

def bar_wrapper(fn: Callable[P, R], *args: P.args, **kwargs: P.kwargs) -> R:
    # Add code here as desired
    return fn(*args, *kwargs)

bar = wrap_func(bar_wrapper, foo)

@indigoviolet
Copy link

indigoviolet commented Jan 13, 2024

Both approaches above require passing the wrapped function fn or foo as an argument to the wrapping function. But a fairly common case is writing a particular function that invokes another function (say from a library, to create slightly better ergonomics or specialize some arguments). In that case, there doesn't appear to be a DRY method to describe the wrapper function's signature in terms of the wrapped function's signature (+/- some params).

See also #13617, #2003

@ligix
Copy link

ligix commented Jan 20, 2024

Or if you want to externalize the wrapper logic, wrap_func could take a wrapper function as an input parameter:

def wrap_func(
    wrapper: Callable[Concatenate[Callable[P, R], P], R], fn: Callable[P, R]
) -> Callable[P, R]:
    def inner(*args: P.args, **kwargs: P.kwargs) -> R:
        return wrapper(fn, *args, **kwargs)
    return inner

def bar_wrapper(fn: Callable[P, R], *args: P.args, **kwargs: P.kwargs) -> R:
    # Add code here as desired
    return fn(*args, *kwargs)

bar = wrap_func(bar_wrapper, foo)

A slightly different version that works as a decorator:

def wraps_function(
    fun: Callable[P, T]
) -> Callable[[Callable[Concatenate[Callable[P, T], P], T]], Callable[P, T]]:
    def decorator(
        wrapper: Callable[Concatenate[Callable[P, T], P], T]
    ) -> Callable[P, T]:
        def decorated(*args: P.args, **kwargs: P.kwargs) -> T:
            return wrapper(fun, *args, **kwargs)
        return decorated
    return decorator

@wraps_function(myfun)
def bar(myfun: Callable[P, T], *args: P.args, **kwargs: P.kwargs) -> T:
    # Add code here as desired
    return myfun(*args, **kwargs) 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants