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 that this decorator will not modify the returned value of the decorated method in the signature.
Note that this decorator will not change the run time behavior of the decorated method.
Arguments
Requires
Argument | Type | Required | |
---|---|---|---|
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
Argument | Type | |
---|---|---|
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 The input signature will be copied from |
Examples
Clone the signature of a function to a method
- Codes
- Results
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"))
Runtime type is 'method'
(val1: int, val2: str) -> str
not a float!