forked from mtusman/gemini-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebugly.py
More file actions
29 lines (25 loc) · 1008 Bytes
/
debugly.py
File metadata and controls
29 lines (25 loc) · 1008 Bytes
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
# dubugly.py
# Mohammad Usman
#
# A collection of useful decorators
from inspect import signature
from functools import wraps
# Should only be used on functions/methods that don't have keyword arguments
def typeassert(*ty_args, **ty_kwargs):
def decorate(func):
# Map function argument names to supplied types
sig = signature(func)
bound_types = sig.bind_partial(*ty_args, **ty_kwargs).arguments
@wraps(func)
def wrapper(*args, **kwargs):
bound_values = sig.bind(*args, **kwargs)
# Enforce type assertions across supplied arguments
for name, value in bound_values.arguments.items():
if name in bound_types:
if not isinstance(value, bound_types[name]):
raise TypeError(
'Argument {} must be {}'.format(name, bound_types[name])
)
return func(*args, **kwargs)
return wrapper
return decorate