fork download
  1. import functools
  2. import inspect
  3.  
  4.  
  5. def type_checking(f):
  6. sig = inspect.signature(f)
  7. @functools.wraps(f)
  8. def wrapper(*args, **kwargs):
  9. bound = sig.bind(*args, **kwargs)
  10. for k, v in bound.arguments.items():
  11. if not isinstance(v, sig.parameters[k].annotation):
  12. raise TypeError()
  13. return_value = f(*args, **kwargs)
  14. if not isinstance(return_value, sig.return_annotation):
  15. raise TypeError()
  16. return return_value
  17. return wrapper
  18.  
  19.  
  20. @type_checking
  21. def double(x: int) -> int:
  22. return x * 2
  23.  
  24. print(double('a'))
  25.  
Runtime error #stdin #stdout #stderr 0.16s 10368KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Traceback (most recent call last):
  File "./prog.py", line 21, in <module>
  File "./prog.py", line 6, in type_checking
AttributeError: 'module' object has no attribute 'signature'