Skip to main content

clone_function

FunctionPrivateDecoratorSource

decorator: Callable[
[Callable[P, T]],
Callable[P, T]
] = clone_function[**P, T](
func_o: Callable[P, Any],
)

@clone_function(func_o)
def func(val1, val2, ...): ...

A decorator for modifying the static type hint of a function by cloning the signature of another function.

This decorator should be only used for cloning the regular functions. It is not suitable for methods.

note

Note that this decorator will not modify the the returned value of the decorated function in the signature.

note

Note that this decorator will not change the run time behavior of the decorated method.

Arguments

Requires

ArgumentTypeRequired
Description
func_o(**P) -> Any

The original function providing the signature to be cloned.

Returns

ArgumentType
Description
decorator((**P) -> T) -> ((**P) -> T)

A wrapper that forward a function as it is directly but the signature of the function will be consistent with the argument func_o.

The input signature will be copied from func_o. But the output value will be determined by the wrapped function.

Examples

Clone the signature of a function to another function

clone_signature_to_function.py
from typing import reveal_type
import inspect
import flask_sqlalchemy_compat as fsc


def test_func(val1: int, val2: str) -> float: ...


@fsc.utilities.clone_function(test_func)
def modified_func(*args, **kwargs) -> str:
return "not a float!"


if __name__ == "__main__":
reveal_type(modified_func)
print(inspect.signature(modified_func))
print(modified_func(1, "2"))