I have a python decorator that expects the signature of the passed function to be a User and a Point, and it returns a function with the same signature:
@dataclass
class User:
name: str
@dataclass
class Point:
x: float
y: float
z: float
def logging_decorator(f: Callable[[User, Point], bool]) -> Callable[[User, Point], bool]:
print('decorating function...')
def decorated(a: User, b: Point) -> bool:
print(f'User {a.name} calling with points {b.x},{b.y},{b.z}')
ret = f(a, b)
print(f'returned {ret}')
return ret
return decorated
At the point it prints decorating function..., I want it to check that the function which has been passed in has the correct signature, and give a sensible error message if it does not.
Here is an example of decorating two functions, one with the correct signature, and one with an incorrect one:
@logging_decorator
def good_f(a: User, b: Point) -> bool:
return True
@logging_decorator
def bad_f(a: str, b: str) -> str:
return "Hello"
How can I add checks to the logging_decorator so it checks that the function being decorated has the correct argument types?
I have tried using the typing_inspect and typeguard packages, but cannot find the functionality I need.
I am using Python 3.8.3