refactored typechecked()
This commit is contained in:
@ -5,12 +5,14 @@ from .utils import typename
|
|||||||
|
|
||||||
def typechecked(func):
|
def typechecked(func):
|
||||||
@functools.wraps(func)
|
@functools.wraps(func)
|
||||||
def typechecked_call(*args, **kwargs):
|
def typechecked_call(obj, *args, **kwargs):
|
||||||
func_types = get_type_hints(func)
|
type_hints = get_type_hints(func)
|
||||||
for index, key in enumerate(func_types.keys()):
|
del type_hints["return"]
|
||||||
if key != "return":
|
for arg, dtype in zip(args, type_hints):
|
||||||
assert func_types[key] == type(args[index+1]), f"{repr(func)} expected to receive input of type {func_types[key].__name__} but received {type(args[index+1]).__name__}"
|
arg_type = type(arg)
|
||||||
return func(*args, **kwargs)
|
if dtype != arg_type:
|
||||||
|
raise TypeError(f"{func} expected to receive input of type {dtype.__name__} but received {arg_type.__name__}")
|
||||||
|
return func(obj, *args, **kwargs)
|
||||||
return typechecked_call
|
return typechecked_call
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user