Skip to main content

clone_method

FunctionPrivateDecoratorSource

decorator: Callable[
[Callable[Concatenate[S, P], T]],
Callable[Concatenate[S, P], T]
] = clone_method[**P, S, T](
func_o: Callable[Concatenate[P], Any],
)

class Example:
@clone_method(func_o)
def func(self, val1, val2, ...): ...

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

This decorator should be only used for cloning the regular bounded method. It is not suitable for classmethod, staticmethod or regular functions.

note

Note that this decorator will not modify the returned value of the decorated method 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. Its signature is expected to be the same as the bounded method.

Note that the function itself is not a bounded method.

Returns

ArgumentType
Description
decorator((self: S, **P) -> T) -> ((self: S, **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 method.

Examples

Clone the signature of a function to a method

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


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


class Example:
@fsc.utilities.clone_method(test_func)
def modified_method(self, *args, **kwargs) -> str:
return "not a float!"


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