Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from inspect import signature
- from functools import wraps
- def check_input_type(func):
- sig = signature(func)
- parameters = sig.parameters
- @wraps(func)
- def wrapper(*args, **kwargs):
- for position, (arg, (name, arg_sig)) in enumerate(zip(args, parameters.items())):
- if not isinstance(arg, arg_sig.annotation):
- raise ValueError(f"Type of argument at position {position} is invalid, Type {arg_sig.annotation} expected.")
- for name in kwargs:
- if not name in parameters:
- raise ValueError(f"Unexpected keyword argument")
- arg_sig = parameters[name]
- if not isinstance(kwargs[name], arg_sig.annotation):
- raise ValueError(f"The value of {name!r} is invalid, Type {arg_sig.annotation} expected.")
- ret = func(*args, **kwargs)
- return ret
- return wrapper
- @check_input_type
- def func(text: str, n: int):
- print(text, n)
- func('adsf',12)
- func(123,'asdf')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement