1MODES = ('in', 'out')
2
3
4def type_check(mode):
5 """Outer wrapper takes mode input(currently in or out)"""
6 def type_check_inner(arg, *args):
7 """Inner wrapper takes allowed types with a minimum of 1"""
8 allowed_types = [arg] + list(args)
9 def take_func(func):
10 """Takes function to be decorated"""
11 def decorator(*args, **kwargs):
12 """Decorator checks if types are correct"""
13 if mode == MODES[0]:
14 inputted_types = list(map(type, args)) + list(map(type, kwargs.values()))
15 if any(inputted_type not in allowed_types for inputted_type in inputted_types):
16 print(f'Invalid input arguments, expected {', '.join(map(str, allowed_types))}!')
17 return func(*args, **kwargs)
18 elif mode == MODES[1]: # Didn't make it an else in case someone wants to add more modes
19 output = func(*args, **kwargs)
20 if type(output) not in allowed_types:
21 print(f'Invalid output value, expected {', '.join(map(str, allowed_types))}!')
22 return output
23 return func(*args, **kwargs)
24 return decorator
25 return take_func
26 return type_check_inner
....
----------------------------------------------------------------------
Ran 4 tests in 0.002s
OK