-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathenforceTH.py
39 lines (26 loc) · 931 Bytes
/
enforceTH.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import functools
def enforceType(func):
@functools.wraps(func)
def wrapper(*args):
wrapper.has_been_called = True
x = func.__annotations__
t = [x[i] for i in x if i != 'return']
if len(args) != len(t):
raise TypeError("Missing required positional arguments and/or annotations.")
for i in range(len(t)):
if not isinstance(args[i],t[i]):
raise ValueError(f"Invalid literal for {t[i]}: {args[i]}")
try:
ReturnValue = x['return']
except KeyError:
raise TypeError("Missing required return value annotation.")
try:
RV = func(*args)
except Exception as e:
raise Exception(e)
ReturnValue = type(ReturnValue) if ReturnValue == None else ReturnValue
if not isinstance(RV, ReturnValue):
raise SyntaxWarning(f"Expected function to return {ReturnValue}. Got {type(RV)} instead.")
return RV
wrapper.has_been_called = False
return wrapper