Skip to content
Discussion options

You must be logged in to vote

This works though

from collections.abc import Callable
from typing import Any, overload


def signature_from[F: Callable](_original: F) -> Callable[[F], F]:
    """Copies the signature of a function to another function."""

    def decorator(func: F) -> F:
        return func

    return decorator


@overload
def f(x: int) -> int: ...
@overload
def f(x: str) -> str: ...
@overload
def f(x: Any) -> Any: ...
def f(x: Any) -> Any:
    return x

# Expected
y1 = f(1)    # int
y2 = f("1")  # str
y3 = f(1.0)  # Any

@signature_from(f)
def g(*args, **kwargs) -> Any:
    if (len(args) + len(kwargs)) != 1:
        raise ValueError
    
    x = args[0] if len(args) == 1 else kwargs["x"]
    return f(x)…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by Glinte
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant